Search code examples
pythonpython-3.xubuntubluetoothkeyboard

How can I connect a bluetooth keyboard without it being a system keyboard?


I want to connect a bluetooth keyboard to my computer but I don't want to use it as a normal HID device (so if i press keys the characters don't get typed). Is it possible to see in Python which keys are pressed exactly?
(I want to assign functions to each key)

I'm on Ubuntu and I'm using Python 3. EDIT: Going to device -> Info -> UUIDs shows this:

00001000-0000-1000-8000-00805f9b34fb ServiceDiscoveryServerServiceClassID 00001124-0000-1000-8000-00805f9b34fb Human Interface Device Service (HID) 00001200-0000-1000-8000-00805f9b34fb PnP Information

So I think it is a HID keyboard.


Solution

  • By default bluetoothd will handover a Bluetooth HID device to the kernel.

    You can disable this behavior either by

    1. rebuilding BlueZ with hid and/or hog profiles disabled
    2. start bluetoothd without the input plugin

    There are more details on rebuilding with --disable-hid and --disable-hog in https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/README#n198

    To remove the input plugin, then modify /lib/systemd/system/bluetooth.service so the ExecStart line has --noplugin=input added. E.g.:

    ExecStart=/usr/lib/bluetooth/bluetoothd --noplugin=input

    To be clear, the above will disable all Bluetooth HID devices being used by the system.

    If you leave it so BlueZ hands over the HID device to the system, then it will create an entry in /dev/input/. You can access information about what keys are pressed with the python-evdev library. More information at: https://python-evdev.readthedocs.io/