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

Daniel Gonzáles picture Daniel Gonzáles · Jun 6, 2013 · Viewed 7.3k times · Source

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.

Answer

Edward Brey picture Edward Brey · Dec 2, 2014

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()).