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