NordicSemiconductor / Android-BLE-Library

A library that makes working with Bluetooth LE on Android a pleasure. Seriously.
BSD 3-Clause "New" or "Revised" License
1.98k stars 413 forks source link

Sometimes onServiceDiscoverd is not called when connecting. #536

Open cos-overclock opened 9 months ago

cos-overclock commented 9 months ago

The BleManager was used to connect with the peripheral device. It is recognised that if the device is not a bonding device when connected, the discoverService is executed after a delay of 300 ms, but in reality the BluetoothGatt executes the discoverService immediately. I don't know if this is the direct cause, but sometimes BleManager's onServicesDiscovered is not called. Am I missing some setting that is required for the connection?

2023-11-13 16:48:17.882 31278-31432 BluetoothGatt                 D  connect() - device: 00:3C:84:13:9B:D5, auto: false
2023-11-13 16:48:17.882 31278-31432 BluetoothGatt                 D  registerApp()
2023-11-13 16:48:17.882 31278-31432 BluetoothGatt                 D  registerApp() - UUID=3c4f615a-b9f9-4bbc-bf0e-f566a7b50f0d
2023-11-13 16:48:17.886 31278-31348 BluetoothGatt                 D  onClientRegistered() - status=0 clientIf=6
2023-11-13 16:48:18.187 31278-31291 BluetoothGatt                 D  onClientConnectionState() - status=0 clientIf=6 device=00:3C:84:13:9B:D5
2023-11-13 16:48:18.187 31278-31278 MyBleManager                  I  Connected to 00:3C:84:13:9B:D5
2023-11-13 16:48:18.189 31278-31278 BleConnect...onObserver       D  ConnectionObserver.onDeviceConnected
2023-11-13 16:48:18.491 31278-31278 BluetoothGatt                 D  discoverServices() - device: 00:3C:84:13:9B:D5
cos-overclock commented 9 months ago

Sorry, I misunderstood, as I had restricted the log level output to logcat. After a 300 msec WAIT for the connection, the discoverService was called. There seems to be another reason why the onServiceDiscoverd callback is not being called.

2023-11-14 10:37:09.401  6449-6583  BluetoothGatt                D  connect() - device: 00:3C:84:13:9B:D5, auto: false
2023-11-14 10:37:09.401  6449-6583  BluetoothGatt                D  registerApp()
2023-11-14 10:37:09.402  6449-6583  BluetoothGatt                D  registerApp() - UUID=1e571094-4100-4f3a-ac42-10e3470bc72a
2023-11-14 10:37:09.407  6449-6557  BluetoothGatt                D  onClientRegistered() - status=0 clientIf=6
2023-11-14 10:37:09.741  6449-6557  BluetoothGatt                D  onClientConnectionState() - status=0 clientIf=6 device=00:3C:84:13:9B:D5
2023-11-14 10:37:09.742  6449-6449  MyBleManager                 D  [Callback] Connection state changed with status: 0 and new state: 2 (CONNECTED)
2023-11-14 10:37:09.742  6449-6449  MyBleManager                 I  Connected to 00:3C:84:13:9B:D5
2023-11-14 10:37:09.743  6449-6449  MyBleManager                 D  wait(300)
2023-11-14 10:37:09.743  6449-6449  BleConnect...onObserver      D  ConnectionObserver.onDeviceConnected
2023-11-14 10:37:10.044  6449-6449  MyBleManager                 V  Discovering services...
2023-11-14 10:37:10.044  6449-6449  MyBleManager                 D  gatt.discoverServices()
2023-11-14 10:37:10.045  6449-6449  BluetoothGatt                D  discoverServices() - device: 00:3C:84:13:9B:D5
2023-11-14 10:37:19.398  6449-6583  BleConnector                 W  kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 10000 ms