A.J.
A.J.

Reputation: 1530

Detect if an android device has mobile data capability

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?

Upvotes: 4

Views: 3301

Answers (3)

A.J.
A.J.

Reputation: 1530

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.

Upvotes: 7

Kai
Kai

Reputation: 15476

Use ConnectivityManager and NetworkInfo to check the current state of connection:

    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo eventInfo = cm.getActiveNetworkInfo();

    //first check to see if an active connection exists
    if (eventInfo != null
            && eventInfo.getState() == NetworkInfo.State.CONNECTED) {
        //now checks for the type of connection
        switch(eventInfo.getType()) {
             case ConnectivityManager.TYPE_MOBILE:
             case ConnectivityManager.TYPE_MOBILE_DUN:
             case ConnectivityManager.TYPE_MOBILE_HIPRI:
             case ConnectivityManager.TYPE_MOBILE_MMS:
             case ConnectivityManager.TYPE_MOBILE_SUPL:
             //is mobile connection
             break;
        }
    } else {
         //device currently in a disconnected state, including connecting 
    }

*psudo code, may not 100% work on copy & paste

Upvotes: 0

Fred Grott
Fred Grott

Reputation: 3476

hmm I have not tested this yet but if it has a data connection then its location over network probably is enabled, thus you can use FEATURE_LOCATION_NETWORK

Upvotes: 0

Related Questions