Mike Holler
Mike Holler

Reputation: 975

Equivalent to WifiManager.calculateSignalLevel for Bluetooth RSSI

I would like to show Bluetooth connection strength as "signal strength bars" on my UI for a Bluetooth Low Energy device. In order to do this, I need to use the RSSI value from BluetoothGattCallback.onReadRemoteRssi. However, that method returns RSSI as decibels rather than on a bar scale. I need some way of reliably converting this value into a bar scale or percentage.

While browsing through the Android docs for an answer, I stumbled on WifiManager.calculateSignalLevel(int rssi, int numLevels). Is it possible to use this method to interpret Bluetooth signal strength? If not, how could I go about doing this?

Upvotes: 2

Views: 1318

Answers (1)

Kenneth Evans
Kenneth Evans

Reputation: 2399

With rssi in decibels (a negative number), use:

int bars = WifiManager.calculateSignalLevel(rssi, 5);

to get the bars in the usual range 0 - 4.

The code in WiFiManager (from GrepCode) is:

private static final int MIN_RSSI = -100;
private static final int MAX_RSSI = -55;
public static 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);
             return (int)((float)(rssi - MIN_RSSI) * outputRange / inputRange);
         }
     }
}

I find that on my Galaxy S4 and Note 10.1 that the level returned is not given by this code. It is more like the code with MAX_RSSI = -63 but not quite. I don't know of any way to find what code is actually being used in these Samsung devices.

I do find that the result seems to be the same as what the phone displays for the signal level. However, I am using it for Wifi signal level, not Bluetooth. I have no experience using the method with Bluetooth.

Upvotes: 2

Related Questions