NordicSemiconductor / Android-BLE-Library

A library that makes working with Bluetooth LE on Android a pleasure. Seriously.
BSD 3-Clause "New" or "Revised" License
1.99k stars 414 forks source link

Catching read and write errors #518

Closed philips77 closed 11 months ago

philips77 commented 11 months ago

This PR fixes #507.

Reading or writing characteristics or descriptors with some restricted UUIDs may throw SecurityExeception. This PR adds a try-catch block. The error returned to the user is FailCallback.REASON_NULL_ATTRIBUTE.

philips77 commented 11 months ago

Btw, the same error is returned when the BluetoothGattCharacteristic or BluetoothGattDescriptor instances are null, or they don't have required property (e.g no READ property when trying to read).

j0bro commented 11 months ago

We have seen this crash in the field on several phone models (see below) running Android 13 as well, but crashlytics shows it happening when registering for notifications only, not the read/write operations. Would this improvement fix that as well? Here is the stacktrace of such an occurrence:

Fatal Exception: java.lang.SecurityException: Need BLUETOOTH PRIVILEGED permission: Neither user 10258 nor current process has android.permission.BLUETOOTH_PRIVILEGED.
       at android.app.ContextImpl.enforce(ContextImpl.java:2240)
       at android.app.ContextImpl.enforceCallingOrSelfPermission(ContextImpl.java:2268)
       at android.content.ContextWrapper.enforceCallingOrSelfPermission(ContextWrapper.java:948)
       at com.android.bluetooth.Utils.enforceBluetoothPrivilegedPermission(Utils.java:411)
       at com.android.bluetooth.gatt.GattService.permissionCheck(GattService.java:480)
       at com.android.bluetooth.gatt.GattService.registerForNotification(GattService.java:3807)
       at com.android.bluetooth.gatt.GattService$BluetoothGattBinder.registerForNotification(GattService.java:1091)
       at com.android.bluetooth.gatt.GattService$BluetoothGattBinder.registerForNotification(GattService.java:1079)
       at android.bluetooth.IBluetoothGatt$Stub.onTransact(IBluetoothGatt.java:812)
       at android.os.Binder.execTransactInternal(Binder.java:1285)
       at android.os.Binder.execTransact(Binder.java:1244)

Device distribution: image

WYZEHanYu commented 9 months ago

@philips77 My problem is the same as @j0bro 's. Can you solve it?

ghost commented 9 months ago

@philips77 Can you please catch the same exception on the call of the gatt.setCharacteristicNotification? It is called into internalEnableIndications, internalDisableNotifications, internalEnableNotifications functions.