Skip to content

Commit

Permalink
Merge pull request #550 from archie94/crash_fix_bluetooth_priviledged…
Browse files Browse the repository at this point in the history
…_permission

Catch security exception on writing / reading characteristics
  • Loading branch information
philips77 authored Mar 21, 2024
2 parents 36836b7 + fd5cea9 commit 1980270
Showing 1 changed file with 73 additions and 43 deletions.
116 changes: 73 additions & 43 deletions ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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();
} 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
Expand Down

0 comments on commit 1980270

Please sign in to comment.