Issue with WifiManager.calculateSignalLevel(RSSI, 5)

后端 未结 3 1301
后悔当初
后悔当初 2020-12-30 18:47

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.calcu

3条回答
  •  渐次进展
    2020-12-30 19:16

    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
    

提交回复
热议问题