21
votes

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.

5
I don't' think it's possible.Since IP address will be assigned for your IP channel, there could be only one channel active at a time WIFI/MobileData.Madhukar Hebbar

5 Answers

17
votes

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)

10
votes

try this may be it helpful .....

For Mobile IP Address.....

 public static String getMobileIPAddress() {
        try {
            List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface intf : interfaces) {
                List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
                for (InetAddress addr : addrs) {
                    if (!addr.isLoopbackAddress()) {
                        return  addr.getHostAddress();
                    }
                }
            }
        } catch (Exception ex) { } // for now eat exceptions
        return "";
    }

For Wifi IP Address....

  public String getWifiIPAddress() {
        WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
        int ip = wifiInfo.getIpAddress();
        return  Formatter.formatIpAddress(ip);
    }

include this permission into your menifest ....

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

use like this ....

String wifiIp = getWifiIPAddress(); 
String mobileIp = getMobileIPAddress();

you get Output like this ......

o

Possibally a dublicate of How to get IP address of the device

3
votes
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
3
votes

Use the following in your java code:

    WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
String ipAddress = Formatter.formatIpAddress(ip);

Don't forget to add this permission in your Android Manifest file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Source: Get Wifi IP Address

Hope it helps! Good Luck!

1
votes

You won't get IP for cellular connection when your wifi is enabled and connected. That's because system doesn't use cellular data connection for battery saving reasons. Altough you can enable both of them at the same time, the system will only use one at a time. It's like: both are allowed, but only one is used.

There is only one example that I can think of: it's when you are connected via wifi to network with no Internet access, then your phone will connect via cellular also.