1

I am able to check whether the device is connected to wifi with this.

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) activity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

However, i want to check the wifi connection speed something like Ping. I want to base on this ping number to set some variables. Something like this,

public int internetspeed(){
    checking...
    return speed;
}

Can someone give me tutorial or example?

Alan Lai
  • 1,094
  • 7
  • 18
  • 41

3 Answers3

3

This snippet will do the job for you

    WifiManager wifiManager = Context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo != null) {
        Integer linkSpeed = wifiInfo.getLinkSpeed(); //measured using WifiInfo.LINK_SPEED_UNITS
    }
K_Anas
  • 31,226
  • 9
  • 68
  • 81
0

Download file in background and measure data per second.

Yahor10
  • 2,123
  • 1
  • 13
  • 13
0

Here is a full class that I use to test connectivity . For the ping matter see my comment .

public class InternetCheck {

    Context mContext;
     public InternetCheck(Context mContext){
           this.mContext = mContext;
      }


    // CHECK FOR INTERNET METHOD
    public final boolean isInternetOn() {
        ConnectivityManager connec = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        // ARE WE CONNECTED TO THE NET
        if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
                || connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING
                || connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING
                || connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) {
            return true;
        } else if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
                || connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
            return false;
        }
        return false;
    }

}
moujib
  • 742
  • 2
  • 7
  • 26
  • The link that you provide is ping a url, but i don't know the device connect to the router ip address, so cannot ping also – Alan Lai May 10 '12 at 07:14