9

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.

Daniel Valencia
  • 677
  • 1
  • 8
  • 12

2 Answers2

9

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

Edward Brey
  • 40,302
  • 20
  • 199
  • 253
7

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();
David Jashi
  • 4,490
  • 1
  • 21
  • 26
  • I tried it on some devices and seems to work perfect, at least for what I needed. Come to think, it is quite logical. Thanks for your help. – Daniel Valencia Jun 06 '13 at 21:34
  • 2
    This will not work if you have a valid 3G connection but data connection turned off. – Manish Sep 08 '13 at 04:00
  • Well, maybe I don't understand your point, but what I needed was to detect if the device has or not cellular data capabilities and, depending of that, start one or other activity. I tried it in the situation that you describes here and seems to work without a problem. – Daniel Valencia Oct 11 '13 at 17:46
  • @Manish As far as I understand Daniel's needs, for him 3G capable device with data connection turned off is the same, as if if had no 3G capabilities at all. I my application it is the same too. – David Jashi Oct 14 '13 at 09:01