I am having problems connecting to my BLE device. It works but sometimes it takes a long time (>8 seconds), sometimes it is much faster (50-100 ms)
I have been hunting this bug for days and I have found how to resolve it (keep on scanning), but the behavior is totally different then I expected.
According to the bluetooth literature one should stop scanning for devices before connecting to a device. I am finding the exact opposite.
When I stop scanning, connecting takes a very long time, 6 seconds on avarage. When I continue scanning (scan for 1500 ms, stop for 1500 ms, and so on) then the connection takes 50-100 ms while scanning, while stopped it connects as soon as i start scanning.
I found this behavior on my Nexus 5 with android version 4.4.2. My question is: does anybody recognize this? This is clearly a bug in the BLE implementation, right?
Here some of my logging:
What i do is manually press buttons on my android device, turning a led on and off on my BLE device. I connect, do service discovery (if needed), write the characteristic and disconnect. Meanwhile there is a background thread doing the start and stop scanning (bluetoothAdapter.startLeScan and stopLeScan).
10-24 15:58:17.656: started scanning: true
10-24 15:58:19.156: stop scanning: true
10-24 15:58:20.666: started scanning: true
10-24 15:58:20.876: on pressed (write 'b' to set the led to blue)
10-24 15:58:20.876: executing BleWriteCharachteristicCommand set:b
10-24 15:58:20.876: connecting to:C1:E2:F9:2E:AA:23
10-24 15:58:20.876: gatt.connect() to C1:E2:F9:2E:AA:23
10-24 15:58:20.916: onConnectionStateChange:STATE_CONNECTED to C1:E2:F9:2E:AA:23
10-24 15:58:20.936: **assureConnectedAndServicesDiscovered took:53**
10-24 15:58:20.936: command finished in 2 ms - set:b
10-24 15:58:21.636: disconnecting from:C1:E2:F9:2E:AA:23
10-24 15:58:21.636: onConnectionStateChange:STATE_DISCONNECTED to C1:E2:F9:2E:AA:23
10-24 15:58:22.166: stop scanning: true
10-24 15:58:23.676: started scanning: true
<cut></cut>
10-24 15:58:34.216: stop scanning: true
10-24 15:58:34.676: on pressed (write 'b' to set the led to blue)
10-24 15:58:34.686: executing BleWriteCharachteristicCommand set:b
10-24 15:58:34.686: connecting to:C1:E2:F9:2E:AA:23
10-24 15:58:34.686: gatt.connect() to C1:E2:F9:2E:AA:23
10-24 15:58:35.716: started scanning: true
here the weird thing: connected immediately after a start scan! but then it takes 1103 ms
10-24 15:58:35.766: onConnectionStateChange:STATE_CONNECTED to C1:E2:F9:2E:AA:23
10-24 15:58:35.786: **assureConnectedAndServicesDiscovered took:1103**
10-24 15:58:35.786: command finished in 2 ms - set:b
10-24 15:58:36.486: disconnecting from:C1:E2:F9:2E:AA:23
10-24 15:58:36.496: onConnectionStateChange:STATE_DISCONNECTED to C1:E2:F9:2E:AA:23
10-24 15:58:37.216: stop scanning: true
10-24 15:58:38.726: started scanning: true
10-24 15:58:40.236: stop scanning: true
0 comments:
Post a Comment