9

I am developing an application using the RxAndroidBle library that performs BLE scans regularly about every 30 seconds, and some BLE operations every minute or so. After a couple of hours, usually between 5 and 24h, the scan stops working. Every time a scan is supposed to be started, I get:

09-05 09:08:37.160 8160-8160/myapp D/BluetoothAdapter: startLeScan(): null
09-05 09:08:37.165 8160-8160/myapp D/BluetoothAdapter: STATE_ON
09-05 09:08:37.165 8160-8160/myapp D/BluetoothAdapter: STATE_ON
09-05 09:08:37.165 8160-8160/myapp D/BluetoothLeScanner: Start Scan
09-05 09:08:37.165 8160-8160/myapp D/BluetoothAdapter: STATE_ON
09-05 09:08:37.165 8160-8160/myapp D/BluetoothAdapter: STATE_ON
09-05 09:08:37.170 8160-8160/myapp D/BluetoothAdapter: STATE_ON
09-05 09:08:37.170 8160-8160/myapp D/BluetoothAdapter: STATE_ON
09-05 09:08:37.210 8160-12850/myapp D/BluetoothLeScanner: onClientRegistered() - status=133 clientIf=0
09-05 09:08:37.210 8160-12850/myapp D/BluetoothLeScanner: Registration failed, unregister clientIf = 0
09-05 09:08:37.215 8160-8160/myapp D/BluetoothLeScanner: Scan failed, reason app registration failed for UUID = 4c321920-a2b7-449a-bc24-ea4361f7a255
09-05 09:08:44.150 8160-8160/myapp V/myapp.debug: unsubscribing scan
09-05 09:08:44.150 8160-8160/myapp V/myapp.debug: Clearing scan subscription
09-05 09:08:44.150 8160-8160/myapp D/BluetoothAdapter: stopLeScan()
09-05 09:08:44.150 8160-8160/myapp D/BluetoothAdapter: STATE_ON
09-05 09:08:44.155 8160-8160/myapp D/BluetoothAdapter: STATE_ON
09-05 09:08:44.155 8160-8160/myapp D/BluetoothAdapter: STATE_ON
09-05 09:08:44.155 8160-8160/myapp D/BluetoothAdapter: STATE_ON
09-05 09:08:44.155 8160-8160/myapp D/BluetoothLeScanner: could not find callback wrapper

Does anyone have any idea of what causes this problem or what can be done to fix it?

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
starman
  • 356
  • 1
  • 4
  • 11
  • Hello, I think that during so long time there can appear some errors during the connections you establish with the devices. There is a bug in RxAndroidBle ( https://github.com/Polidea/RxAndroidBle/issues/63 ) that prevents the BLE clients to be closed properly in some erroneous scenarios. – Dariusz Seweryn Sep 08 '16 at 11:35
  • 1
    After more investigation I think it's mostly related to the scanning. If I don't scan at all, I don't this problem it appears. I am still interested in learning how to work around the problem if scanning is required for the use case however. – starman Sep 14 '16 at 06:52
  • @starman you found the solution for that? – Waqar UlHaq May 29 '19 at 08:39

2 Answers2

6

On the older implementations of Android there seems to be a race condition between the time a bluetooth adapter is enabled and how quickly you can do a scan. You can trigger this error by either trying to scan with a disabled bluetooth adapter or one that is transitioning (or has open connections and is trying to read). The underlying issue that was causing it in my app was the inability of the bluetooth sub system to get a new bluetooth socket. The answer above (running out of GATT resources) could be part of it. The overall logic in an older Android device to avoid this issue is: 1. Make sure you disable/enable the bluetooth adapter about every 5 scans. This seems to help clear out older cached data. 2. Make sure you don't try to initiate a scan while the bluetooth adapter is not enabled (this is made possible if you are disabling/enabling on a regular basis). 3. Make sure you have a delay between disconnecting from GATT interfaces and doing your next scan. 4. Don't try to read the GATT characteristics of any more than about 3 devices at a time.

Overall, its somewhat unavoidable in an older Android device to completely avoid the problem but you can mitigate it by carefully timing your scan/stop scan/connect/disconnect/ cycle.

David Rubie
  • 86
  • 1
  • 4
  • 3
    You saved my life. This should be an accepted answer. You indeed can't directly start a scan after the `BluetoothAdapter.STATE_ON` state. You've to wait a few seconds. I don't exactly know how long, but it seems that waiting 5 seconds works for me. – Displee Dec 20 '19 at 08:47
  • 3
    Amazing find David. Trying to reconnect immediately after a disconnection will indeed cause this "app registration failed" error... which can then be caught by the GattCallback's onScanFailed() method. This is not only for old devices, although more frequent on older devices, it also happenned to me on my Galaxy S10 with the latest OS. – Simon Corcos Apr 01 '21 at 14:11
  • @SimonCorcos how this callback happens while connecting? you are talking about connection but the callback is for scan only? – Khamidjon Khamidov Dec 09 '21 at 10:27
  • @KhamidjonKhamidov there's a ScanCallback class with this method in it. https://developer.android.com/reference/android/bluetooth/le/ScanCallback – Simon Corcos Dec 10 '21 at 18:07
  • @SimonCorcos but it is just for BluetoothLeScanner.startScan(ScanCallback) method. I see it has ScanCallback. But, how disconnection cause "app registration failed" on scancallback? Connecting and scanning are different terms? – Khamidjon Khamidov Dec 13 '21 at 04:37
  • 1
    @KhamidjonKhamidov sorry, maybe you just misunderstood my message. When I say "trying to reconnect immediately after a disconnection" I mean "starting a scan immediately after a disconnection" – Simon Corcos Dec 13 '21 at 21:10
  • Cool. @SimonCorcos it looks very cool. I will try to do as you said. Now I am adding 4 seconds delay. What do you think it will be enought? – Khamidjon Khamidov Dec 14 '21 at 08:15
  • @Displee what do you think about 4 seconds. as we connect/disconnect a lot, I am thinking it might slow the app a bit more. – Khamidjon Khamidov Dec 14 '21 at 10:37
  • I'm using 2 seconds and I haven't had any problem since. – Simon Corcos Dec 14 '21 at 13:54
4

The problem is that after couple of connections you reach a maximal number of BluetoothGatt objects.

After you disconnect every device before you start a new scan call close() on your BluetoothGatt object.

Alexander Tumanin
  • 1,638
  • 2
  • 23
  • 37
  • This definitely helped. In addition to this, I had to add a delay before starting the scan too ( 5 seconds delay worked for me ) – Gurunath Sripad Sep 29 '20 at 12:49