teemo91 teemo91 - 18 days ago 6
Android Question

Get the IP address of the device onboarded with AllJoyn

Is there a way I can fetch the IP address of the device onboarded with AllJoyn? The service publish doesn't last for very long and I cannot rely on it to read the IP from the DNS record. Is there an API that is in AllJoyn that returns the IP address of the device onboarded? I am working with the Android code currently and found nothing that's close. Thanks for the help.

Answer

Ended up using the MAC address advertised as the name of the AP and doing a reverse lookup by parsing the ARP cache accessible through /proc/net/arp file.

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;
    }
Comments