How to get IP address of cellular network when device is connected to WiFi in Android
Asked Answered
N

6

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.

Nett answered 18/11, 2016 at 5:59 Comment(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.Wrangle
T
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)

Terranceterrane answered 22/11, 2016 at 13:19 Comment(1)
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).Marguerita
D
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

Discipline answered 22/11, 2016 at 13:50 Comment(2)
To get cellular ip address reliably, you need to make sure that the interface's name begins with 'rmnet'.Intrust
Also you need to be sure that interface address is not a link local address by using isLinkLocalAddress().Intrust
F
3
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
Footworn answered 28/11, 2016 at 7:10 Comment(0)
I
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!

Inapplicable answered 29/11, 2016 at 4:11 Comment(0)
S
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.

Skiba answered 22/11, 2016 at 14:7 Comment(0)
G
-1

On recent Pixels running stock Android 13, Developer options has a "Mobile data always active" toggle ("for fast network switching"), and consequently /system/bin/ifconfig [-S] reports both rmnet (cellular data) and wlan (wifi) addresses on rooted devices. Thus, when wifi is connected and in active use, the rmnet address is also alive, and you can successfully ssh from a remote device into the Pixel's rmnet address, either IPv4 or IPv6 depending on the APN (Access Point Name) protocols, without affecting the primary wifi connection. The trick, of course, is knowing what that rmnet address is, particularly in the case of an ever-changing IPv6 address ... the phone must report it to the remote device in some manner.

Gracye answered 8/10, 2023 at 17:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.