22

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.

chema989
  • 3,962
  • 2
  • 20
  • 33
Asus gates
  • 380
  • 1
  • 5
  • 20
  • 1
    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 Nov 18 '16 at 10:22

5 Answers5

16

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)

techfly
  • 1,826
  • 3
  • 25
  • 31
  • I am not sure, if this is entirely true. On one hand, there are several situations, in which the phone communicates on both interfaces: - The Wifi is in Hotspot mode, sharing the mobile conntection. - The Wifi does not offer internet access. Modern phones route local and internet requests differently in that case. But even when I am connected to a proper Wifi, I can get an IP address for both interfaces. I doubt that you can make a request that would be routed through the mobile network in this situation, but it is definitively connected and has a valid IP (tried on Nexus 5x and Pixel 3). – Sebastian Staacks Nov 05 '20 at 12:07
10

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

Community
  • 1
  • 1
sushildlh
  • 8,986
  • 4
  • 33
  • 77
  • To get cellular ip address reliably, you need to make sure that the interface's name begins with 'rmnet'. – Lywx Oct 14 '19 at 12:53
  • Also you need to be sure that interface address is not a link local address by using isLinkLocalAddress(). – Lywx Oct 14 '19 at 13:09
3
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
Jamil Hasnine Tamim
  • 4,389
  • 27
  • 43
3

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!

Shreshth Kharbanda
  • 1,777
  • 14
  • 24
1

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.

Raphau
  • 116
  • 5