Skip to content Skip to sidebar Skip to footer

Check The Bandwidth Rate In Android

We have an option to check the network connection types in Android (whether it is 3G, edge or gprs). I need to check the the bandwidth rate. I need to initiate a call. For that I n

Solution 1:

You can download a known-size file from your server, and calculate how long did it take to download it. Then you have your bandwidth. Simple but works :)

Sample, not tested :

//Download your imagelongstartTime= System.currentTimeMillis();
HttpGethttpRequest=newHttpGet(newURL(urlString).toURI());
HttpClienthttpClient=newDefaultHttpClient();
HttpResponseresponse= (HttpResponse) httpClient.execute(httpRequest);
longendTime= System.currentTimeMillis();

HttpEntityentity= response.getEntity();
BufferedHttpEntity bufHttpEntity;
bufHttpEntity = newBufferedHttpEntity(entity);

//You can re-check the size of your filefinallongcontentLength= bufHttpEntity.getContentLength();

// Log
Log.d(TAG, "[BENCHMARK] Dowload time :"+(endTime-startTime)+" ms");

// Bandwidth : size(KB)/time(s)floatbandwidth= contentLength / ((endTime-startTime) *1000);

Solution 2:

This will Returns the current link speed in LINK_SPEED_UNITS.

but this work for WIFI Only

WifiManagerwifiManager= Context.getSystemService(Context.WIFI_SERVICE);
WifiInfowifiInfo= wifiManager.getConnectionInfo();
if (wifiInfo != null) {
    IntegerlinkSpeed= wifiInfo.getLinkSpeed(); //measured using WifiInfo.LINK_SPEED_UNITS
}

Solution 3:

Try facebook network library class (library size = 16 KB)

We can fetch manually / network change listener also available.

Manual fetch code:

ConnectionQuality cq = ConnectionClassManager.getInstance().getCurrentBandwidthQuality();

Github link - https://github.com/facebook/network-connection-class

Post a Comment for "Check The Bandwidth Rate In Android"