Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the IP of the wifi hotspot in Android?

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:

  • Filtering by the wifi iface display name: it's not a good approach because the display name changes from one device to another (wlan0, eth0, wl0.1, etc).
  • Filtering by its mac address: almost work, but on some devices the hotspot iface does not have a MAC address ( iface.getHardwareAddress() returns null)...so not a valid solution.

Any suggestions?

like image 827
sirlion Avatar asked Mar 05 '12 20:03

sirlion


People also ask

How do I find my hotspot IP address Android?

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.

How do I find my mobile hotspot IP address?

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.

Does mobile hotspot show IP address?

Mobile hotspot does not provide IP address.

What is my IP hotspot?

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.


2 Answers

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

like image 93
ajma Avatar answered Oct 14 '22 03:10

ajma


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;

}
like image 22
navid_gh Avatar answered Oct 14 '22 03:10

navid_gh