I can scan for Bluetooth LE devices through startLeScan
or via the new getBluetoothLeScanner
and that works fine. However even though it keeps on scanning it never detects the same device twice. That's unfortunate because I would like to receive events when the rssi of a beacon changes. Does Android support that?
Asked
Active
Viewed 3,036 times
2

Jan Jongboom
- 26,598
- 9
- 83
- 120
-
Related: http://stackoverflow.com/questions/27111309/decreased-ble-startscan-detected-devices-on-android-5-0-lollipop There doesn't seem to be a way to get updates on RSSI without an established connection. In the link a scan is started/stopped periodically. – cygery Apr 20 '15 at 19:45
-
1I found this: "On Nexus 4 and Nexus 7 the callback is invoked either when a new device has been found or when the sensor is non-connectable. In the later case you get RSSI updates constantly." - From [Android bug tracker](https://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=65863) – Jan Jongboom Apr 21 '15 at 07:31
2 Answers
1
Implement a BluetoothGattCallback
and override the onReadRemoteRssi
method.
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
broadcastUpdate(intentAction);
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
broadcastUpdate(intentAction);
}
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
super.onReadRemoteRssi(gatt, rssi, status);
// use rssi value here
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
};

CzarMatt
- 1,773
- 18
- 22
-
Should note that you need to call `mBluetoothGatt.readRemoteRssi()` for the event to trigger – Jonno_FTW Oct 15 '18 at 01:59
1
Two solutions which I used.
1) Make a connection, call readRemoteRssi();
of gatt and override onReadRemoteRssi
, and have your own check to see if the RSSI is changed.
2) StrartLEScan every 20 Sec(20 sec is enough time to receive new advertisement packets) and have a check on onLeScan
callback to see if the device is found, if yes then compare RSSI.
RSSI is the value which your device(scanner) derives.
Also if you have code for GATT STACK, you will get onLEScan called many times for single device, it is GattService which truncate duplicate results.

AAnkit
- 27,299
- 12
- 60
- 71