Search code examples
bluetoothraspberry-pibluezraspberry-pi-zero

Making linux into a bluetooth keyboard (HID)


I've got a Raspberry Pi which I want to have sends keystrokes via BT to a connected device. The pi comes with some BT stack already in place, but when I try to connect to the device it comes up as an audio device.

  1. What do I have to do to get the Pi to present as a HID?
  2. Assume I have the keystroke to send from a process on the Pi, how do I send it to the remote device? Probably the easiest way conceptually would be for me to write the bytes out a socket connection, and the would wind up at the remote. But I don't understand the BT stack enough.
  3. Preferably, this HID keyboard would only work when the application is running, and not limit the Pi's other BT functions.

On Arduino (not what this is about) here are functions to send a keystroke, on Linux there are APIs fo the various USB device classes. Not sure what there is on linux. I've only played with the cli utility.


Solution

  • For Bluetooth there are a couple of ways HID devices are implemented.

    The Linux Bluetooth Stack can implement this selecting different profiles using the D-Bus API documented at: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/profile-api.txt

    A while ago I did an experiment to create a HID keyboard service with Python on a RPi: https://gist.github.com/ukBaz/a47e71e7b87fbc851b27cde7d1c0fcf0