Skip to content

Commit

Permalink
fix(android): do not crash when using startNotifications on character…
Browse files Browse the repository at this point in the history
…istic that does not support notifications
  • Loading branch information
pwespi committed Apr 10, 2021
1 parent c9e8688 commit 44bc318
Showing 1 changed file with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -299,27 +299,39 @@ class Device(
}
val service = bluetoothGatt?.getService(serviceUUID)
val characteristic = service?.getCharacteristic(characteristicUUID)
if (characteristic != null) {
val result = bluetoothGatt?.setCharacteristicNotification(characteristic, enable)
val descriptor = characteristic.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG))
if (enable) {
if ((characteristic.properties and BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
} else if ((characteristic.properties and BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
descriptor.value = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
}
} else {
descriptor.value = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE
}
val resultDesc = bluetoothGatt?.writeDescriptor(descriptor)
if (result == true && resultDesc == true) {
// wait for onDescriptorWrite
} else {
reject(key, "Setting notification failed.")
if (characteristic == null) {
reject(key, "Characteristic not found.")
return
}

val result = bluetoothGatt?.setCharacteristicNotification(characteristic, enable)
if (result != true) {
reject(key, "Setting notification failed.")
return
}

val descriptor = characteristic.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG))
if (descriptor == null) {
reject(key, "Setting notification failed.")
return
}

if (enable) {
if ((characteristic.properties and BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
} else if ((characteristic.properties and BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
descriptor.value = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
}
} else {
reject(key, "Characteristic not found.")
descriptor.value = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE
}

val resultDesc = bluetoothGatt?.writeDescriptor(descriptor)
if (resultDesc != true) {
reject(key, "Setting notification failed.")
return
}
// wait for onDescriptorWrite
}

private fun resolve(key: String, value: String) {
Expand Down

0 comments on commit 44bc318

Please sign in to comment.