Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save HeadHodge/2d3dc6dc2dce03cf82f61d8231e88144 to your computer and use it in GitHub Desktop.

Select an option

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

@nithin-raptee
Copy link

Hi! Thanks for the demo script. It runs properly, and I'm able to connect to my phone as well.
I verified my phone receives the letter 'M' but it doesn't receive the Volume UP command. But the script prints that it has been sent successfully.

Is there anything I've not configured properly?

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