Getting My LAN ip address (192.168.xxxx) (IPV4)
Asked Answered
H

2

15

In my android device I am trying to find its IP address(IPV4).
If I do the following code

InetAddress inet = InetAddress.getLocalHost();
System.out.println(inet.getHostAddress()); //giving me 127.0.0.1

The code is giving me 127.0.0.1.
I wanted to get the actual IP 198.168.xx.xx.

(In My pc the same code giving me the actual IP though.)

Hygrometer answered 22/6, 2013 at 14:53 Comment(4)
[OT] sorry but hiding a private IP (192.168.xxx?) is not necessary, you can't be hacked by people that knows that info.Draughtboard
@GrailsGuy Not duplicate to that, OP wants 192.168 local network address and not the public router address.Cranage
@LuiggiMendoza I know, I was lazy to put that there.Hygrometer
The answer of DigitalRounin is very useful for your purpose. Check thisSovran
D
25
public static String getIpAddress() { 
            try {
                for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                    NetworkInterface intf = en.nextElement();
                    for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                        InetAddress inetAddress = enumIpAddr.nextElement();
                        if (!inetAddress.isLoopbackAddress()&&inetAddress instanceof Inet4Address) {
                            String ipAddress=inetAddress.getHostAddress().toString();
                            Log.e("IP address",""+ipAddress);
                            return ipAddress;
                        }
                    }
                }
            } catch (SocketException ex) {
                Log.e("Socket exception in GetIP Address of Utilities", ex.toString());
            }
            return null; 
    }

Give permissions

Also add in mainfest.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Dysentery answered 22/6, 2013 at 14:55 Comment(7)
thanks for quick reply. I am getting "fe80::2064:32ff:fe5d:2edf%p2p0" as my address. Is it ipv6 address. how to get ipv4 address?Hygrometer
+1 for checking all the interfaces. This will take care of both wifi and cellular interfacesShearwater
@VishnudevK Thats because ur router is configured for ipv6 Address.Dysentery
@Dysentery but in phone's status its showing the ipv4 address only. is there any way to convert it to ipv4.Hygrometer
@VishnudevK ipv4 and ipv6 Both are Different thing, they CANT BE CONVERTED.Dysentery
@ayush got it. see the change I madeHygrometer
@VishnudevK I have edited the question since the answer is for IPV4 only.Dysentery
A
2

You can use this to get your IP address.

WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
return String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
        (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));

This returns it as a String in the form "X.X.X.X"

The only permission you need in your manifest.xml is

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Assembled answered 22/6, 2013 at 14:56 Comment(4)
And if he is not using WiFi?Apotheosize
What's an example of a device that doesn't use wifi to get an internal LAN address? 198.168.xx.xx. Ethernet port?Assembled
As often, I concentrate in the body of the question and miss data from the header (it is the only place where the LAN part is informed). And yes, an ethernet port is possible (though I agree that it is not that frequent nowadays).Apotheosize
:) np I understand. In my experience the code I posted works very well in production on Android devices to do exactly what the user asked.Assembled

© 2022 - 2024 — McMap. All rights reserved.