@@ -943,19 +943,24 @@ private boolean internalEnableNotifications(@Nullable final BluetoothGattCharact
943943 }
944944
945945 log (Log .VERBOSE , () -> "Enabling notifications for " + characteristic .getUuid ());
946- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
947- log (Log .DEBUG , () ->
948- "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb, value=0x01-00)" );
949- return gatt .writeDescriptor (descriptor , BluetoothGattDescriptor .ENABLE_NOTIFICATION_VALUE ) == BluetoothStatusCodes .SUCCESS ;
950- } else {
951- log (Log .DEBUG , () -> "descriptor.setValue(0x01-00)" );
952- descriptor .setValue (BluetoothGattDescriptor .ENABLE_NOTIFICATION_VALUE );
953- log (Log .DEBUG , () -> "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb)" );
954- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
955- return gatt .writeDescriptor (descriptor );
946+ try {
947+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
948+ log (Log .DEBUG , () ->
949+ "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb, value=0x01-00)" );
950+ return gatt .writeDescriptor (descriptor , BluetoothGattDescriptor .ENABLE_NOTIFICATION_VALUE ) == BluetoothStatusCodes .SUCCESS ;
956951 } else {
957- return internalWriteDescriptorWorkaround (descriptor );
952+ log (Log .DEBUG , () -> "descriptor.setValue(0x01-00)" );
953+ descriptor .setValue (BluetoothGattDescriptor .ENABLE_NOTIFICATION_VALUE );
954+ log (Log .DEBUG , () -> "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb)" );
955+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
956+ return gatt .writeDescriptor (descriptor );
957+ } else {
958+ return internalWriteDescriptorWorkaround (descriptor );
959+ }
958960 }
961+ } catch (final SecurityException e ) {
962+ log (Log .ERROR , e ::getLocalizedMessage );
963+ return false ;
959964 }
960965 }
961966 return false ;
@@ -978,19 +983,24 @@ private boolean internalDisableNotifications(@Nullable final BluetoothGattCharac
978983 }
979984
980985 log (Log .VERBOSE , () -> "Disabling notifications and indications for " + characteristic .getUuid ());
981- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
982- log (Log .DEBUG , () ->
983- "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb, value=0x00-00)" );
984- return gatt .writeDescriptor (descriptor , BluetoothGattDescriptor .DISABLE_NOTIFICATION_VALUE ) == BluetoothStatusCodes .SUCCESS ;
985- } else {
986- log (Log .DEBUG , () -> "descriptor.setValue(0x00-00)" );
987- descriptor .setValue (BluetoothGattDescriptor .DISABLE_NOTIFICATION_VALUE );
988- log (Log .DEBUG , () -> "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb)" );
989- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
990- return gatt .writeDescriptor (descriptor );
986+ try {
987+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
988+ log (Log .DEBUG , () ->
989+ "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb, value=0x00-00)" );
990+ return gatt .writeDescriptor (descriptor , BluetoothGattDescriptor .DISABLE_NOTIFICATION_VALUE ) == BluetoothStatusCodes .SUCCESS ;
991991 } else {
992- return internalWriteDescriptorWorkaround (descriptor );
992+ log (Log .DEBUG , () -> "descriptor.setValue(0x00-00)" );
993+ descriptor .setValue (BluetoothGattDescriptor .DISABLE_NOTIFICATION_VALUE );
994+ log (Log .DEBUG , () -> "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb)" );
995+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
996+ return gatt .writeDescriptor (descriptor );
997+ } else {
998+ return internalWriteDescriptorWorkaround (descriptor );
999+ }
9931000 }
1001+ } catch (final SecurityException e ) {
1002+ log (Log .ERROR , e ::getLocalizedMessage );
1003+ return false ;
9941004 }
9951005 }
9961006 return false ;
@@ -1012,19 +1022,24 @@ private boolean internalEnableIndications(@Nullable final BluetoothGattCharacter
10121022 }
10131023
10141024 log (Log .VERBOSE , () -> "Enabling indications for " + characteristic .getUuid ());
1015- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
1016- log (Log .DEBUG , () ->
1017- "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb, value=0x02-00)" );
1018- return gatt .writeDescriptor (descriptor , BluetoothGattDescriptor .ENABLE_INDICATION_VALUE ) == BluetoothStatusCodes .SUCCESS ;
1019- } else {
1020- log (Log .DEBUG , () -> "descriptor.setValue(0x02-00)" );
1021- descriptor .setValue (BluetoothGattDescriptor .ENABLE_INDICATION_VALUE );
1022- log (Log .DEBUG , () -> "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb)" );
1023- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
1024- return gatt .writeDescriptor (descriptor );
1025+ try {
1026+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
1027+ log (Log .DEBUG , () ->
1028+ "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb, value=0x02-00)" );
1029+ return gatt .writeDescriptor (descriptor , BluetoothGattDescriptor .ENABLE_INDICATION_VALUE ) == BluetoothStatusCodes .SUCCESS ;
10251030 } else {
1026- return internalWriteDescriptorWorkaround (descriptor );
1031+ log (Log .DEBUG , () -> "descriptor.setValue(0x02-00)" );
1032+ descriptor .setValue (BluetoothGattDescriptor .ENABLE_INDICATION_VALUE );
1033+ log (Log .DEBUG , () -> "gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb)" );
1034+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
1035+ return gatt .writeDescriptor (descriptor );
1036+ } else {
1037+ return internalWriteDescriptorWorkaround (descriptor );
1038+ }
10271039 }
1040+ } catch (final SecurityException e ) {
1041+ log (Log .ERROR , e ::getLocalizedMessage );
1042+ return false ;
10281043 }
10291044 }
10301045 return false ;
@@ -1242,7 +1257,12 @@ private boolean internalBeginReliableWrite() {
12421257
12431258 log (Log .VERBOSE , () -> "Beginning reliable write..." );
12441259 log (Log .DEBUG , () -> "gatt.beginReliableWrite()" );
1245- return reliableWriteInProgress = gatt .beginReliableWrite ();
1260+ try {
1261+ return reliableWriteInProgress = gatt .beginReliableWrite ();
1262+ } catch (final SecurityException e ) {
1263+ log (Log .ERROR , e ::getLocalizedMessage );
1264+ return false ;
1265+ }
12461266 }
12471267
12481268 private boolean internalExecuteReliableWrite () {
@@ -1255,7 +1275,12 @@ private boolean internalExecuteReliableWrite() {
12551275
12561276 log (Log .VERBOSE , () -> "Executing reliable write..." );
12571277 log (Log .DEBUG , () -> "gatt.executeReliableWrite()" );
1258- return gatt .executeReliableWrite ();
1278+ try {
1279+ return gatt .executeReliableWrite ();
1280+ } catch (final SecurityException e ) {
1281+ log (Log .ERROR , e ::getLocalizedMessage );
1282+ return false ;
1283+ }
12591284 }
12601285
12611286 private boolean internalAbortReliableWrite () {
@@ -1266,15 +1291,20 @@ private boolean internalAbortReliableWrite() {
12661291 if (!reliableWriteInProgress )
12671292 return false ;
12681293
1269- log (Log .VERBOSE , () -> "Aborting reliable write..." );
1270- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
1271- log (Log .DEBUG , () -> "gatt.abortReliableWrite()" );
1272- gatt .abortReliableWrite ();
1273- } else {
1274- log (Log .DEBUG , () -> "gatt.abortReliableWrite(device)" );
1275- gatt .abortReliableWrite (gatt .getDevice ());
1294+ try {
1295+ log (Log .VERBOSE , () -> "Aborting reliable write..." );
1296+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
1297+ log (Log .DEBUG , () -> "gatt.abortReliableWrite()" );
1298+ gatt .abortReliableWrite ();
1299+ } else {
1300+ log (Log .DEBUG , () -> "gatt.abortReliableWrite(device)" );
1301+ gatt .abortReliableWrite (gatt .getDevice ());
1302+ }
1303+ return true ;
1304+ } catch (final SecurityException e ) {
1305+ log (Log .ERROR , e ::getLocalizedMessage );
1306+ return false ;
12761307 }
1277- return true ;
12781308 }
12791309
12801310 @ Deprecated
0 commit comments