Skip to content

Commit

Permalink
Catching SecurityException from setCharacteristicNotification(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
philips77 committed Feb 16, 2024
1 parent f924120 commit e8adafb
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,12 @@ private boolean internalEnableNotifications(@Nullable final BluetoothGattCharact
final BluetoothGattDescriptor descriptor = getCccd(characteristic, BluetoothGattCharacteristic.PROPERTY_NOTIFY);
if (descriptor != null) {
log(Log.DEBUG, () -> "gatt.setCharacteristicNotification(" + characteristic.getUuid() + ", true)");
gatt.setCharacteristicNotification(characteristic, true);
try {
gatt.setCharacteristicNotification(characteristic, true);
} catch (final SecurityException e) {
log(Log.ERROR, e::getLocalizedMessage);
return false;
}

log(Log.VERBOSE, () -> "Enabling notifications for " + characteristic.getUuid());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Expand Down Expand Up @@ -965,7 +970,12 @@ private boolean internalDisableNotifications(@Nullable final BluetoothGattCharac
BluetoothGattCharacteristic.PROPERTY_NOTIFY | BluetoothGattCharacteristic.PROPERTY_INDICATE);
if (descriptor != null) {
log(Log.DEBUG, () -> "gatt.setCharacteristicNotification(" + characteristic.getUuid() + ", false)");
gatt.setCharacteristicNotification(characteristic, false);
try {
gatt.setCharacteristicNotification(characteristic, false);
} catch (final SecurityException e) {
log(Log.ERROR, e::getLocalizedMessage);
return false;
}

log(Log.VERBOSE, () -> "Disabling notifications and indications for " + characteristic.getUuid());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Expand Down Expand Up @@ -994,7 +1004,12 @@ private boolean internalEnableIndications(@Nullable final BluetoothGattCharacter
final BluetoothGattDescriptor descriptor = getCccd(characteristic, BluetoothGattCharacteristic.PROPERTY_INDICATE);
if (descriptor != null) {
log(Log.DEBUG, () -> "gatt.setCharacteristicNotification(" + characteristic.getUuid() + ", true)");
gatt.setCharacteristicNotification(characteristic, true);
try {
gatt.setCharacteristicNotification(characteristic, true);
} catch (final SecurityException e) {
log(Log.ERROR, e::getLocalizedMessage);
return false;
}

log(Log.VERBOSE, () -> "Enabling indications for " + characteristic.getUuid());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Expand Down

0 comments on commit e8adafb

Please sign in to comment.