Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save HeadHodge/2d3dc6dc2dce03cf82f61d8231e88144 to your computer and use it in GitHub Desktop.
Save HeadHodge/2d3dc6dc2dce03cf82f61d8231e88144 to your computer and use it in GitHub Desktop.
Bluez HID over GATT (HOGP) Keyboard Emulator Example

Bluez HID over GATT (HOGP) Keyboard Emulator Example

A working example of a BLE Keyboard Peripheral (Server) ran on my Raspberry PI4-b with Buster Raspbian-Lite for all to use. My desire is to hopefully save at least one other person the literal weeks I've spent trying to get this working for my little home automation project.

Verified working connected to my Blade Android, Windows 10 laptop, and Amazon FireTV stick devices

Once running and connectted properly to a client device, it will update the battery status with a lower percentage every 60 seconds, use the Keyboard Input Report to send a capital 'M' every 10 secomds, and use the Consumer Input Report to send a 'volumeUp' every 15 seconds.

TIPS

Tip #1:

Ensure you use Bluez Version 5.56 or better. This example uses Bluez features that were only experimental in prior versions.

Tip #2:

The PI4 ships with a dual bluetooth chip. You must ensure you pair devices in BLE mode. To help, change this option in the /etc/bluetooth/main.conf file:

#Restricts all controllers to the specified transport. Default value
#is "dual", i.e. both BR/EDR and LE enabled (when supported by the HW).
#Possible values: "dual", "bredr", "le"
#ControllerMode = dual
#ControllerMode = bredr
ControllerMode = le

Tip #3:

Once you have this example running, use the Bluetoothctl command to make sure your bluetooth controller's 'power' option is on and that the 'advertise' option is on. This will advertise your bluetooth controller to other bluetooth client devices in order to discover and pair with your server. You can write your own GAD service to programically advertise your device, but it's not required if you properly use the BluetoothCtl or BtMgmt tools that ship with Bluez.

Tip #4:

I have not tested this yet.... But to eliminate the questions during pairing, try setting the Bluez default-agent option to 'noInputnoOutput'. Also use the Bluetoothctl command to 'trust' the client device your connecting with.

gaatServer.py:

https://github.com/HeadHodge/Bluez-HID-over-GATT-Keyboard-Emulator-Example/blob/main/gattServer.py

@HeadHodge
Copy link
Author

Hi All,

I wrote this for my own personal use for my little home automation project. As such, no support for this example is available, but I'll be happy to answer any questions I can.

Regards

@WeijenH
Copy link

WeijenH commented Aug 23, 2023

Hi I have some related question. For esp32, is it possible to write BLEkeyboard code with GATT?

@joyhope
Copy link

joyhope commented Dec 16, 2023

The demo is very helpful. I run the demo on my Linux desktop. It is unlucky to find the simulator device by my Android phone.

  1. I setup the main.conf ControllerMode to le.
  2. bluetoothctl config the advertise on. (discoverable is also on)
  3. bluetoothctl show command indicate there is HID profile (confirmed the application success running.)

Is there anything I missing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment