Reputation: 2407
In application, I need to find out current (maximum) bandwidth of current network connection (WLAN/3G/4G).
How can this be done?
Uploading small file, like 20kb or so would not be an option, since, I might get network speed that is not the same for 5MB, 20MB or 50MB files since effective network upload/download speed depends on file size.
Uploading a large file is another option, but how big would it need to be and how often do I need to do the check for the current maximum bandwidth?
I am guessing this is not something already available somewhere about current connection from the system (android).
My idea is to have this information available for the app while it is being used, which can run in the background too.
Upvotes: 0
Views: 1700
Reputation: 3886
You can use ConnectivityManager to get the networking information. Firstly here, you can check if the user is connected to Wifi. If they are on mobile radio, you can grab the type of network they are connected to with the following:
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if( activeNetwork.getType() != ConnectivityManager.TYPE_WIFI)
{
String typeName=activeNetwork.getSubtypeName()
int type = activeNetwork.getSubtype ()
switch(type)
{
// values are of type TelephonyManager.NETWORK_TYPE_* enums
}
}
A handy-lookup table then lets you know what kind of network you’re dealing with, and what to expect as the best possible bandwidths and latency.
But in truth, that value gives you the BEST CASE scenario; in the wild, you’ll get worse results. See even though your user might be on an LTE network, the user might be over their data-cap for the month, and be getting throttled connectivity as a result.
This is why I like to always add a timing metric to known asset sizes. So, if you're fetching 128x128 thumbnails, you know (roughly) how many bytes in size it is, and with the above information, you can guess at it's expected latency to fetch. You can time the real fetch time, however, and adjust your metrics accordingly.
Getting networking right has more to do with writing code that's adaptive to network conditions, than mot anything else.
Upvotes: 2
Reputation: 21
There is a library from Facebook called network-connection-class .
It provides DeviceBandwidthSampler
which you could use to perform sampling. It also provides ConnectionClassManager
to inform you the bandwidth quality (after sampling).
The bandwidth quality information is define in Connection Quality
.
Upvotes: 2