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

2

21

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

Cembalo answered 22/2, 2016 at 6:56 Comment(5)
https://mcmap.net/q/661327/-how-to-get-link-speed-programmatically May be help youFecteau
The reason people use pinging is because there is no other way to be accurate. A link may support up to 100 Mbps, but you may only be able to get 2 due to congestion. The only way to know that is to calculate. It also isn't helpful to know that your link supports 100Mbps to foo.com, if you want to hit bar.com which is congested and only able to support 10. Any other method will be an approximation.Wilmington
found a useful medium article. have a look android.jlelse.eu/…Dicho
Did you try this sample : github.com/bertrandmartel/speed-test-libYulan
@Ankit Mehta No. I haven't tried the sample. Without any pings to any server, in my understanding, there is no way to check for connectivity speed.Cembalo
P
5

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());
    }
});
Peak answered 10/7, 2018 at 9:27 Comment(2)
I want to check internet speed and set image or video url accordingly for example if current internet speed is between 50kbps - 150 kbps link1 , 150kbps - 500kbps link2 and >500kbps link3. so How to achieve that?Supra
This library does not work on vpn , I tried that speedTestSocket.setProxyServer("216.56.48.118:9000"); , it is throwing error INVALID_HTTP_RESPONSE : Error occurred while parsing http frame and INVALID_HTTP_RESPONSE : Error status code -1Changteh
C
3

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"
Coitus answered 20/1, 2021 at 6:48 Comment(1)
This class uses TrafficStats.getMobileRxBytes which returns how much data has been received across mobile networks in total, then waits a second to call it again to find a difference between the two numbers. This is not an accurate representation of the current speed of the user's network. If the user's device is currently not doing much networking, then this number will be lower than if a user was downloading something in the background while this method was called even while they are in the same network.Tortoiseshell

© 2022 - 2024 — McMap. All rights reserved.