Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to detect if a device is WiFi or WiFi+Cellular

Is there a way to check if the user is using a device (this applies primarily to tablets) with Cellular conection?. That is, the smartphones comes with built-in Wi‑Fi and Cellular (generally), but some tablets only comes with Wi-Fi. How I can know what type of device is running my application?

I tried the following without results:

cell = ConnectivityManager.isNetworkTypeValid(ConnectivityManager.TYPE_MOBILE);
wifi = ConnectivityManager.isNetworkTypeValid(ConnectivityManager.TYPE_WIFI);

if (cell) tv_1.setText("The tablet has cellular");
   else tv_1.setText("The tablet does not have cellular");
if (wifi) tv_2.setText("The tablet has wifi");
   else tv_2.setText("The tablet does not have wifi");

The problem is that both comparisons always return true, even if it's a tablet that has no cellular.

I only need to know if the device has a SIM card slot (model with cellular) or it is a model that only has WiFi, is that possible?

Thanks in advance.

like image 810
Daniel Gonzáles Avatar asked Jun 06 '13 18:06

Daniel Gonzáles


People also ask

How do I know if my phone is using Wi-Fi or cellular?

On Android phones: Go to Settings. Tap Connections. Then, tap Data Usage.

How do I know if my tablet has cellular?

You can quickly tell if an iPad has cellular by looking at the top back of the iPad - if there's a thick black or white band like the one below you know it has cellular (that's where all the antennae go...)

What Network is my Android phone on?

Go to the Settings menu. Tap on 'Connections' or 'Network & Internet. ' Go to the 'Mobile Networks' section.


2 Answers

If the goal is to determine whether the connection is metered, you should call ConnectivityManager.isActiveNetworkMetered(), or if older device support is required, ConnectivityManagerCompat.isActiveNetworkMetered().

For background on reacting to varying connection types, see the Managing Network Usage (although be aware of that documentation's problem of not using isActiveNetworkMetered()).

like image 85
Edward Brey Avatar answered Oct 12 '22 20:10

Edward Brey


Here is excerpt from my code (it works so far):

ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mEthernet = connManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
NetworkInfo m3G = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mWifi!=null) isOnWifi = mWifi.isConnected();
if (mEthernet!=null) isOnEthernet = mEthernet.isConnected();
if (m3G!=null) is3G = m3G.isConnected();
like image 7
David Jashi Avatar answered Oct 12 '22 20:10

David Jashi