Fix Android cleanup on gatt disconnect (#109)

This commit is contained in:
Rohit Sangwan
2024-12-05 15:56:46 +05:30
committed by GitHub
parent 8524b94f19
commit 9acb13f106
2 changed files with 5 additions and 4 deletions
@@ -178,9 +178,9 @@ fun <T> SparseArray<T>.toList(): List<Pair<Int, T>> {
fun BluetoothGatt.getCharacteristic( fun BluetoothGatt.getCharacteristic(
service: String, service: String,
characteristic: String, characteristic: String,
): BluetoothGattCharacteristic? = ): BluetoothGattCharacteristic? {
getService(UUID.fromString(service)).getCharacteristic(UUID.fromString(characteristic)) return getService(UUID.fromString(service))?.getCharacteristic(UUID.fromString(characteristic))
}
fun subscriptionFailedError(error: String? = null): Result<Unit> { fun subscriptionFailedError(error: String? = null): Result<Unit> {
return Result.failure( return Result.failure(
@@ -716,7 +716,6 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
private fun cleanConnection(gatt: BluetoothGatt) { private fun cleanConnection(gatt: BluetoothGatt) {
knownGatts.remove(gatt) knownGatts.remove(gatt)
gatt.disconnect() gatt.disconnect()
gatt.close()
readResultFutureList.removeAll { readResultFutureList.removeAll {
if (it.deviceId == gatt.device.address) { if (it.deviceId == gatt.device.address) {
@@ -885,6 +884,8 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
gatt.device.address, false, status.parseHciErrorCode() gatt.device.address, false, status.parseHciErrorCode()
) {} ) {}
} }
Log.d(TAG, "Closing gatt for ${gatt.device.name}")
gatt.close()
} }
} }