From 28338c2a96cb594a6aef890d1af9021d37fd7a83 Mon Sep 17 00:00:00 2001 From: Arka Prava Basu Date: Thu, 21 Mar 2024 19:40:13 +0530 Subject: [PATCH 1/2] Catch security exception on writing / reading characteristics - https://github.com/NordicSemiconductor/Android-BLE-Library/issues/507 - https://issuetracker.google.com/issues/330663537, https://issuetracker.google.com/issues/330663537 - this is a continuation of 229ad11f75f0a85c005a885386aab2cbc5516ff7 where we get read write errors on some Android devices (above api level 33). Signed-off-by: Arka Prava Basu --- .../android/ble/BleManagerHandler.java | 116 +++++++++++------- 1 file changed, 73 insertions(+), 43 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 652466ab..4f21664b 100644 --- a/ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java +++ b/ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java @@ -943,19 +943,24 @@ private boolean internalEnableNotifications(@Nullable final BluetoothGattCharact } log(Log.VERBOSE, () -> "Enabling notifications for " + characteristic.getUuid()); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - log(Log.DEBUG, () -> - "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb, value=0x01-00)"); - return gatt.writeDescriptor(descriptor, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE) == BluetoothStatusCodes.SUCCESS; - } else { - log(Log.DEBUG, () -> "descriptor.setValue(0x01-00)"); - descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); - log(Log.DEBUG, () -> "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb)"); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - return gatt.writeDescriptor(descriptor); + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + log(Log.DEBUG, () -> + "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb, value=0x01-00)"); + return gatt.writeDescriptor(descriptor, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE) == BluetoothStatusCodes.SUCCESS; } else { - return internalWriteDescriptorWorkaround(descriptor); + log(Log.DEBUG, () -> "descriptor.setValue(0x01-00)"); + descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); + log(Log.DEBUG, () -> "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb)"); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + return gatt.writeDescriptor(descriptor); + } else { + return internalWriteDescriptorWorkaround(descriptor); + } } + } catch (final SecurityException e) { + log(Log.ERROR, e::getLocalizedMessage); + return false; } } return false; @@ -978,19 +983,24 @@ private boolean internalDisableNotifications(@Nullable final BluetoothGattCharac } log(Log.VERBOSE, () -> "Disabling notifications and indications for " + characteristic.getUuid()); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - log(Log.DEBUG, () -> - "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb, value=0x00-00)"); - return gatt.writeDescriptor(descriptor, BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE) == BluetoothStatusCodes.SUCCESS; - } else { - log(Log.DEBUG, () -> "descriptor.setValue(0x00-00)"); - descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); - log(Log.DEBUG, () -> "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb)"); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - return gatt.writeDescriptor(descriptor); + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + log(Log.DEBUG, () -> + "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb, value=0x00-00)"); + return gatt.writeDescriptor(descriptor, BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE) == BluetoothStatusCodes.SUCCESS; } else { - return internalWriteDescriptorWorkaround(descriptor); + log(Log.DEBUG, () -> "descriptor.setValue(0x00-00)"); + descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); + log(Log.DEBUG, () -> "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb)"); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + return gatt.writeDescriptor(descriptor); + } else { + return internalWriteDescriptorWorkaround(descriptor); + } } + } catch (final SecurityException e) { + log(Log.ERROR, e::getLocalizedMessage); + return false; } } return false; @@ -1012,19 +1022,24 @@ private boolean internalEnableIndications(@Nullable final BluetoothGattCharacter } log(Log.VERBOSE, () -> "Enabling indications for " + characteristic.getUuid()); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - log(Log.DEBUG, () -> - "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb, value=0x02-00)"); - return gatt.writeDescriptor(descriptor, BluetoothGattDescriptor.ENABLE_INDICATION_VALUE) == BluetoothStatusCodes.SUCCESS; - } else { - log(Log.DEBUG, () -> "descriptor.setValue(0x02-00)"); - descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); - log(Log.DEBUG, () -> "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb)"); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - return gatt.writeDescriptor(descriptor); + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + log(Log.DEBUG, () -> + "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb, value=0x02-00)"); + return gatt.writeDescriptor(descriptor, BluetoothGattDescriptor.ENABLE_INDICATION_VALUE) == BluetoothStatusCodes.SUCCESS; } else { - return internalWriteDescriptorWorkaround(descriptor); + log(Log.DEBUG, () -> "descriptor.setValue(0x02-00)"); + descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); + log(Log.DEBUG, () -> "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb)"); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + return gatt.writeDescriptor(descriptor); + } else { + return internalWriteDescriptorWorkaround(descriptor); + } } + } catch (final SecurityException e) { + log(Log.ERROR, e::getLocalizedMessage); + return false; } } return false; @@ -1242,7 +1257,12 @@ private boolean internalBeginReliableWrite() { log(Log.VERBOSE, () -> "Beginning reliable write..."); log(Log.DEBUG, () -> "gatt.beginReliableWrite()"); - return reliableWriteInProgress = gatt.beginReliableWrite(); + try { + return reliableWriteInProgress = gatt.beginReliableWrite(); + } catch (final SecurityException e) { + log(Log.ERROR, e::getLocalizedMessage); + return false; + } } private boolean internalExecuteReliableWrite() { @@ -1255,7 +1275,12 @@ private boolean internalExecuteReliableWrite() { log(Log.VERBOSE, () -> "Executing reliable write..."); log(Log.DEBUG, () -> "gatt.executeReliableWrite()"); - return gatt.executeReliableWrite(); + try { + return gatt.executeReliableWrite(); + } catch (final SecurityException e) { + log(Log.ERROR, e::getLocalizedMessage); + return false; + } } private boolean internalAbortReliableWrite() { @@ -1266,15 +1291,20 @@ private boolean internalAbortReliableWrite() { if (!reliableWriteInProgress) return false; - log(Log.VERBOSE, () -> "Aborting reliable write..."); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - log(Log.DEBUG, () -> "gatt.abortReliableWrite()"); - gatt.abortReliableWrite(); - } else { - log(Log.DEBUG, () -> "gatt.abortReliableWrite(device)"); - gatt.abortReliableWrite(gatt.getDevice()); + try { + log(Log.VERBOSE, () -> "Aborting reliable write..."); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + log(Log.DEBUG, () -> "gatt.abortReliableWrite()"); + gatt.abortReliableWrite(); // todo maybe here too? + } else { + log(Log.DEBUG, () -> "gatt.abortReliableWrite(device)"); + gatt.abortReliableWrite(gatt.getDevice()); + } + return true; + } catch (final SecurityException e) { + log(Log.ERROR, e::getLocalizedMessage); + return false; } - return true; } @Deprecated From fd5cea93f25adab82a976b7f50dc1508ccc6816d Mon Sep 17 00:00:00 2001 From: Arka Prava Basu Date: Thu, 21 Mar 2024 20:22:15 +0530 Subject: [PATCH 2/2] Remove comment Signed-off-by: Arka Prava Basu --- .../main/java/no/nordicsemi/android/ble/BleManagerHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 4f21664b..fa5fca10 100644 --- a/ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java +++ b/ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java @@ -1295,7 +1295,7 @@ private boolean internalAbortReliableWrite() { log(Log.VERBOSE, () -> "Aborting reliable write..."); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { log(Log.DEBUG, () -> "gatt.abortReliableWrite()"); - gatt.abortReliableWrite(); // todo maybe here too? + gatt.abortReliableWrite(); } else { log(Log.DEBUG, () -> "gatt.abortReliableWrite(device)"); gatt.abortReliableWrite(gatt.getDevice());