2

can i get low or high wifi connectivity. I mean can i measure the signal level from 1 to 5.(assume only one network). i used calculatesignallevel. but it either returns 0 or 1. kindly, help me out

user562237
  • 775
  • 5
  • 15
  • 24

2 Answers2

7

You need to import the following two classes

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

Then, you can measure the wifi signal strength like this:

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);            
WifiInfo info = wifi.getConnectionInfo();           
int wifiSignalStrength = WifiManager.calculateSignalLevel(info.getRssi(), 4);

//displaying the wifi signal level in a Toast   
Toast.makeText(getApplicationContext(), "signal strength: "+wifiSignalStrength, Toast.LENGTH_LONG).show(); 
Sourav
  • 1,214
  • 14
  • 24
  • thank u... but what does 4 in the calculatesignallevel indicate... because i want to set an image according to signal strength – user562237 Mar 11 '11 at 07:25
  • 3
    It indicates the number of levels to consider in the calculated level. See [here](http://developer.android.com/reference/android/net/wifi/WifiManager.html#calculateSignalLevel%28int,%20int%29) – Sourav Mar 11 '11 at 07:28
1

actually the code in the API is flawed; if you use 46 levels or more, it will always return 0. Read here for more info. I guess most people will want to use 101 for levels, to accurately calculate percentage.

It's fixed in 4.0.1 and up. I made this so you can use it on any platform version.

public int getWifiSignalStrength(Context context){
    int MIN_RSSI        = -100;
    int MAX_RSSI        = -55;  
    int levels          = 101;
    WifiManager wifi    = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);            
    WifiInfo info       = wifi.getConnectionInfo(); 
    int rssi            = info.getRssi();

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
        return WifiManager.calculateSignalLevel(info.getRssi(), levels);
    } else {             
        // this is the code since 4.0.1
        if (rssi <= MIN_RSSI) {
            return 0;
        } else if (rssi >= MAX_RSSI) {
            return levels - 1;
        } else {
            float inputRange = (MAX_RSSI - MIN_RSSI);
            float outputRange = (levels - 1);
            return (int)((float)(rssi - MIN_RSSI) * outputRange / inputRange);
        }
    }
}//end method
Community
  • 1
  • 1
slinden77
  • 3,378
  • 2
  • 37
  • 35