获取使用AllJoyn登录的设备的IP地址



有没有办法获取AllJoyn登录设备的IP地址?服务发布不会持续很长时间,我不能依赖它从DNS记录中读取IP。AllJoyn中是否有API返回登录设备的IP地址?我目前正在使用安卓代码,没有发现任何接近的东西。谢谢你的帮助。

最终使用广告中的MAC地址作为AP的名称,并通过解析可通过/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;
    }

我还没有在AllJoyn上尝试过,但我在android上使用这段代码从eth0端口获取ipad地址;认为这可能会对你有所帮助-

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");

最新更新