Getting My LAN ip address (192.168.xxxx) (IPV4)
16,612
Solution 1
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" />
Solution 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" />
Author by
Vishnudev K
Am a Java developer curious about coding and programming. Android development is my hobby.
Updated on July 19, 2022Comments
-
Vishnudev K almost 2 years
In my android device I am trying to find its IP address(IPV4).
If I do the following codeInetAddress 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.)
-
Vishnudev K about 11 yearsthanks for quick reply. I am getting "fe80::2064:32ff:fe5d:2edf%p2p0" as my address. Is it ipv6 address. how to get ipv4 address?
-
Srikant Sahay about 11 years+1 for checking all the interfaces. This will take care of both wifi and cellular interfaces
-
Ayush about 11 years@VishnudevK Thats because ur router is configured for ipv6 Address.
-
SJuan76 about 11 yearsAnd if he is not using WiFi?
-
Vishnudev K about 11 years@Ayush but in phone's status its showing the ipv4 address only. is there any way to convert it to ipv4.
-
Ken Wolf about 11 yearsWhat's an example of a device that doesn't use wifi to get an internal LAN address? 198.168.xx.xx. Ethernet port?
-
Ayush about 11 years@VishnudevK ipv4 and ipv6 Both are Different thing, they CANT BE CONVERTED.
-
Vishnudev K about 11 years@ayush got it. see the change I made
-
Ayush about 11 years@VishnudevK I have edited the question since the answer is for IPV4 only.
-
SJuan76 about 11 yearsAs 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). -
Ken Wolf about 11 years:) 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.