Finding the IP address of Galaxy Tab 10.1 for application

与世无争的帅哥 提交于 2020-01-03 05:07:28

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/10286294/finding-the-ip-address-of-galaxy-tab-10-1-for-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!