Question

I need to find the device's ip address when it's hosting a hotspot. I've used this code so far :

//if is using Hotspot
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)) {
                return inetAddress.getHostAddress();
            }
        }
    }
}

This works quite fine but the wifi NetworkInterface name differs on some devices. So I have to find the device's wifi NetworkInterface name (for its hotspot) first. How can I find this name? Or is there a better approach to find the device's ip address?

/// Finding the right ip address by the MAC also doesn't seem to work

Was it helpful?

Solution 2

I recently figured out that the WifiAP ip address is hardcoded in Android. Unless a user has changed this value manually (I think that is very uncommon) using the hardcoded value is absolutely sufficient. I think this is the best way to go. The IP address is "192.168.43.1" : https://github.com/CyanogenMod/android_frameworks_base/blob/cm-10.1/wifi/java/android/net/wifi/WifiStateMachine.java?source=c#L1299

OTHER TIPS

At first I tried to fetch the MAC address of the WiFi interface to compare it with the MAC address of each interface. But it turns out, that at least on my N4 running CM the MAC of the WiFi interface changes when turning on the Hotspot.

So I wrote some code to walk though the list of devices to find something to identify the wifi interface. This code works perfectly on my N4:

private String getWifiIp() throws SocketException {
    for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
            en.hasMoreElements(); ) {
        NetworkInterface intf = en.nextElement();
        if (intf.isLoopback()) {
            continue;
        }
        if (intf.isVirtual()) {
            continue;
        }
        if (!intf.isUp()) {
            continue;
        }
        if (intf.isPointToPoint()) {
            continue;
        }
        if (intf.getHardwareAddress() == null) {
            continue;
        }
        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
                enumIpAddr.hasMoreElements(); ) {
            InetAddress inetAddress = enumIpAddr.nextElement();
            if (inetAddress.getAddress().length == 4) {
                return inetAddress.getHostAddress();
            }
        }
    }
    return null;
}

Only one single interface matches all conditions: wlan0.

Possible other solution:

Walk trough some most common interface names and try to find them in the list: new String[] { "wlan0", "eth0", ...];

This can help you out.

Making a call to the shell to ask for the network adapters and check the ones you need like in this case is wlan so follow this code

Process p=Runtime.getRuntime().exec("ip link show | grep -o \": wlan[0-9]:\" ");

BufferedReader readCommandOutput = 
    new BufferedReader(new InputStreamReader(p.getInputStream()));

String line=null;
while((line=readCommandOutput.readLine())!=null){
    // Lines containing wlan will be returned
    // parse to get the name of that interface.
    String interface=line.split(":")[1];
    //this is the interface you need.
}

if (line==null){
    //nothing was found.
}

readCommandOutput.close();

Attached notes

This image is the output of the command on the shell. Or you can use the Android Terminal Emulator from the play store in order to run this command in android shell.

For retrieving the ip address

WifiManager wifi=(WifiManager)getSystemService(WIFI_SERVICE);
int address=wifi.getDhcpInfo().ipAddress;
Toast.makeText(this,intToIP(address),1).show();         


public String intToIP(int i) {
    return ((i & 0xFF) 
            + "." + ((i >> 8) & 0xFF)
            + "." + ((i >> 16) & 0xFF)
            + "." + ((i >> 24) & 0xFF));
 }

this solution works when you want to get ip of the hotspot device on the same device but dont know how to get it on connected device (mostly the ip of server is 192.168.43.1 you can get the work done by hardcoding it)

 public void serverip()

    {
     DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
            if (dhcpInfo==null){
                Toast.makeText(MainActivity.this, "No ip for server", Toast.LENGTH_SHORT).show();
            }else{

                Toast.makeText(MainActivity.this, "ip of server "+android.text.format.Formatter.formatIpAddress(dhcpInfo.gateway), Toast.LENGTH_SHORT).show();
    }


            }

Please replace intf.getName() with intf.getDisplayName() and then try.

Once Try this 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();
                    if (!inetAddress.isLoopbackAddress()) {

                        String ip = Formatter.formatIpAddress(inetAddress.hashCode());
                        Toast.makeText(getApplicationContext(), "***** IP="+ ip, 1).show();

                        return ip;
                    }
                }
            }
        } catch (SocketException ex) {
            Toast.makeText(getApplicationContext(), "***** IP="+ex.toString(), 1).show();

        }
        return null;
    }

Write code at oncreat to check ,hotspot is Enable are not.....

WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); 
        wifiManager.setWifiEnabled(true);
        wifiManager.setWifiEnabled(false);
        boolean wifiEnabled = wifiManager.isWifiEnabled();
        if (wifiEnabled) {
            Toast.makeText(getApplicationContext(),"on",Toast.LENGTH_SHORT).show();
            getLocalIpAddress();
        }else {
                Toast.makeText(getApplicationContext(),"off",Toast.LENGTH_SHORT).show();
        }

user2224350 said : "I recently figured out that the WifiAP ip address is hardcoded in Android."

Unless a user has changed this value manually (I think that is very uncommon) using the hardcoded value is absolutely sufficient. I think this is the best way to go. The IP address is "192.168.43.1", as seen here.

This works on my Samsung A3, and Huawei PRA-LX1, but the HTC Desires 530 returns 192.168.1.1 as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top