-1

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.

xnsdfgr
  • 3
  • 2

1 Answers1

0

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/

ukBaz
  • 6,985
  • 2
  • 8
  • 31
  • Disabling the input plugin worked, thank you. But how do I access the keyboard now in Python? – xnsdfgr Jul 20 '21 at 11:37
  • That will depend a little bit if it is a HID or a HoG keyboard. There are some examples in the BlueZ source tree. Not directly HID or HoG but the basic structure of how you access things. https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test/test-profile and https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test/example-gatt-client. The API docs are at: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc – ukBaz Jul 20 '21 at 12:40
  • I do not understand what this file [https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test/test-profile](https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test/test-profile) does. What is it for? Also, when I try to connect my keyboard with blueman now it connects and then disconnects all the time. How do I prevent that? – xnsdfgr Jul 22 '21 at 17:47
  • The reason the keyboard disconnects is because you have removed the plugin/client to handle the service being offered by the keyboard so BlueZ disconnects. If you update your question with if you have a HID or HID over GATT keyboard then we can maybe give you some more specific instructions. Did you investigate leaving `input` running and using `evdev` to capture the key presses? That would be the easier route – ukBaz Jul 22 '21 at 18:17
  • I updated the question now. How do I investigate using `evdev`? I tried using `python3-evdev` but could not find a device file for the bluetooth keyboard. – xnsdfgr Jul 23 '21 at 15:57
  • To use `evdev` you needs to not use `--noplugin=input`. Then when you pair and connect the keyboard it will appear. – ukBaz Jul 23 '21 at 16:25
  • 1
    Now it works I found another possibility to disable the input: In python, I can do: `device = evdev.InputDevice("Your Device")` and then use `device.grab()` to disable input for all other applications. Thanks a lot! – xnsdfgr Jul 23 '21 at 16:49
  • I think it must be: ExecStart=/usr/lib/bluetooth/bluetoothd --noplugin=input,hog – Martin Jan 31 '23 at 20:39