How to Determine If Network Type Is 2G, 3G or 4G

How detect networktype (2G/3G/LTE) when connected to WiFi

The issue was that in API-version 24 and forward getNetworkType() has been split in into two separate methods:

getDataNetworkType()

and

getVoiceNetworkType()

I added a check in my code to se if the returned type was IWLAN and if so use getVoiceNetworkType() instead.

if (tm.getNetworkType() != 18)
return tm.getNetworkType();
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)
return tm.getVoiceNetworkType();
else
return -1;

Any way to identify the data type being used (2G/3G) in Android?

Yes, there is.

On TelephonyManager you have some constants like TelephonyManager.NETWORK_TYPE_EDGE to check that. Use those constants along with the methods getType() and getSubtype() from NetworkInfo.

EDIT: I was being stupid. You can simply call NetworkInfo.getSubtypeName and you're good to go.

NetworkInfo info = Connectivity.getNetworkInfo(context);
Log.d("tag","Network type: " + info.getSubtypeName());

Or you could also try the other solution.


OLD SOLUTION

Try something like:

NetworkInfo info = Connectivity.getNetworkInfo(context);
getConnectionType(info.getType(),info.getSubtype());

And call this function:

private String getConnectionType(int type, int subType) {
if(type==ConnectivityManager.TYPE_WIFI){
return "WiFi";
}
else if(type==ConnectivityManager.TYPE_MOBILE){
switch(subType){
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_CDMA:
return "1G"; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_GPRS:
return "2G"; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
case TelephonyManager.NETWORK_TYPE_UMTS:
return "3G"; // ~ 2-14 Mbps
case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
return "4G"; // ~ 10+ Mbps
// Unknown
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
default:
return "Not defined";
}
}
else{
return "Not defined";
}
}

Of course, the method above is just a suggestion to show how it works, you can change it for your own purposes, and make it more complete, change the return type, etc.

Is there a way to detect which cellular network is used on Android?

I'm not aware of a way to query the hardware capabilities of the device (which radios are present), but if you're asking how to detect the current type of cellular data connection, look to TelephonyManager.getNetworkType(), which "returns a constant indicating the radio technology (network type) currently in use on the device for data transmission".

I would consider the values NETWORK_TYPE_LTE and NETWORK_TYPE_HSPAP to indicates a 4G connection. Since the line between 3G and 4G is blurry, and since the set of older technologies is effectively fixed (we aren't inventing new 2G networks), a better strategy may be identifying network technologies that are not sufficient and displaying a warning if the connection is using a known slow technology (e.g., EDGE).

Also keep in mind that network technology alone doesn't necessarily equate to a certain connection speed. Even a 4G connection can run at speeds that are insufficient for video streaming depending on many factors, some of which are external to the device (weather, signal strength, device battery level, bandwidth available at the cell tower, etc.)

Other caveats:

  • You should first check whether the active network connection is a cellular connection. To do this, get ConnectivityManager.getActiveNetworkInfo() and examine that object's getType(). This will indicate whether the active network is Wi-Fi or cellular. Keep in mind that there may be no active network (null will be returned).

  • You should also check ConnectivityManager.isActiveNetworkMetered() for a hint about whether the current network connection has a data restriction. If so, you should warn the user before performing data-intensive operations regardless of the connection type.



Related Topics



Leave a reply



Submit