user198530
user198530

Reputation: 361

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

Upvotes: 21

Views: 22158

Answers (2)

Gk Mohammad Emon
Gk Mohammad Emon

Reputation: 6938

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"

Upvotes: 3

Anisuzzaman Babla
Anisuzzaman Babla

Reputation: 7480

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());
    }
});

Upvotes: 5

Related Questions