so i a working with android and ble device and when i tap my ble device it will send a notification that my application should receive and work accordingly but i am getting failed to write descriptor to enable notifications only if i manually turn on it in nrf connect app i am able to receive it in my application which i dont want i want my app to enable it.
==============this is my code====================
fun enableNotifications(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) {
gatt.setCharacteristicNotification(characteristic, true)
val descriptor = characteristic.getDescriptor(UUID.fromString(CCC_DESCRIPTOR_UUID))
if (descriptor != null) {
descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
// Write descriptor properly (wait for onDescriptorWrite() callback)
if (!gatt.writeDescriptor(descriptor)) {
Log.e("BluetoothScanActivity", "Failed to write descriptor for ${characteristic.uuid}")
}
} else {
Log.e("BluetoothScanActivity", "Descriptor not found for ${characteristic.uuid}")
}
}
// Handle descriptor write callback
val bluetoothGattCallback = object : BluetoothGattCallback() {
override fun onDescriptorWrite(gatt: BluetoothGatt?, descriptor: BluetoothGattDescriptor?, status: Int) {
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.d("BluetoothScanActivity", "Successfully wrote descriptor for ${descriptor?.characteristic?.uuid}")
} else {
Log.e("BluetoothScanActivity", "Failed to write descriptor for ${descriptor?.characteristic?.uuid}")
}
}
}
i gave code to enable notifications and write on descriptors but i am getting error as failed to write descriptor can u please rectify this issuei am getting this error
You can only have one outstanding GATT operation at a time per BluetoothGatt object. You need to wait until the previous operation has completed, which is indicated by the callback for the corresponding operation.
In your case, you must wait for onDescriptorWrite for the first descriptor write before it is possible to send the second descriptor write. Per your log I can see that you perform in total three descriptor writes.
Please see my previous answer https://stackoverflow.com/a/47103533/556495.