1

I have the need to list all available IP addresses for any particular Android Device.

I have found some sample code but this only results in returning one IP address, which happens to be an IPv6 address. I need to get all available IPs for any particular device. I do the same on an iOS version of this app and it returns 3 IPv6 addresses, one 192. address and one 10. address. I am trying to replicate the same on Android. I pass all values to an Array and display them in a list.

The code I have in place is:

public String getLocalIpAddress()
{
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    IPAddresses.setText(inetAddress.getHostAddress().toString());
                    return inetAddress.getHostAddress().toString();
                }
            }
         }
    } catch (SocketException ex) {
        String LOG_TAG = null;
        Log.e(LOG_TAG, ex.toString());
    }

    return null;
}
Moshe Katz
  • 15,992
  • 7
  • 69
  • 116
Dan
  • 2,304
  • 6
  • 42
  • 69

1 Answers1

5

It looks to me like your code is just returning the first match - is that not the problem? I would have expected you to build up list of addresses, and return that instead of just a single string. Something like this:

public String[] getLocalIpAddress()
{          
    ArrayList<String> addresses = new ArrayList<String>();
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    IPAddresses.setText(inetAddress.getHostAddress().toString());
                    addresses.add(inetAddress.getHostAddress().toString());
                }
             }
         }
     } catch (SocketException ex) {
         String LOG_TAG = null;
         Log.e(LOG_TAG, ex.toString());
     }
     return addresses.toArray(new String[0]);
}

I'm not sure what the IPAddresses.setText call is doing, so I've left it in, but I expect that would also need to be adjusted in some way to handle the fact that you could have multiple addresses being matched.

James Holderness
  • 22,721
  • 2
  • 40
  • 52
  • I'm attempting your solution and trying to pass the returned value to an Array with no luck. I'm trying: String[] ipArray = new String[] { getLocalIpAddress() }; I've been on iOS for a while and I'm bit rusty coming back to Android! – Dan Jun 03 '13 at 20:49
  • 1
    `String[] ipArray = getLocalIpAddress();` should be all you need to do. – James Holderness Jun 03 '13 at 20:52