My android app(react native app named "myApp") is scanning for BLE for 10 seconds in a loop(start - wait for 10 seconds - stop) I have noticed that after a while(something about 7 hours) my app can't find any device(although there is devices around).
When I saved and checked the logcat, I saw that after each try to scan, "myApp" tries to run registerClient()
and after the scan unregisterClient()
from GATT
Right before the problem starts I can see the following:
BtGatt.GattService: registerClient() - UUID=c4de3a02-66c8-41ac-bf9d-f42d8f9d160f, pid=2097, name=com.myApp
E bt_att : GATT_Register: can't Register GATT client, MAX client reached!
E bt_btif : Register with GATT stack failed.
bt_att : GATT_Register: can't Register GATT client, MAX client reached!
After more digging into the logcat I saw that there is one more client that tries to registerClient()
to GATT:
BtGatt.GattService: registerClient() - UUID=9aa64bcd-402a-4474-ab5d-250e14bd77a1, pid=1311, name=com.google.android.gms.persistent
After looking for "com.google.android.gms.persistent" I found that it's google play service.
This service is trying to register to GATT but also gets the same problem(MAX client reached) until it tries to register when "myApp" is unregistered from GATT - so it have available spot and then "myApp" can't register no more as "com.google.android.gms.persistent" will not unregister.
The only thing that is resolving it is to toggle Bluetooth, and then "myApp" can register again.
Did someone encountered this problem and found any solution? maybe how to clear all clients that are registered to GATT service?
EDIT:
I'm using "LegacyScanManager" and "react native ble manager" package. It's scan function(that is calling to "scan" function of LegacyScanManager) is:
@ReactMethod
public void scan(ReadableArray serviceUUIDs, final int scanSeconds, boolean allowDuplicates, ReadableMap options, Callback callback) {
Log.d(LOG_TAG, "scan");
if (getBluetoothAdapter() == null) {
Log.d(LOG_TAG, "No bluetooth support");
callback.invoke("No bluetooth support");
return;
}
if (!getBluetoothAdapter().isEnabled()) {
return;
}
synchronized (peripherals) {
Iterator<Map.Entry<String, Peripheral>> iterator = peripherals.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Peripheral> entry = iterator.next();
if (!entry.getValue().isConnected()) {
iterator.remove();
}
}
}
if (scanManager != null) scanManager.scan(serviceUUIDs, scanSeconds, options, callback);
}
The parameters that I'm passing :
serviceUUIDs = none(empty list),
scanSeconds = 10,
allowDuplicates = false,
options = "numberOfMatches = 3, matchMode = 1, scanMode = 0" ,
callback = my callback function from js
Thanks you!