Quantcast
Channel: Android Mode - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 941

Get local IP-address of device

$
0
0

Hello,

in my application I would like to display the local IP-address of my device. I found the following on github. It kind of works. However, if I print the result to the console, it isn't a human readable IP-address but a string like fe80::8c3a:e3ff:fe99:345e%p2p0

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()) {
          return inetAddress.getHostAddress().toString();
        }
      }
    }
  }
  catch (SocketException ex) {
    // Log.e(LOG_TAG, ex.toString());
  }
  return null;
}

Has anyone got an idea how this could be transformed in a human readable IP-address? I've already looked up the documentation for getHostAddress() which says it should be an IP-address in the form of 127.0.0.1 and the like. But this doesn't seem to be the case...

THanks!


Viewing all articles
Browse latest Browse all 941

Trending Articles