Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getAllNetworkInfo detect wifi or mobile connection, but never both

Tags:

android

I have a function which check network connections using:

ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo[] networkInfos = cm.getAllNetworkInfo();

but, as soon the Wifi network is connected, the mobile network is given as DISCONNECTED

How could I know if both networks are connected?

Thanks in advance for your time.

like image 512
Cyril Jacquart Avatar asked Jan 28 '26 20:01

Cyril Jacquart


2 Answers

Try this:

ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobileNet = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(netInfo!=null){
  if(mobileNet.isConnected()){
    System.out.println("Mobile Network is connected");

  }
  if(wifi.isConnected()){
    System.out.println("Wi-fi Network is connected");
  }

}

How could I know if both networks are connected?

You can create for sure method that will check for both types e.q.

if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
   // do your stuff
}
if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
   // do your stuff
}

But in Android OS, if Wifi and Mobile network is available (and you can connect to), OS will choose Wifi by default so it'll use Wifi as active network connection.

like image 43
Simon Dorociak Avatar answered Jan 31 '26 09:01

Simon Dorociak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!