23

I am trying to get the IP address of an device i.e using WIFI or 3G connection. I am getting the ip address in IPV6 format which is not understandable. I want in IPV4 format IP address.I have done google but dint found any proper solutions.

here is code which I am using to get IP address of an device

public String getLocalIpAddress() {
    try {
        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();
                System.out.println("ip1--:" + inetAddress);
                System.out.println("ip2--:" + inetAddress.getHostAddress());
                if (!inetAddress.isLoopbackAddress()) {


                    String ip = inetAddress.getHostAddress().toString();
                    System.out.println("ip---::" + ip);
                    EditText tv = (EditText) findViewById(R.id.ipadd);
                    tv.setText(ip);
                    return inetAddress.getHostAddress().toString();

                }
            }
        }
    } catch (Exception ex) {
        Log.e("IP Address", ex.toString());
    }
    return null;
}

I am getting this ouput :

ip1--:/fe80::5054:ff:fe12:3456%eth0%2
ip2--:fe80::5054:ff:fe12:3456%eth0

It should be displayed like this :

192.168.1.1

please help me out..

Joe
  • 41,484
  • 20
  • 104
  • 125
Rahul Baradia
  • 11,802
  • 17
  • 73
  • 121

4 Answers4

48

After trying many tricks.. finally I can get the IP address in IPV4 format.. Here is my code..

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();
                System.out.println("ip1--:" + inetAddress);
                System.out.println("ip2--:" + inetAddress.getHostAddress());

      // for getting IPV4 format
      if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress())) {

                    String ip = inetAddress.getHostAddress().toString();
                    System.out.println("ip---::" + ip);
                    EditText tv = (EditText) findViewById(R.id.ipadd);
                    tv.setText(ip);
                    // return inetAddress.getHostAddress().toString();
                    return ip;
                }
            }
        }
    } catch (Exception ex) {
        Log.e("IP Address", ex.toString());
    }
    return null;
}

Added if condition as shown below

 /**This shows IPV4 format IP address*/
 if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress())){}

instead of this

 /**This shows IPV6 format IP address*/
 if (!inetAddress.isLoopbackAddress()){}

Many Thanks.. Rahul

An alternative for checking if the address is a version 4 address is:

if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address)
Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
Rahul Baradia
  • 11,802
  • 17
  • 73
  • 121
9

You cannot assume that any device has just one network address. You also cannot assume that it will have any IPv4 - it may be IPv6 only, so your application will need to be able to handle both IPv4 and IPv6 address displays.

Typically, an Android phone has at least two interfaces that get assigned usable ip addresses, rmnet0 for the 3G data, which for IPv4 is often carrier-grade NATed and so cannot accept incoming socket connections, and may also have an IPv6 address; and wlan0 for the wifi, which will have whatever IPv4 and/or IPv6 address it can negotiate with the network it attaches to.

Some versions of Android will intentionally drop the (typically more expensive) rmnet0 link when it attaches to wifi - in an attempt to reduce 3G data usage. This behaviour is a problem when the wifi has attached to something that is a captive portal that requires a manual sign-in.

Simon Hewison
  • 91
  • 1
  • 1
2

It seems there is a seperate class Inet4Address in the Java API for IPv4 addresses.

Community
  • 1
  • 1
0nyx
  • 144
  • 2
  • 12
  • I tried but its giving default ip address of an emulator not localhost ip address.. – Rahul Baradia Jun 13 '12 at 14:13
  • Localhost is 127.0.0.1, I doubt that is what you want. – Chris Stratton Jun 13 '12 at 14:52
  • @Tech.Rahul if you wanted to get the localhost could you not use something like `InetAddress addr = InetAddress.getLocalHost();`? – 0nyx Jun 13 '12 at 15:43
  • Are you on an emulator? If you so may have some difficulties, both in that the emulator emulates a mobile connection rather the wifi, and in that it's network connection is a forwarding thing through the hosting pc. – Chris Stratton Jun 14 '12 at 04:25
  • @ChrisStratton I am trying to get in Emulator.. is it possible to get right.. ?? – Rahul Baradia Jun 14 '12 at 04:26
  • What do you hope to do with it? The emulator will not accept traffic from the hosting machine's external interfaces anyway, and you know from the docs what address the emulator appears to have to things running on the hosting machine. Basically the only time knowing the actual IP address will be of practical use is when you have a real device running on wifi and want to contact the device from something else on the subnet, and in that case you can get the address from the wifi APIs. – Chris Stratton Jun 14 '12 at 04:30
  • ya .. on actual device we can know the IP address.. but was getting in IPV6 format.. now I am able to get in IPV4 format too.. thanks fr the help .. – Rahul Baradia Jun 14 '12 at 04:58
0

Following is a simple way to check either if it's IPv4 or IPv6:

InetAddress address = InetAddress.getByName(ip);
if (address instanceof Inet6Address) {
    // It's ipv6
} else if (address instanceof Inet4Address) {
    // It's ipv4
}
rkachach
  • 16,517
  • 6
  • 42
  • 66