System stops collecting data from and connecting to devices after running for many hours.
See original GitHub issueVersion
- react-native-ble-manager v7.1.4
- react-native v061.5
- iOS/Android v.6 & v.9
Expected behaviour
Remains connected and collecting data. Other issues that seem similar #252
Actual behaviour
On an Android 6 medical grade device made by advantech. I connect to 4, bonded devices, I have a 2 notification characteristics setup one of which gets data every ~6 seconds. This all works fine for a while, like hours, then slowly devices disconnect and scanning for devices that are advertising do not get picked up.
I see a couple things in the logs that are pointing to running out of BluetoothGATT instances, as described here. There are only 32 BluetoothGATT objects available for the system from what I’m reading/understanding. Something seems to be holding onto these or something else that I can’t fathom. The code does not appear to be doing this in RN-ble-mgr but I’m not a Java programmer; perhaps the library we’re using? I have a counter for when devices connect/disconnect and it’s not happening so it may not have anything to do with those two actions.
I see these messages
03-12 09:04:53.515 13449 13484 D BluetoothAdapter: STATE_ON
03-12 09:04:53.518 7079 7092 D BtGatt.GattService: registerClient() - UUID=28ff312c-ec14-4f97-a90f-eb46fdb34265
03-12 09:04:53.518 7079 7106 E bt_btif : Register with GATT stack failed.
03-12 09:04:53.518 7079 7106 E bt_btif : Register with GATT stack failed.
03-12 09:04:53.518 7079 7098 D BtGatt.GattService: onClientRegistered() - UUID=28ff312c-ec14-4f97-a90f-eb46fdb34265, clientIf=0
03-12 09:04:53.518 13449 13463 D BluetoothLeScanner: onClientRegistered() - status=133 clientIf=0
and
03-13 14:11:16.805 8509 8509 D BluetoothLeScanner: could not find callback wrapper
03-13 14:11:16.807 8509 8543 D ReactNativeJS: BleManagerStopScan
03-13 14:11:16.852 8509 8543 I ReactNativeJS: active session
03-13 14:11:17.466 2696 3018 D EVENT_THREAD: void CEventThread::run() POLLIN event on Fd (1)
03-13 14:11:17.466 2696 3018 D EVENT_THREAD: void CEventThread::run() Do poll with timeout: -1
03-13 14:11:17.466 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=3 connected=0 conn_id=259 reason=0x0008
03-13 14:11:17.466 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=4 connected=0 conn_id=260 reason=0x0008
03-13 14:11:17.466 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=5 connected=0 conn_id=261 reason=0x0008
03-13 14:11:17.466 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=6 connected=0 conn_id=262 reason=0x0008
03-13 14:11:17.466 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=7 connected=0 conn_id=263 reason=0x0008
03-13 14:11:17.467 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=8 connected=0 conn_id=264 reason=0x0008
03-13 14:11:17.467 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=9 connected=0 conn_id=265 reason=0x0008
03-13 14:11:17.467 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=10 connected=0 conn_id=266 reason=0x0008
03-13 14:11:17.467 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=11 connected=0 conn_id=267 reason=0x0008
03-13 14:11:17.467 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=12 connected=0 conn_id=268 reason=0x0008
03-13 14:11:17.467 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=13 connected=0 conn_id=269 reason=0x0008
03-13 14:11:17.467 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=14 connected=0 conn_id=270 reason=0x0008
03-13 14:11:17.467 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=15 connected=0 conn_id=271 reason=0x0008
03-13 14:11:17.467 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=16 connected=0 conn_id=272 reason=0x0008
03-13 14:11:17.467 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=17 connected=0 conn_id=273 reason=0x0008
03-13 14:11:17.467 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=18 connected=0 conn_id=274 reason=0x0008
03-13 14:11:17.467 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=19 connected=0 conn_id=275 reason=0x0008
03-13 14:11:17.467 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=20 connected=0 conn_id=276 reason=0x0008
03-13 14:11:17.467 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=21 connected=0 conn_id=277 reason=0x0008
03-13 14:11:17.467 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=22 connected=0 conn_id=278 reason=0x0008
03-13 14:11:17.467 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=23 connected=0 conn_id=279 reason=0x0008
03-13 14:11:17.467 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=24 connected=0 conn_id=280 reason=0x0008
03-13 14:11:17.467 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=25 connected=0 conn_id=281 reason=0x0008
03-13 14:11:17.468 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=26 connected=0 conn_id=282 reason=0x0008
03-13 14:11:17.468 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=27 connected=0 conn_id=283 reason=0x0008
03-13 14:11:17.468 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=28 connected=0 conn_id=284 reason=0x0008
03-13 14:11:17.468 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=29 connected=0 conn_id=285 reason=0x0008
03-13 14:11:17.468 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=30 connected=0 conn_id=286 reason=0x0008
03-13 14:11:17.468 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=31 connected=0 conn_id=287 reason=0x0008
03-13 14:11:17.468 3927 4077 W bt_btif : bta_gattc_conn_cback() - cif=32 connected=0 conn_id=288 reason=0x0008
03-13 14:11:17.468 3927 4077 I bt_btm_sec: btm_sec_disconnected clearing pending flag handle:65 reason:8
03-13 14:11:17.469 3927 3985 D BtGatt.GattService: onDisconnected() - clientIf=8, connId=264, address=80:EA:CA:70:01:0A
03-13 14:11:17.469 8509 8601 D BluetoothGatt: onClientConnectionState() - status=8 clientIf=8 device=80:EA:CA:70:01:0A
03-13 14:11:17.470 8509 8601 D ReactNativeBleManager: onConnectionStateChange to 0 on peripheral: 80:EA:CA:70:01:0A with status 8
03-13 14:11:17.470 8509 8601 D BluetoothGatt: cancelOpen() - device: 80:EA:CA:70:01:0A
03-13 14:11:17.473 3927 5420 D BtGatt.GattService: clientDisconnect() - address=80:EA:CA:70:01:0A, connId=null
03-13 14:11:17.473 3927 3985 E BluetoothRemoteDevices: state12newState1
03-13 14:11:17.473 8509 8601 D BluetoothGatt: close()
03-13 14:11:17.473 8509 8601 D BluetoothGatt: unregisterApp() - mClientIf=8
03-13 14:11:17.474 3927 30116 D BtGatt.GattService: unregisterClient() - clientIf=8
03-13 14:11:17.477 8509 8601 D ReactNativeBleManager: Peripheral event (BleManagerDisconnectPeripheral):80:EA:CA:70:01:0A
03-13 14:11:17.478 8509 8543 D ReactNativeJS: 'BleManagerDisconnectPeripheral ', { status: 8, peripheral: '80:EA:CA:70:01:0A' }
03-13 14:11:17.478 3927 4077 E bt_btif : No such connection need to be cancelled
03-13 14:11:17.478 3927 4077 E bt_btif : bta_gattc_mark_bg_conn unable to find the bg connection mask for: 80:ea:ca:70:01:0a
Steps to reproduce
- Connect to multiple devices; I’m using 4
- Get data from devices via notifications regularly; mine happen ~6sec from each device.
- Let run for many hours.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (6 by maintainers)
Top GitHub Comments
I have solved this issue by using a long running scan. Prior I was running a scan about every 10 seconds that lasted for 5 seconds. By doing so, somehow it leaked GATT objects. I have made the following change and have not yet hit the gatt object limit. I now run a scan for 30 minutes or until all my devices are connected. Because sometimes a client connects and disconnects immediately, I delay the stop scan for another 10 seconds to ensure all clients are still connected.
My lessons learned:
I am now sure that scanning is not letting go of the resources some how some way. On an android 6 tablet. I run a scan loop every 10 seconds that has a duration of 5 seconds. As you can see below, the clientif value just keeps climbing until it finally runs out of GATT objects.
Does anyone one see anything suspect here? https://gist.githubusercontent.com/anwarhamr/207f3ba5111f9c00bc7e26e8fc716c7b/raw/6e8859a3030ef40133d09fb0096ae899ab3d3898/scan%2520only%2520every%252010s%2520for%25205s