Issue with WifiManager.calculateSignalLevel(RSSI, 5)

扶醉桌前 提交于 2019-12-18 12:44:07

问题


I am trying to use the Wifimanager to calculate the Signal Level of the access points found during a scan.

I am using the following method:

WifiManager.calculateSignalLevel(int, int)

But it appears to always return the same int no matter what the RSSI level is.

Here is my code:


public int calculateQoS(int aRSSI){

    signalLevel = WifiManager.calculateSignalLevel(RSSI, 5);

    return signalLevel;

}

public void testCalculateQoS(){

            Log.d("signal", "signal = : "
                    + connMonitor.calculateQoS(-44)
                    + " " + connMonitor.calculateQoS(-80)
                    + " " + connMonitor.calculateQoS(-120)
                    + " " + connMonitor.calculateQoS(-20));

        }

The logging outputs 1 for all the test cases for calculateQoS(int).

Am I missing something simple here? Why is the SignalLevel always 1?


回答1:


It seems that calculateSignalLevel is implemented this way:

public static int calculateSignalLevel(int rssi, int numLevels) {
  if (rssi <= MIN_RSSI) {
      return 0;
  } else if (rssi >= MAX_RSSI) {
      return numLevels - 1;
  } else {
      int partitionSize = (MAX_RSSI - MIN_RSSI) / (numLevels - 1);
      return (rssi - MIN_RSSI) / partitionSize;
  }
}

Maybe this code snippet can explain your problem. Also note:

http://code.google.com/p/android/issues/detail?id=2555




回答2:


thanks to this question I could prevent problem on lower API versions then the one I'm targetting. So 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



回答3:


This issue is only in android 2.3,
you can replace it with latest code of WiFiManger of an android 4.2

Here is the code:

public int calculateSignalLevel(int rssi, int numLevels) {
    if(rssi <= MIN_RSSI) {
        return 0;
    } else if(rssi >= MAX_RSSI) {
        return numLevels - 1;
    } else {
        float inputRange = (MAX_RSSI - MIN_RSSI);
        float outputRange = (numLevels - 1);
        if(inputRange != 0)
            return (int) ((float) (rssi - MIN_RSSI) * outputRange / inputRange);
    }
    return 0;
}


来源:https://stackoverflow.com/questions/2873469/issue-with-wifimanager-calculatesignallevelrssi-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!