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

Fixed restoring connection state after cancelled connection attempt #529

Closed philips77 closed 10 months ago

philips77 commented 10 months ago

This PR fixes #524.

When the first connection attempt fails, and the library will start to retry connection, this code is invoked: https://github.com/NordicSemiconductor/Android-BLE-Library/blob/53e169ec7dec77923c50619967b895e8a91091aa/ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java#L639-L648 If the ConnectRequest times out, or gets cancelled when the thread is waiting 200 ms, the connection was cancelled, but connectionState was never set back to STATE_DISCONNECTED.

This PR fixes that by checking the value of bluetoothGatt reference after internalConnect is called. In case of null, that is when connectRequest.finished was true, the state is reset to disconnected.

Also, this PR adds another change. When the connection attempt fails, the onServicesInvalidated() is not called, as there were no services to invalidate in the first place. This MAY be breaking for some, but I hope not.