有什么方法可以获取安装 AllJoyn 的设备的 IP 地址?服务发布不会持续很长时间,我不能依赖它从 DNS 记录中读取 IP。AllJoyn 中是否有一个 API 可以返回设备的 IP 地址?我目前正在使用 Android 代码,但没有发现任何接近的东西。谢谢您的帮助。
问问题
191 次
2 回答
0
我没有用 AllJoyn 尝试过,但我在 android 上使用这段代码从 eth0 端口获取 ipaddress;认为这可能会帮助你 -
Class<?> SystemProperties = Class.forName("android.os.SystemProperties");
Method method = SystemProperties.getMethod("get", new Class[]{String.class});
String ip = null;
return ip = (String) method.invoke(null,"dhcp.eth0.ipaddress");
于 2016-03-24T23:59:17.257 回答
0
最终使用作为 AP 名称通告的 MAC 地址,并通过解析可通过 /proc/net/arp 文件访问的 ARP 缓存进行反向查找。
if (device.getAPWifiInfo() != null) {
String mac = device.getAPWifiInfo().getSSID();
String split_mac[] = mac.split(" ");
Log.i(TAG, "Mac from ssid is " + split_mac[1]);
mac = split_mac[1];
ip = getIPfromMac(mac);
Log.i(TAG, "IP is " + ip);
}
//returns the ip and takes mac address as parameter
public static String getIPfromMac(String mac) {
if (mac == null)
return null;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
String[] splitted = line.split(" +");
if (splitted != null && splitted.length >= 4 && mac.equalsIgnoreCase(splitted[3])) {
// Basic sanity check
String ip = splitted[0];
return ip;
}
}
return null;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
于 2016-03-29T20:32:25.447 回答