diff --git a/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/Device.kt b/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/Device.kt index 582dd6fe..774cd4b6 100644 --- a/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/Device.kt +++ b/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/Device.kt @@ -35,6 +35,7 @@ class Device( private var bluetoothGatt: BluetoothGatt? = null private var callbackMap = HashMap Unit)>() private var timeoutMap = HashMap() + private var setNotificationsKey = "" private val gattCallback: BluetoothGattCallback = object : BluetoothGattCallback() { override fun onConnectionStateChange( @@ -125,6 +126,21 @@ class Device( callbackMap[notifyKey]?.invoke(CallbackResponse(true, value)) } } + + override fun onDescriptorWrite( + gatt: BluetoothGatt, + descriptor: BluetoothGattDescriptor, + status: Int + ) { + super.onDescriptorWrite(gatt, descriptor, status) + if (status == BluetoothGatt.GATT_SUCCESS) { + resolve(setNotificationsKey, "Setting notification succeeded.") + } else { + reject(setNotificationsKey, "Setting notification failed.") + } + setNotificationsKey = "" + + } } fun getId(): String { @@ -219,6 +235,7 @@ class Device( callback: (CallbackResponse) -> Unit, ) { val key = "setNotifications|$serviceUUID|$characteristicUUID" + setNotificationsKey = key val notifyKey = "notification|$serviceUUID|$characteristicUUID" callbackMap[key] = callback if (notifyCallback != null) { @@ -240,11 +257,7 @@ class Device( } val resultDesc = bluetoothGatt?.writeDescriptor(descriptor) if (result == true && resultDesc == true) { - if (enable) { - resolve(key, "Notification enabled.") - } else { - resolve(key, "Notification disabled.") - } + // wait for onDescriptorWrite } else { reject(key, "Setting notification failed.") }