0

I've written an app for HTC desire and it gets the devices IP address and prints it to the screen. When I install this app to my tab 10.1 the IP address come out in letter and numbers in a strange format?

private String getIpAddress()
    {
        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(TAG , ex.toString());
        }
        return null;
    }

What is different on the tab 10.1?

( IP returned - fe80::be47:60ff:feff:21e2)

Needs to be an IP address as this is what I pass to the terminal on PC to connect to my app.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Tom Lumbard ܤ
  • 23
  • 4
  • 11

1 Answers1

3

Your Galaxy Tab 10.1 receives an IPv6 address, in this case fe80::be47:60ff:feff:21e2. This might be caused by different preferences or capabilities of your HTC Desire and Galaxy Tab regarding IPv6.

There's nothing wrong with that, more and more devices will (have to) use IPv6 addresses when the IPv4 address space runs out.

As Nesim points out in his comment on the question, IPv6 addresses starting with fe80: are link-local addresses, i.e. a range of addresses a device self-assigns if it has no connectivity to any network that hands out addresses, e.g. via DHCP.

So it seems like your Galaxy Tab isn't connected to any wifi network or isn't receiving any address via DHCP.

The code snippet in the question returns the first address it finds and doesn't filter out link-local addresses (which aren't useful to connect from the outside). The following code gives you all addresses that are neither loopback nor link-local. How you select between many of them is up to you -- I honestly don't know:

private static List<InetAddress> getIpAddress() {
  try {
    List<InetAddress> result = new ArrayList<InetAddress>();

    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
      NetworkInterface intf = interfaces.nextElement();
      Enumeration<InetAddress> addresses = intf.getInetAddresses();
      while (addresses.hasMoreElements()) {
        InetAddress address = addresses.nextElement();
        if (!address.isLoopbackAddress() && !address.isLinkLocalAddress()) {
          result.add(address);
        }
      }
    }
    return result;
  } catch (SocketException ex) {
    Log.e(TAG, "Failed to list network interfaces.", ex);
    return null;
  }
}

For comparison, my Windows system lists 23 network interfaces (most of them virtual) with a total of 10 addresses, 2 of which are loop back addresses (localhost), 4 link-local addresses -- that leaves 4 adresses that the above code would return -- picking the first one seems overly optimistic.

Community
  • 1
  • 1
Philipp Reichart
  • 20,771
  • 6
  • 58
  • 65
  • Ok i understand this but i need to pass an ip address to the terminal on pc to connect to the android app – Tom Lumbard ܤ Apr 23 '12 at 19:27
  • It *is* an IP address, just not an IPv4 address. Any modern software should be able to handle both IPv4 and IPv6 -- if your terminal doesn't, you need to find another terminal. – Philipp Reichart Apr 23 '12 at 19:37
  • As Nesim points out in a comment on the question, this is a link-local address, i.e. an address the device self-assigns if it has no connectivity. Maybe your Galaxy Tab isn't connected to a wifi network? – Philipp Reichart Apr 23 '12 at 19:39
  • I can go to wifi setting and see the devices IP from there so its deffinatly connected. Would just be nice for that address to be printed out in the app but i can live with the long way! – Tom Lumbard ܤ Apr 23 '12 at 19:57
  • 1
    The code snippet in your question returns the first address it encounters -- you might want to consider the other adresses as well. I added some code to help you with that. – Philipp Reichart Apr 23 '12 at 21:05