From e8adafb988f47f534b8eec15d4115e3bf022aa1b Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Fri, 16 Feb 2024 11:35:25 +0100 Subject: [PATCH] Catching SecurityException from setCharacteristicNotification(...) --- .../android/ble/BleManagerHandler.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java b/ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java index 2e1e037b..652466ab 100644 --- a/ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java +++ b/ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java @@ -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) { @@ -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) { @@ -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) {