Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the current speed of internet (mobile & Wifi) using Android

I have an app that has to work in offline and online mode. Application has to make requests to the app server based on the internet speed of the current network connection (Wifi or Data).

If Wifi is connected, I can get the Wifi signal strength of the current connection using this code.

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), 5);

OR

wifiInfo.getLinkSpeed();

If the mobile internet is connected, I was able to get the singal strength using the listener class

@Override
    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        super.onSignalStrengthsChanged(signalStrength);
        int obj  = signalStrength.getGsmSignalStrength();
        int dBm = (2 * obj) -113;
 }

The above codes gets the signal strength, not the speed. So, how do I get the current speed of the connected network?

Normally, the speed test apps found in the stores or websites check the speed by sending or downloading a specific file or pinging a remote server and then calculating the speed using the time elapsed.

This upload/download or pinging feature is not supported by the app server with which I am communicating, so I am not able to use that.

So, is there any alternative way to check the speed of the current internet connection that can be done real-time?

Any leads could be helpful.

PS: This link has the code for checking the connectivity type of the current network. But there are cases in which I have a LTE signal with full strength but no/very slow internet connection. So, this too, wont be an accurate solution for me.

https://gist.github.com/emil2k/5130324

like image 907
user198530 Avatar asked Feb 22 '16 06:02

user198530


People also ask

How can I check my SIM internet speed?

TestMy. net's speed test database stores information on millions of Internet connections. This tool can average connection speed for any Internet provider, country or city in the world. So you can easily average speed test results, compare maximum speeds and research logged results for SIM INTERNET.

How to check the internet connection speed on your mobile?

Developed by Frederik Lipfert, Speedtest.pro Speed Test & WiFi Finder allows you to check the Internet connection speed on your mobile. In addition to this, the app can also detect the available open wireless networks in the nearby areas. Some other features that the app offers include:

How to improve mobile internet connection?

So keep taking mobile speed tests regularly and performing these handy operations to ensure that you enjoy uninterrupted connection. Mobile Speed Test is a website dedicated to performing mobile speed tests for the various network connections. It has been offering free mobile speed test.

What is an internet speed test?

An internet speed test measures the connection speed and quality of your connected device to the internet. It does so by running multiple consecutive tests that analyze different aspects of your internet connection, namely ping (latency), download speed , and upload speed .

What is a good internet speed?

A good internet speed is at least 25 Mbps download and 3 Mbps upload. These internet speeds are the bare minimum for a broadband connection as defined by the Federal Communications Commission (FCC).


2 Answers

You can not get Download/Upload speed without pinging any server. As Your server doesn't support ping you can use third party pinging site.

With JSpeedTest library you can do it easily. Some of your desired functionalities are available in this library. Such as

  • speed test download
  • speed test upload
  • download / upload progress monitoring
  • configurable hostname / port / uri (username & password for FTP)
  • configurable socket timeout and chunk size
  • configure upload file storage

Gradle:

compile 'fr.bmartel:jspeedtest:1.32.1'

Example Code:

SpeedTestSocket speedTestSocket = new SpeedTestSocket();

// add a listener to wait for speedtest completion and progress
speedTestSocket.addSpeedTestListener(new ISpeedTestListener() {

    @Override
    public void onCompletion(SpeedTestReport report) {
        // called when download/upload is complete
        System.out.println("[COMPLETED] rate in octet/s : " + report.getTransferRateOctet());
        System.out.println("[COMPLETED] rate in bit/s   : " + report.getTransferRateBit());
    }

    @Override
    public void onError(SpeedTestError speedTestError, String errorMessage) {
         // called when a download/upload error occur
    }

    @Override
    public void onProgress(float percent, SpeedTestReport report) {
        // called to notify download/upload progress
        System.out.println("[PROGRESS] progress : " + percent + "%");
        System.out.println("[PROGRESS] rate in octet/s : " + report.getTransferRateOctet());
        System.out.println("[PROGRESS] rate in bit/s   : " + report.getTransferRateBit());
    }
});
like image 121
Anisuzzaman Babla Avatar answered Sep 19 '22 03:09

Anisuzzaman Babla


You can do it simply just add this class named TrafficUtils to your project and call the following method -

TrafficUtils.getNetworkSpeed()

But first of all, add the two following permissions on your AndroidManifest.xml -

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"
like image 45
Gk Mohammad Emon Avatar answered Sep 19 '22 03:09

Gk Mohammad Emon