Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 need to check the bandwidth rate. Above a particular bandwidth only I need to make visible an option for a call (to initiate a call).

I need to find the connection speed programmatically (connection speed for Mobile Data Link, EDGE).

like image 726
jennifer Avatar asked Nov 22 '11 11:11

jennifer


1 Answers

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 image
long startTime = System.currentTimeMillis();
HttpGet httpRequest = new HttpGet(new URL(urlString).toURI());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
long endTime = System.currentTimeMillis();

HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity;
bufHttpEntity = new BufferedHttpEntity(entity);

//You can re-check the size of your file
final long contentLength = bufHttpEntity.getContentLength();

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

// Bandwidth : size(KB)/time(s)
float bandwidth = contentLength / ((endTime-startTime) *1000);
like image 176
Camille R Avatar answered Nov 15 '22 17:11

Camille R