GrandMarquis
GrandMarquis

Reputation: 1913

Best way to evaluate connection speed

I'm developing an app which needs to get music file by streaming for playing live.

In the request song api I can specify the bandwith (eg: 50kbps, 100kbps, 300, 600 or 1 Mbps).

The more the bandwith is big, the more the file will get time to be fetched. As I don't want the users to be restricted about that I have multiple choices to deal with it:

I know that connection speed could vary a lot if user loose the wifi, or is using 3g moving in the street. And the thing is that I can't change the bandwidth when the song will be playing.

Maybe you have experience about that you would like to share?

Thank you!

Upvotes: 35

Views: 39506

Answers (4)

GrandMarquis
GrandMarquis

Reputation: 1913

Facebook released a library for this:

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

this wasn't existing in 2011..

Upvotes: 43

Sandeep Kumar Patil
Sandeep Kumar Patil

Reputation: 339

protected String doInBackground(String... urls) {
    String response = "";

    startTime = System.currentTimeMillis();
    for (String url : urls) {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {

            HttpResponse execute = client.execute(httpGet);
            InputStream content = execute.getEntity().getContent();


            BufferedReader buffer = new BufferedReader(
                    new InputStreamReader(content));
            String s = "";
            while ((s = buffer.readLine()) != null) {
                response += s;
            }
            endTime = System.currentTimeMillis();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return response;
}

@Override
protected void onPostExecute(String result) {
    // TODO Auto-generated method stub


    long dataSize = result.length() / 1024;
    takenTime = endTime - startTime;
    long s = takenTime / 1000;
    double speed = dataSize / s;

    Toast.makeText(context, "" + s + "kbps", Toast.LENGTH_SHORT).show();
}

Upvotes: 4

codeScriber
codeScriber

Reputation: 4612

why not try to change your view of things. Try to flow with your users. say your user wishes to download 128 kbit quality song. you start the download , WHILE downloading you make an average download time, take a few seconds for this average to stabilize, and if it's below certain value make a pop up to tell the user that his connection is too slow for the current bandwidth and ask him if to lessen the quality or to keep downloading slowly.

This will:

  1. let the users the option to always assume they can get the best quality media.
  2. let u do your check in runtime and change the quality accordingly while downloading without the need to pre check.
  3. keeps your app simple to users.

I know i'm not answering your specific requirement, i'm just offering a different view.

Upvotes: 5

atzu
atzu

Reputation: 424

Detect network connection type on Android

You can check all available options here: http://developer.android.com/reference/android/telephony/TelephonyManager.html

This can fix the mobile network type but can't help you with the Wifi speed, you should code it by downloading something from a server you know and calculate the time.

I hope it helps.

Upvotes: 2

Related Questions