Android: Using HID over GATT directly in Application (Gadget=HID → control Application)

Viewed 2298

I have a Bluetooth LE Joystick with a couple of buttons. I want to connect it directly to the app without using the Bluetooth Settings, but by connecting to it via BluetoothLeScanner.

I already can connect to a Heart Rate Monitor and get periodic notifications of the HR-values.

I also have created a little temperature/light/humidity sensor with an ESP32 to which I can also connnect and display the sensor values in the app.

My understanding of this HID device may be erroneous. With the help of the App BLE Scanner I can connect to this gadget via BLE and see the offered services.

One of them is 0x1812 HUMAN INTERFACE DEVICE, which contains a couple of characteristics of type REPORT, among others which are described HID OVER GATT PROFILE SPECIFICATION (HOGP_SPEC)

How can I find out which button got pressed, what position the joystick has been moved to? I guess that this has to occur via notifications to which I have to subscribe to.

Is there any Android library avaliable to handle HID input devices? What I do NOT want to do is to use the Bluetooth settings and connect to the device and have it become detected as a HID device and replace the keyboard with some odd mapping. I don't want the OS to get involved at all, just like when I connect and use the HRM and the ESP32.

I couldn't find any information on this.

I can connect to the device with my app, enumerate the services and characteristics. But from there on, I have no clue how to work with HID. Ideally I'd get notifications like "Button A has been pressed" "Joystick got moved to x=32" where x would range from -127 to 127, for example.

1 Answers

Accessing the HID characteristics requires the BLUETOOTH_PRIVILEGED permission since Android 5.0. The only way for an app to obtain that permission is to be signed with the same certificates as system apps.

This is a security feature, to prevent an app from receiving HID events from your keyboard and stealing your passwords.

Quoting from https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/BLE_5F00_on_5F00_Android_5F00_v1.0.1.pdf

4.6 Protected services

Android does not allow to read or write data to the following characteristics (and their descriptors):

  1. HID Service (since Android 5):
a.  HID Information
b.  Report Map
c.  HID Control Point
d.  Report
  1. FIDO (https://fidoalliance.org/) (since Android 6)
a.  U2F (0000FFFD-0000-1000-8000-00805F9B34FB)

Only applications with BLUETOOTH_PRIVILEGED, that is, applications signed with the same certificate as the system, are able to read and write data to those characteristics.

Related