1

I'm trying to capture events from a BLE HID, and I've been getting this error when running gatt.setCharacteristicNotification(characteristic, true); inside onServicesDiscovered, which can't be easily resolved as BLUETOOTH_PRIVILEGED is reserved for system-level apps. Does anyone know what I'm doing wrong? Here's my onServicesDiscovered function:

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            List<BluetoothGattService> services = gatt.getServices();
            for(int i = 0; i < services.size(); i++){
                List<BluetoothGattCharacteristic> characteristics = services.get(i).getCharacteristics();
                for(int j = 0; j < characteristics.size(); j++){
                    //Register all characteristics for all services

                    BluetoothGattService service = services.get(i);
                    if (service != null) {
                        BluetoothGattCharacteristic characteristic = characteristics.get(j);
                        if (characteristic != null) {
                            try {
                                gatt.setCharacteristicNotification(characteristic, true);
                            }
                            catch(Exception e){
                                Log.d(TAG, "EXCEPTION: " + e);
                            }
                        }
                    }
                }
            }
        } else {
            Log.w(TAG, "onServicesDiscovered received: " + status);
        }
    }

I've made sure Bluetooth + Location are enabled, and have added the following lines to my AndroidManifest.xml:

 <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Alex
  • 19
  • 5
  • 1
    What Android version? Do you request the runtime BLE permissions in your app? Please also consider adding the error message as text in your question. – Kozmotronik Jun 12 '23 at 05:37
  • Please keep in mind that `setCharacteristicNotification` is not enough to enable notifications since it only prepares your android device to receive them but does not activate them on the BLE device. You need to set the characteristics descriptor as well: https://stackoverflow.com/questions/32184536/enabling-bluetooth-characteristic-notification-in-android-bluetooth-low-energy – Michael Kotzjan Jun 12 '23 at 13:05

1 Answers1

0

The error message is correct. You need privileged bluetooth permissions in order to receive HID notifications. Otherwise any app using bluetooth could also potentially sniff passwords pressed on a connected bluetooth keyboard.

The privileged bluetooth permission is only given to system apps.

So what you are trying to achieve is not possible on Android. If you write the reasons why you need to capture BLE HID events, maybe we can give you alternative solutions.

Emil
  • 16,784
  • 2
  • 41
  • 52
  • Sure- I'm trying to capture events from cheap BLE smart rings ([example](https://amazon.com/Bluetooth-Control-Fingertip-Controller-Chargeable/dp/B09X16B2KT)) to be used with smart glasses. It also needs to run in the background, or when the phone's screen is locked, which would require an accessibility service to read the KeyEvents, as opposed to just reading the BLE data directly. I actually tried the accessibility approach at first, but the emulated swiping up/down had the same event codes, making it unusable. Perhaps there's a way to differentiate the direction of "mouse" swipes? – Alex Jun 13 '23 at 07:24