Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 420
Asus gates Avatar asked Nov 18 '16 05:11

Asus gates


People also ask

How do I find the IP address of a device connected to my WiFi?

Find the page in your router web interface that lists the devices connected to your router. For NETGEAR routers, click BASIC > Attached Devices. Some models might use a slightly different menu label, like Device Manager. A Device Name and IP Address displays for each connected device.

Is there an IP address for cellular data?

Yes the WiFi and cellular connection have separate IP addresses. You can check your IP address in settings > about phone. You can also check your public IP address by going to a website, e.g. https://www.whatismyip.com/.


1 Answers

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)

like image 88
techfly Avatar answered Oct 04 '22 04:10

techfly