Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if an android device has mobile data capability

Tags:

android

tablet

I want to detect whether or not a device has mobile data capability. By mobile data capability I don't mean an active or connected mobile data connection, just the ability to use mobile data.

I currently use the following

if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
    //I assume device has mobile data capability
}

This works fine for most cases but one - If the device does not have calling facility but has the capability to utilize a cellular network for data connection only. Such devices are typically tablets which have a SIM card slot but it can be used only for data connection, not calling.

How can I detect whether the device has mobile data capability in this case and in all other cases? What is the best method?

like image 832
A.J. Avatar asked Mar 17 '23 14:03

A.J.


1 Answers

I found the solution myself. Posting it for others who might need it.

ConnectivityManager cm = (ConnectivityManager) 
        getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm
        .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if (ni == null) {
    // Device does not have mobile data capability
}

From the documentation of getNetworkInfo(int networkType):

Parameters: networkType integer specifying which networkType in which you're interested.

Returns: a NetworkInfo object for the requested network type or null if the type is not supported by the device. This method requires the caller to hold the permission android.Manifest.permission.ACCESS_NETWORK_STATE.

This method can be extended to check for other types of network as well. Just put the required networkType in getNetworkInfo().

Personally, I perform this test only if getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY) returns false. This check can then confirm whether or not device has mobile data capability even though it does not have telephony feature (which is the case with some tablets). If device has FEATURE_TELEPHONY, I assume it has mobile data capability. This way we can possibly reduce execution time in most cases.

like image 63
A.J. Avatar answered Apr 01 '23 07:04

A.J.