12

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).

halfer
  • 19,824
  • 17
  • 99
  • 186
jennifer
  • 8,133
  • 22
  • 69
  • 96

3 Answers3

17

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);
Camille R
  • 1,433
  • 2
  • 17
  • 29
  • i need to find the connection speed programmatically in Android – jennifer Nov 22 '11 at 11:15
  • 8
    BTW this is also programmatically – ingsaurabh Nov 22 '11 at 11:21
  • 1
    The biggest your file is, the more precise the test is going to be, but obviously the longest it will be. In my opinion, a good compromise would be 50KB. – Camille R Nov 22 '11 at 12:37
  • Also if time taken is 0 seconds then what will be bandwidth, we are dividing by zero. – User7723337 Mar 20 '13 at 08:34
  • 1
    Well... I don't think a download can take 0 ms but yes that would be wise to check – Camille R Mar 26 '13 at 20:05
  • @CamilleR do you know a way to calculate this while the file is being downloaded? Is there a way to see how much the download speed is? – El Mac May 04 '15 at 11:46
  • 1
    @Elmac : You can check http://stackoverflow.com/questions/22932821/httpclient-post-with-progress-and-multipartentitybuilder to get the progress of the download; and see how long it took to download this %/size to know the current download speed – Camille R May 05 '15 at 12:41
7

This will Returns the current link speed in LINK_SPEED_UNITS.

but this work for WIFI Only

WifiManager wifiManager = Context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo != null) {
    Integer linkSpeed = wifiInfo.getLinkSpeed(); //measured using WifiInfo.LINK_SPEED_UNITS
}
MorningGlory
  • 758
  • 1
  • 11
  • 21
0

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

Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159