0

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!

  • Are you correctly stopping the scans you start? Maybe show your code? – Emil May 29 '19 at 10:40
  • Yes, I'm stopping 10 seconds after I started the scan. My code is written in react native and in it's native android class is using "LegacyScanManager" scan function – Daniel Piflaks May 29 '19 at 10:57
  • Well the only reason that this happens (unless there is a bug in Android's Bluetooth stack) is if the resources are not cleaned up properly. For scanning this means the scan needs to be stopped at some point. For a GATT connection it means the BluetoothGatt object must be closed. So it's best if you show the code so we can try to find the bug. I guess you use https://github.com/innoveit/react-native-ble-manager/blob/master/android/src/main/java/it/innove/LegacyScanManager.java but how does your own code look like? – Emil May 29 '19 at 12:33
  • First of all, thank you for your help! I'm using "react native ble manager". I added it's scanning function to the post. – Daniel Piflaks May 29 '19 at 13:07
  • Can you show your looping code that "is scanning for BLE for 10 seconds in a loop(start - wait for 10 seconds - stop)"? – Emil May 29 '19 at 13:38

0 Answers0