Search code examples
androidibeaconrssialtbeacon

rssi returned by altbeacon library 127 messes up distance


I am working on an app that creates certain messages on entering different regions based on the range to a beacon (e.g. position < .5 | position < 2 | position < 10). I experienced a weird behavior while logging the distance and the rssi. Currently I am replacing the RSSI with the last received RSSI if it is 127. This is the only value that seems completely out of place.

The Problem

05-08 10:09:48.379  21495-22283 I/RangingService? RSSI: 127 Distance: 2533.712149492241 meters
05-08 10:09:49.500  21495-22284 I/RangingService? RSSI: -38 Distance: 0.07068384635393776 meters
05-08 10:09:50.662  21495-22301 I/RangingService? RSSI: 127 Distance: 0.07068384635393776 meters
05-08 10:09:51.795  21495-22302 I/RangingService? RSSI: 127 Distance: 2533.712149492241 meters
05-08 10:09:52.946  21495-22318 I/RangingService? RSSI: -34 Distance: 0.10971085339590038 meters
05-08 10:09:54.074  21495-22327 I/RangingService? RSSI: 127 Distance: 0.10971085339590038 meters
05-08 10:09:55.375  21495-22404 I/RangingService? RSSI: 127 Distance: 2533.712149492241 meters
05-08 10:09:56.499  21495-22488 I/RangingService? RSSI: -36 Distance: 0.08827424889527898 meters
05-08 10:09:57.637  21495-22492 I/RangingService? RSSI: -36 Distance: 0.008486821580451052 meters
05-08 10:09:58.771  21495-22493 I/RangingService? RSSI: 127 Distance: 0.08827424889527898 meters
05-08 10:09:59.902  21495-22509 I/RangingService? RSSI: 127 Distance: 2533.712149492241 meters

If you look at the distance, it will randomly spike with every calculation after or with the rssi of 127. I don't understand why the rssi would be 127 so randomly so often. Any ideas?

The workaround

int rssi2use = beacon.getRssi();
if (rssi2use == 127) {
    Log.i(TAG, "Using last Rssi " + lastRssi + " instead of 127");
    rssi2use = lastRssi;
} else {
    lastRssi = beacon.getRssi();
}

MyBeacon.java Simplification of distance calulation to clean out the averages of rssis

public class MyBeacon extends Beacon {

    private static final String TAG = MyBeacon.class.getSimpleName();

    private final Beacon beacon;
    private final int rssi2use;

    public MyBeacon(final Beacon beacon, int rssi2use) {
        this.beacon = beacon;
        this.rssi2use = rssi2use;
    }

    @Override
    public double getDistance() {
        return mDistance = calculateDistance(beacon.getTxPower(), rssi2use);
    }

    public Beacon getSuperBeacon() {
        return beacon;
    }
}

RangingService.java

@Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "Service onCreate");
        beaconManager.setDebug(true);
        RangedBeacon.setSampleExpirationMilliseconds(2200);
        beaconManager = BeaconManager.getInstanceForApplication(this);
        beaconManager.bind(this);
    }

I only listen for one speficic beacon.

@Override
    public void onBeaconServiceConnect() {

        beaconManager.setRangeNotifier(new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                if (beacons.size() == 1) {
                    Beacon beacon = beacons.iterator().next();
                    handleBeaconInRange(beacon);
                }
            }
        });

        try {
            beaconManager.startRangingBeaconsInRegion(new Region(RANGE_REGION_ID, null, null, null));
        } catch (RemoteException e) {
            Log.e(TAG, "Exception on onbeaconServiceConnect", e);
        }

    }

There's more to handleBeaconInRange() that reacts to different distances, but this produces the log.

private void handleBeaconInRange(Beacon beacon) {
        int rssi2use = beacon.getRssi();
        if (rssi2use == 127) {
            Log.i(TAG, "Using last Rssi " + lastRssi + " instead of 127");
            rssi2use = lastRssi;
        } else {
            lastRssi = beacon.getRssi();
        }

        MyBeacon myBeacon = new MyBeacon(beacon, rssi2use);
        double distance = myBeacon.getDistance();
        Log.i(TAG, "RSSI: " + rssi2use + " Distance: " + distance + " meters");
}

The Log:

05-08 09:25:47.675  27646-29040/ I/RangingService? Using last Rssi -56 instead of 127
05-08 09:25:47.675  27646-29040/ I/RangingService? RSSI: -56 Distance: 0.7040448636262671 meters
05-08 09:25:48.839  27646-29041/ I/RangingService? RSSI: -56 Distance: 0.7040448636262671 meters
05-08 09:25:49.982  27646-29042/ I/RangingService? RSSI: -58 Distance: 0.97085 meters
05-08 09:25:51.121  27646-29043/ I/RangingService? Using last Rssi -58 instead of 127
05-08 09:25:51.121  27646-29043/ I/RangingService? RSSI: -58 Distance: 0.97085 meters
05-08 09:25:52.263  27646-29044/ I/RangingService? Using last Rssi -58 instead of 127
05-08 09:25:52.263  27646-29044/ I/RangingService? RSSI: -58 Distance: 0.97085 meters
05-08 09:25:53.408  27646-29045/ I/RangingService? RSSI: -55 Distance: 0.5879588872684474 meters
05-08 09:25:54.575  27646-29046/ I/RangingService? Using last Rssi -55 instead of 127
05-08 09:25:54.575  27646-29046/ I/RangingService? RSSI: -55 Distance: 0.5879588872684474 meters
05-08 09:25:55.701  27646-29047/ I/RangingService? RSSI: -54 Distance: 0.4893928979531776 meters
05-08 09:25:56.858  27646-29064/ I/RangingService? RSSI: -61 Distance: 1.1474711863884917 meters
05-08 09:25:57.991  27646-29074/ I/RangingService? Using last Rssi -61 instead of 127
05-08 09:25:57.992  27646-29074/ I/RangingService? RSSI: -61 Distance: 1.1474711863884917 meters
05-08 09:25:59.130  27646-29075/ I/RangingService? RSSI: -46 Distance: 0.09846874009910023 meters
05-08 09:26:00.247  27646-29081/ I/RangingService? Using last Rssi -46 instead of 127
05-08 09:26:00.247  27646-29081/ I/RangingService? RSSI: -46 Distance: 0.09846874009910023 meters
05-08 09:26:01.392  27646-29085/ I/RangingService? RSSI: -49 Distance: 0.18521700814366415 meters
05-08 09:26:02.534  27646-29086/ I/RangingService? Using last Rssi -49 instead of 127
05-08 09:26:02.534  27646-29086/ I/RangingService? RSSI: -49 Distance: 0.18521700814366415 meters
05-08 09:26:03.680  27646-29087/ I/RangingService? Using last Rssi -49 instead of 127
05-08 09:26:03.680  27646-29087/ I/RangingService? RSSI: -49 Distance: 0.18521700814366415 meters
05-08 09:26:04.823  27646-29088/ I/RangingService? RSSI: -33 Distance: 0.0035551625558338114 meters
05-08 09:26:05.967  27646-29089/ I/RangingService? RSSI: -37 Distance: 0.011161878866100936 meters

Solution

  • This is not related to the library. It seems to be related to the used device, either firmware or adapter are spitting out wrong readings.

    Thank you @davidgyoung for helping clearing this.