Android – How to get IP address of cellular network when device is connected to WiFi in Android

androidip addressnetworking

Is there a way through which I can get IP address of both WiFi and cellular network in Android simultaneously.I tried using many examples but was able to get Address of only WiFi network and not cellular network.I have enabled both WiFi and cellular network and device is having Internet access through WiFi.

Here is the code which I am using to get the IP address:

    String ipAddress = null;
    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()) {
                    ipAddress = inetAddress.getHostAddress().toString();
                    Log.i("Here is the Address",ipAddress);
                }
            }
        }
    } catch (SocketException ex) {

    }

Is it possible to get IP address of cellular network when device is connected to WiFi.If yes how is that feasible.

Best Answer

Whenever you enable WiFi on your device AND have an active connection to a WiFi network, your mobile data is temporarily disabled, no matter if you have enabled it manually or not. The setting "Mobile data on/off" is only taken into consideration if you have no active WiFi connection.

Some custom ROMs have an option to keep the mobile connection alive when you connect to a WiFi (so in case you lose your WiFi connection, it switches to mobile faster), but still, the WiFi connection is used.

Conclusion: You cannot get both IP addresses as you cannot have both WiFi and mobile network on (and if you can, you only use WiFi actively)