As the title says... I'm trying to be able to get the IP of the wifi iface when it is configured as hotspot. Ideally, I would like to find something that works for all the phones.
Of course, the WifiManager is useless when it comes to get info from the AP.
Luckily, I've been able to get the IPs of all the interfaces by doing this:
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();
if (!inetAddress.isLoopbackAddress()) {
Log.d("IPs", inetAddress.getHostAddress() );
}
}
}
} catch (SocketException ex) {
Log.e(LOG_TAG, ex.toString());
}
return null;
}
This chunk of code will print all the IP of all the interfaces (Wifi hotspot included). The main problem is that I don't find a way to identify the WiFi interface. This is an issue since some phones have multiple interfaces (WiMax, etc). This is what I've tried so far:
Any suggestions?
Find the IP address on Android If you have an Android 11 smartphone, go to Settings > Network & internet > Wi-Fi. However, on Android 12, you can find this feature under Network & internet > Internet. Tap the name of your Wi-Fi network and check that you've joined if you aren't already connected.
The Android builtin wifi tethering is designed to use 192.168. 43.1/24 as the server, with netd handling the tethering, using dnsmasq . First DNS range is 192.168. 42.1-254 and and 2nd DNS range is 192.168.
Mobile hotspot does not provide IP address.
You're almost right, the default IP address of hotspot is 192.168. 43.1 (If device maker didn't change.) You can check the source code of Android framework (AOSP). Therefore, the default value is 192.168.
Here's what I did to get the wifi hotspot ip:
public String getWifiApIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
if (intf.getName().contains("wlan")) {
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()
&& (inetAddress.getAddress().length == 4)) {
Log.d(TAG, inetAddress.getHostAddress());
return inetAddress.getHostAddress();
}
}
}
}
} catch (SocketException ex) {
Log.e(TAG, ex.toString());
}
return null;
}
This will give you the IP address of any wifi device, which means it's not just for the hotspot. If you're connected to another wifi network (meaning you're not in hotspot mode), it'll return an IP.
You should check if you are in AP mode first or not. You can use this class for that: http://www.whitebyte.info/android/android-wifi-hotspot-manager-class
You can use this.
((WifiManager) mContext.getSystemService(Context.WIFI_SERVICE)).getDhcpInfo().serverAddress
Full Code
private String getHotspotIPAddress() {
int ipAddress = mContext.getSystemService(Context.WIFI_SERVICE)).getDhcpInfo().serverAddress;
if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
ipAddress = Integer.reverseBytes(ipAddress);
}
byte[] ipByteArray = BigInteger.valueOf(ipAddress).toByteArray();
String ipAddressString;
try {
ipAddressString = InetAddress.getByAddress(ipByteArray).getHostAddress();
} catch (UnknownHostException ex) {
ipAddressString = "";
}
return ipAddressString;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With