If I start the application over Wi-Fi and I switch to 3G, it prints 0, 15, so it's mobile network but I don't know why that subtype means. Then I switch back to Wi-Fi and it prints 0, 3 for a second (Mobile network, NETWORK_TYPE_UMTS) and displays that I'm connected to a mobile network on my UI, finaly it connects to Wi-Fi and it prints 1, 0. So what does 15 means? Docs doesn't say what are these values.
if (networkInfo != null && networkInfo.isConnected()) {
int netType = networkInfo.getType();
int netSubtype = networkInfo.getSubtype();
Log.d("Receiver", String.valueOf(netType));
Log.d("Receiver", String.valueOf(netSubtype));
if (netType == ConnectivityManager.TYPE_WIFI) {
Log.i("Receiver", "WiFi");
} else if (netType == ConnectivityManager.TYPE_MOBILE
&& netSubtype == TelephonyManager.NETWORK_TYPE_UMTS
&& !telephonyManager.isNetworkRoaming()) {
Log.i("Receiver", "Mobile");
}
}
http://developer.android.com/reference/android/net/NetworkInfo.html#getSubtype()
So what does 15 means?
It means how fast your internet connection through the mobile connection is, so constant 15 is actually HSPA+
HSPA+ (also called Evolved HSPA or 4G) is a further evolution of
HSPA that offers data speeds of up to 42 Mbps.
you can go to this post
sample code:
if(type==ConnectivityManager.TYPE_WIFI){
return true;
}else if(type==ConnectivityManager.TYPE_MOBILE){
switch(subType){
case TelephonyManager.NETWORK_TYPE_1xRTT:
return false; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_CDMA:
return false; // ~ 14-64 kbps
case TelephonyManager.NETWORK_TYPE_EDGE:
return false; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return true; // ~ 400-1000 kbps
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return true; // ~ 600-1400 kbps
case TelephonyManager.NETWORK_TYPE_GPRS:
return false; // ~ 100 kbps
case TelephonyManager.NETWORK_TYPE_HSDPA:
return true; // ~ 2-14 Mbps
case TelephonyManager.NETWORK_TYPE_HSPA:
return true; // ~ 700-1700 kbps
case TelephonyManager.NETWORK_TYPE_HSUPA:
return true; // ~ 1-23 Mbps
case TelephonyManager.NETWORK_TYPE_UMTS:
return true; // ~ 400-7000 kbps
/*
* Above API level 7, make sure to set android:targetSdkVersion
* to appropriate level to use these
*/
case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
return true; // ~ 1-2 Mbps
case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
return true; // ~ 5 Mbps
case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
return true; // ~ 10-20 Mbps
case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
return false; // ~25 kbps
case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
return true; // ~ 10+ Mbps
// Unknown
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
default:
return false;
}
Have a look at the NETWORK_TYPE_HSPAP it has a connection speed of 10-20 Mbps
There's an accompanying method called getSubtypeName() which will return a human-readable value describing the subtype.
EDIT: Digging through the source, it seems that it will be one of the TelephonyManager.NETWORK_TYPE_* constants (e.g. NETWORK_TYPE_GPRS)
Related
I am trying to download some file in android app. For this I have to compare android default settings download via WiFi or WiFi/mobile-data status. Is it possible to get this default connection status in android application.
Expected: Settings->securitysettings->security policy update->Download updates via->Wi-Fi only and WiFi or mobile networks.
Here is it possible to get this network selection status in android application.
You Can check which connection App is using as of now
using this appraoch
static String getNetworkType(Context applicationContext) {
ConnectivityManager cm = (ConnectivityManager) applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = null;
if (cm != null) {
info = cm.getActiveNetworkInfo();
}
if (info == null || !info.isConnected())
return "-"; // not connected
if (info.getType() == ConnectivityManager.TYPE_WIFI)
return "WIFI";
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
int networkType = info.getSubtype();
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_GPRS:
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN: // api< 8: replace by 11
case TelephonyManager.NETWORK_TYPE_GSM: // api<25: replace by 16
return "2G";
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_EVDO_B: // api< 9: replace by 12
case TelephonyManager.NETWORK_TYPE_EHRPD: // api<11: replace by 14
case TelephonyManager.NETWORK_TYPE_HSPAP: // api<13: replace by 15
case TelephonyManager.NETWORK_TYPE_TD_SCDMA: // api<25: replace by 17
return "3G";
case TelephonyManager.NETWORK_TYPE_LTE: // api<11: replace by 13
case TelephonyManager.NETWORK_TYPE_IWLAN: // api<25: replace by 18
case 19: // LTE_CA
return "4G";
default:
return "Unknown";
}
}
return "Unknown";
}
My application completely depends on the network so I have to check whether the data communication is there between the application and my server and make a popup on communication lost and if not connected to Wifi or Mobile Data and if the connected service is having slow connection.
I am using the following code where I can get whether the device is connected to the Wifi or mobile data and get the type of connection and also got the code to get the type of mobile network connected which is not working at all.
please refer the following code...
public void CheckInternet()
{
Plugin.Connectivity.CrossConnectivity.Current.ConnectivityChanged += delegate
{
if (!(Plugin.Connectivity.CrossConnectivity.Current.IsConnected))
{
StartActivity(typeof(InternetCheckingView));
}
if (Plugin.Connectivity.CrossConnectivity.Current.IsConnected)
{
ConnectivityManager connectivityManager = (ConnectivityManager)GetSystemService(ConnectivityService);
NetworkInfo networkInf = connectivityManager.ActiveNetworkInfo;
bool isOnline = networkInf.IsConnected;
NetworkInfo networkInfo = connectivityManager.ActiveNetworkInfo;
bool isWifi = networkInfo.Type == ConnectivityType.Wifi;
bool isdata = networkInfo.Type == ConnectivityType.Mobile;
if (!isOnline)
{ StartActivity(typeof(InternetCheckingView)); }
else if(isWifi)
{
Toast.MakeText(_context, "Wifi Connected", ToastLength.Short).Show();
}
else if (isdata)
{
Toast.MakeText(_context, "Mobile Data Connected", ToastLength.Short).Show();
bool datatype = IsConnectionFast(subType);
if(!(datatype))
{StartActivity(typeof(InternetCheckingView));}
}
}
};
}
public bool IsConnectionFast(object networkType)
{
switch (subType)
{
//case TelephonyManager.NETWORK_TYPE_1xRTT:
case NetworkType.OneXrtt:
return false; // ~ 50-100 kbps//case TelephonyManager.NETWORK_TYPE_CDMA:
case NetworkType.Cdma:
return false; // ~ 14-64 kbps//case TelephonyManager.NETWORK_TYPE_EDGE:
case NetworkType.Edge:
return false; // ~ 50-100 kbps//case TelephonyManager.NETWORK_TYPE_EVDO_0:
case NetworkType.Evdo0:
return true; // ~ 400-1000 kbps //case TelephonyManager.NETWORK_TYPE_EVDO_A:
case NetworkType.EvdoA:
return true; // ~ 600-1400 kbps//case TelephonyManager.NETWORK_TYPE_GPRS:
case NetworkType.Gprs:
return false; // ~ 100 kbps//case TelephonyManager.NETWORK_TYPE_HSDPA:
case NetworkType.Hsdpa:
return true; // ~ 2-14 Mbps//case TelephonyManager.NETWORK_TYPE_HSPA:
case NetworkType.Hspa:
return true; // ~ 700-1700 kbps//case TelephonyManager.NETWORK_TYPE_HSUPA:
case NetworkType.Hsupa:
return true; // ~ 1-23 Mbps//case TelephonyManager.NETWORK_TYPE_UMTS:
case NetworkType.Umts:
return true; // ~ 400-7000 kbps
/*
* Above API level 7, make sure to set android:targetSdkVersion
* to appropriate level to use these
*/
//case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
case NetworkType.Ehrpd:
return true; // ~ 1-2 Mbps//case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
case NetworkType.EvdoB:
return true; // ~ 5 Mbps//case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
case NetworkType.Hspap:
return true; // ~ 10-20 Mbps//case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
case NetworkType.Iden:
return false; // ~25 kbps//case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
case NetworkType.Lte:
return true; // ~ 10+ Mbps// Unknown//case TelephonyManager.NETWORK_TYPE_UNKNOWN:
case NetworkType.Unknown:
return false;
default:
return false;
}
}
Here I would like to detect the slow network or not and if that is a slow network I have to call StartActivity(typeof(InternetCheckingView))
Can any one please help me to do that in I would like to open a popup whenever there is no data communication available like a onclicklisner in so that the even filers when ever there is a data loss even when the network is connected
(Added--)
I am checking for the data communication by pinging a url when the network connected change like disconnected or changes between wifi and mobile data but i would like to know the solution to get a popup to connect network whenever the data communication is lost even if it is connected to the network only when the app is opened.
I need to stop the Android app from syncing data to the server when it is using 2G network connection and allow it when it is using 3G/4G or WiFi connection, the data/WiFi identification is easy but how can I know if the phone is currently using 2G mode or 3G/4G mode?
Using TelephonyManager can identify the SIM mode but not the actual data carrier being used in real-time, since Android assigns E icon for 2G and H,H+for 3G then there must be a way to identify this. Any ideas?
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.
In my App, am using ScanResult to get the list of networks. How do I find the bandwidth of the networks (say is it 22MHz or 40MHz etc) ?
You can get the range of bandwidth by using the below functions but not the exact bandwidth.
public static void printBandwidthOfConnection(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
Log.i("Range",rangeOfConnection(info.getType(),info.getSubtype())));
}
/**
* Check if the connection is fast
* #param type
* #param subType
* #return string
*/
public static String rangeOfConnection(int type, int subType){
if(type==ConnectivityManager.TYPE_WIFI){
return "wifi";
}else if(type==ConnectivityManager.TYPE_MOBILE){
switch(subType){
case TelephonyManager.NETWORK_TYPE_1xRTT:
return "50-100 kbps";
case TelephonyManager.NETWORK_TYPE_CDMA:
return " 14-64 kbps";
case TelephonyManager.NETWORK_TYPE_EDGE:
return "50-100 kbps";
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return "400-1000 kbps";
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return "600-1400 kbps";
case TelephonyManager.NETWORK_TYPE_GPRS:
return "100 kbps";
case TelephonyManager.NETWORK_TYPE_HSDPA:
return "2-14 Mbps";
case TelephonyManager.NETWORK_TYPE_HSPA:
return "700-1700 kbps";
case TelephonyManager.NETWORK_TYPE_HSUPA:
return "1-23 Mbps"
case TelephonyManager.NETWORK_TYPE_UMTS:
return "400-7000 kbps";
/*
* Above API level 7, make sure to set android:targetSdkVersion
* to appropriate level to use these
*/
case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
return " 1-2 Mbps";
case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
return "5 Mbps";
case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
return "10-20 Mbps";
case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
return "25 kbps";
case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
return "10+ Mbps";
default:
return null;
}
}else{
return null;
}
}
I ran into the case that when I had 4G off and connected to certain wifi access point but without capability to send out or receive data, the flag used to check the connectivity of network was set to be true like below.
NetworkInfo ni = context.getActiveNetworkInfo();
boolean flag = ni.isConnected();
In this case, I should obviously drop the wifi and switch on my 4G or in other words turn to use my 4g instead of wifi network.
But How could I check the quality of wifi connectivity?
you can check the speed in the mobile network using this code,
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
public class Connectivity {
/*
* HACKISH: These constants aren't yet available in my API level (7), but I need to handle these cases if they come up, on newer versions
*/
public static final int NETWORK_TYPE_EHRPD=14; // Level 11
public static final int NETWORK_TYPE_EVDO_B=12; // Level 9
public static final int NETWORK_TYPE_HSPAP=15; // Level 13
public static final int NETWORK_TYPE_IDEN=11; // Level 8
public static final int NETWORK_TYPE_LTE=13; // Level 11
/**
* Check if there is any connectivity
* #param context
* #return
*/
public static boolean isConnected(Context context){
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
return (info != null && info.isConnected());
}
/**
* Check if there is fast connectivity
* #param context
* #return
*/
public static boolean isConnectedFast(Context context){
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
return (info != null && info.isConnected() && Connectivity.isConnectionFast(info.getType(),info.getSubtype()));
}
/**
* Check if the connection is fast
* #param type
* #param subType
* #return
*/
public static boolean isConnectionFast(int type, int subType){
if(type==ConnectivityManager.TYPE_WIFI){
System.out.println("CONNECTED VIA WIFI");
return true;
}else if(type==ConnectivityManager.TYPE_MOBILE){
switch(subType){
case TelephonyManager.NETWORK_TYPE_1xRTT:
return false; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_CDMA:
return false; // ~ 14-64 kbps
case TelephonyManager.NETWORK_TYPE_EDGE:
return false; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return true; // ~ 400-1000 kbps
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return true; // ~ 600-1400 kbps
case TelephonyManager.NETWORK_TYPE_GPRS:
return false; // ~ 100 kbps
case TelephonyManager.NETWORK_TYPE_HSDPA:
return true; // ~ 2-14 Mbps
case TelephonyManager.NETWORK_TYPE_HSPA:
return true; // ~ 700-1700 kbps
case TelephonyManager.NETWORK_TYPE_HSUPA:
return true; // ~ 1-23 Mbps
case TelephonyManager.NETWORK_TYPE_UMTS:
return true; // ~ 400-7000 kbps
// NOT AVAILABLE YET IN API LEVEL 7
case Connectivity.NETWORK_TYPE_EHRPD:
return true; // ~ 1-2 Mbps
case Connectivity.NETWORK_TYPE_EVDO_B:
return true; // ~ 5 Mbps
case Connectivity.NETWORK_TYPE_HSPAP:
return true; // ~ 10-20 Mbps
case Connectivity.NETWORK_TYPE_IDEN:
return false; // ~25 kbps
case Connectivity.NETWORK_TYPE_LTE:
return true; // ~ 10+ Mbps
// Unknown
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
return false;
default:
return false;
}
}else{
return false;
}
}
}
For anyone running into this question, here's an answer from Sep 8, 2015
https://github.com/facebook/network-connection-class
Network Connection Class
Network Connection Class is an Android library that allows you to
figure out the quality of the current user's internet connection. The
connection gets classified into several "Connection Classes" that make
it easy to develop against. The library does this by listening to the
existing internet traffic done by your app and notifying you when the
user's connection quality changes. Developers can then use this
Connection Class information and adjust the application's behaviour
(request lower quality images or video, throttle type-ahead, etc).
Network Connection Class currently only measures the user's downstream
bandwidth. Latency is also an important factor, but in our tests,
we've found that bandwidth is a good proxy for both.
The Network Connection Class library takes care of spikes using a
moving average of the incoming samples, and also applies some
hysteresis (both with a minimum number of samples and amount the
average has to cross a boundary before triggering a bucket change)