Package: bluetooth-2-usb
Bluetooth to USBTable of Contents1. Introduction2. Features3. Requirements4. Installation4.1. Prerequisites4.2. Setup5. Usage5.1. Connection to target device / host5.1.1. Raspberry Pi 4 Model B5.1.2. Raspberry Pi Zero (2) W(H)5.2. Command-line arguments5.3. Consuming the API from your Python code6. Updating7. Uninstallation8. Troubleshooting8.1. The Pi keeps rebooting or crashes randomly8.2. The installation was successful, but I don't see any output on the target device8.3. In bluetoothctl, my device is constantly switching on/off8.4. I have a different issue8.5. Everything is working, but can it help me with Bitcoin mining?9. Bonus points10. Contributing11. License12. Acknowledgments1. IntroductionConvert a Raspberry Pi into a HID relay that translates Bluetooth keyboard and mouse input to USB. Minimal configuration. Zero hassle.The issue with Bluetooth devices is that you usually can't use them to:wake up sleeping devices,access the BIOS or OS select menu (e.g., GRUB),access devices without Bluetooth interface (e.g., devices in a restricted environment or most KVM switches).Sounds familiar? Congratulations!You just found the solution!Linux's gadget mode allows a Raspberry Pi to act as USB HID (Human Interface Device). Therefore, from the host's perspective, it appears like a regular USB keyboard or mouse. You may think of your Pi as a multi-device Bluetooth dongle.2. FeaturesSimple installation and highly automated setupSupports multiple input devices (currently keyboard and mouse - more than one of each kind simultaneously)Supports146 multimedia keys(e.g., mute, volume up/down, launch browser, etc.)Auto-discovery feature for input devicesAuto-reconnect feature for input devices (power off, energy saving mode, out of range, etc.)Robust error handling and loggingInstallation as a systemd serviceReliable concurrency using state-of-the-artTaskGroupsClean and actively maintained code base3. RequirementsA Raspberry Pi with Bluetooth andUSB OTG supportrequired forUSB gadgetsin so-called device mode. Supported models include:Raspberry Pi Zero W(H): Includes Bluetooth 4.1 and supports USB OTG with the lowest price tag.Raspberry Pi Zero 2 W: Similar to the Raspberry Pi Zero W, it has Bluetooth 4.1 and USB OTG support while providing additional processing power.Raspberry Pi 4 Model B: Offers Bluetooth 5.0 and USB-C OTG support for device mode, providing the best performance (that is until the Pi 5 is available).Linux OS with systemd support (e.g.,Raspberry Pi OS, recommended).Python 3.11 for usingTaskGroups.[!NOTE]
Raspberry Pi 3 Models feature Bluetooth 4.2 but no native USB gadget mode support. Earlier models like Raspberry Pi 1 and 2 do not support Bluetooth natively and have no USB gadget mode support.[!NOTE]
The latest version of Raspberry Pi OS, based on Debian Bookworm, supports Python 3.11 through the official package repositories. For older versions, you maybuild it from source. Note that building may take anything between a few minutes (Pi 4B) and more than an hour (Pi 0W).4. InstallationFollow these steps to install and configure the project:4.1. PrerequisitesInstall an OS on your Raspberry Pi (e.g., usingPi Imager)Connect to a network via Ethernet cable orWi-Fi. Make sure this network has Internet access.(optional, recommended) EnableSSH, if you intend to access the Pi remotely.[!NOTE]
These settings above may be configuredduring imaging(recommended),on first bootorafterwards.Connect to the Pi and make suregitis installed:sudo apt update && sudo apt upgrade -y && sudo apt install -y gitPair and trust any Bluetooth devices you wish to relay, either via GUI or via CLI:bluetoothctlscan on... wait for your devices to show up and note their MAC addresses (you may also type the first characters and hitTABfor auto-completion in the following commands) ...trust A1:B2:C3:D4:E5:F6pair A1:B2:C3:D4:E5:F6connect A1:B2:C3:D4:E5:F6[!NOTE]
ReplaceA1:B2:C3:D4:E5:F6by your input device's Bluetooth MAC address4.2. SetupOn the Pi, clone the repository to your home directory:cd ~ && git clone https://github.com/quaxalber/bluetooth_2_usb.gitRun the installation script as root:sudo ~/bluetooth_2_usb/scripts/install.shReboot:sudo rebootVerify that the service is running:service bluetooth_2_usb statusIt should look something like this and sayActive: active (running):user@pi0w:~ $servicebluetooth_2_usbstatus● bluetooth_2_usb.service - Bluetooth to USB HID relayLoaded: loaded (/etc/systemd/system/bluetooth_2_usb.service; enabled; preset: enabled)Active: active (running) since Wed 2023-12-13 10:33:00 CET; 44min agoMain PID: 5865 (bash)Tasks: 4 (limit: 389)CPU: 2min 49.448sCGroup: /system.slice/bluetooth_2_usb.service├─5865 bash /usr/bin/bluetooth_2_usb --auto_discover --grab_devices└─5869 python3.11 /home/user/bluetooth_2_usb/bluetooth_2_usb.py --auto_discover --grab_devicesDec 13 10:33:00 pi0w systemd[1]: Started bluetooth_2_usb.service - Bluetooth to USB HID relay.Dec 13 10:33:06 pi0w bluetooth_2_usb[5869]: 23-12-13 10:33:06 [INFO] Launching Bluetooth 2 USB v0.8.0Dec 13 10:33:06 pi0w bluetooth_2_usb[5869]: 23-12-13 10:33:06 [INFO] Discovering input devices...Dec 13 10:33:09 pi0w bluetooth_2_usb[5869]: 23-12-13 10:33:09 [INFO] Activated relay for device /dev/input/event2, name "AceRK Mouse", phys "0a:1b:2c:3d:4e:5f"Dec 13 10:33:09 pi0w bluetooth_2_usb[5869]: 23-12-13 10:33:09 [INFO] Activated relay for device /dev/input/event1, name "AceRK Keyboard", phys "0a:1b:2c:3d:4e:5f"Dec 13 10:33:09 pi0w bluetooth_2_usb[5869]: 23-12-13 10:33:09 [INFO] Activated relay for device /dev/input/event0, name "vc4-hdmi", phys "vc4-hdmi/input0"[!NOTE]
Something seems off? Try yourself inTroubleshooting!5. Usage5.1. Connection to target device / host5.1.1. Raspberry Pi 4 Model BConnect theUSB-C power portof your Pi 4B via cable with a USB port on your target device. You should hear the USB connection sound (depending on the target device) and be able to access your target device wirelessly using your Bluetooth keyboard or mouse. In case the Pi solely draws power from the host, it will take some time for the Pi to boot.[!IMPORTANT]
It's essential to use the small power port instead of the bigger USB-A ports, since only the power port has theOTGfeature required forUSB gadgets.5.1.2. Raspberry Pi Zero (2) W(H)For the Pi Zero, the situation is quite the opposite: Donotuse the power port to connect to the target device,usethe other port instead (typically labeled "DATA" or "USB"). You may connect the power port to a stable power supply.5.2. Command-line argumentsCurrently you can provide the following CLI arguments:user@pi0w:~ $bluetooth_2_usb-husage: bluetooth_2_usb.py [--device_ids DEVICE_IDS] [--auto_discover] [--grab_devices] [--list_devices] [--log_to_file] [--log_path LOG_PATH] [--debug] [--version] [--help]Bluetooth to USB HID relay. Handles Bluetooth keyboard and mouse events from multiple input devices and translates them to USB using Linux's gadget mode.options:--device_ids DEVICE_IDS, -i DEVICE_IDSComma-separated list of identifiers for input devices to be relayed.An identifier is either the input device path, the MAC address or any case-insensitive substring of the device name.Example: --device_ids '/dev/input/event2,a1:b2:c3:d4:e5:f6,0A-1B-2C-3D-4E-5F,logi'Default: None--auto_discover, -a   Enable auto-discovery mode. All readable input devices will be relayed automatically.Default: disabled--grab_devices, -g    Grab the input devices, i.e., suppress any events on your relay device.Devices are not grabbed by default.--list_devices, -l    List all available input devices and exit.--log_to_file, -f     Add a handler that logs to file, additionally to stdout.--log_path LOG_PATH, -p LOG_PATHThe path of the log fileDefault: /var/log/bluetooth_2_usb/bluetooth_2_usb.log--debug, -d           Enable debug mode (Increases log verbosity)Default: disabled--version, -v         Display the version number of this software and exit.--help, -h            Show this help message and exit.5.3. Consuming the API from your Python codeThe API is designed such that it may be consumed both via CLI and from within external Python code. More details on thiscoming soon!6. UpdatingYou may update to the latest stable release by running:sudo ~/bluetooth_2_usb/scripts/update.sh[!NOTE]
The update script performs a clean reinstallation, that is rununinstall.sh, delete the repo folder, clone again and run the install script. The current branch will be maintained.7. UninstallationYou may uninstall Bluetooth 2 USB by running:sudo ~/bluetooth_2_usb/scripts/uninstall.sh8. Troubleshooting8.1. The Pi keeps rebooting or crashes randomlyThis is likely due to the limited power the Pi can draw from the host's USB port. Try these steps:If available, connect your Pi to a USB 3 port on the host / target device (usually blue) or preferably USB-C.[!IMPORTANT]Do not usethe blue (or black) USB-A portsof your Pito connect.This won't work.Do usethe small USB-C power port (in case of Pi 4B). For Pi Zero, use the data port to connect to the host and attach the power port to a dedicated power supply.Try toconnect to the Pi via SSHinstead of attaching a display directly and remove any unnecessary peripherals.Install alite versionof your OS on the Pi (without GUI)For Pi 4B: Get aUSB-C Data/Power Splitterand draw power from a dedicated power supply. This should ultimately resolve any power-related issues, and your Pi 4B will no longer be dependent on the host's power supply.[!NOTE]
The Pi Zero is recommended to have a 1.2 A power supply for stable operation, the Pi Zero 2 requires 2.0 A and the Pi 4B even 3.0 A, while hosts may typically only supply up to 0.5/0.9 A through USB-A 2.0/3.0 ports. However, this may be sufficient depending on your specific soft- and hardware configuration. For more information see theRaspberry Pi documentation.8.2. The installation was successful, but I don't see any output on the target deviceThis could be due to a number of reasons. Try these steps:Verify that the service is running:service bluetooth_2_usb statusVerify that you specified the correct input devices inbluetooth_2_usb.serviceVerify that your Bluetooth devices are paired, trusted, connected andnotblocked:bluetoothctlinfo A1:B2:C3:D4:E5:F6It should look like this:user@pi0w:~ $bluetoothctlAgent registered[CHG] Controller 0A:1B:2C:3D:4E:5F Pairable: yes[AceRK]#infoA1:B2:C3:D4:E5:F6Device A1:B2:C3:D4:E5:F6 (random)Name: AceRKAlias: AceRKPaired: yes     <---Trusted: yes    <---Blocked: no     <---Connected: yes  <---WakeAllowed: noLegacyPairing: noUUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)UUID: Device Information        (0000180a-0000-1000-8000-00805f9b34fb)UUID: Human Interface Device    (00001812-0000-1000-8000-00805f9b34fb)UUID: Nordic UART Service       (6e400001-b5a3-f393-e0a9-e50e24dcca9e)[!NOTE]
ReplaceA1:B2:C3:D4:E5:F6by your input device's Bluetooth MAC addressReload and restart service:sudo systemctl daemon-reload && sudo service bluetooth_2_usb restartReboot Pisudo rebootRe-connect the Pi to the host and check that the cable is capable of transmitting data, not power onlyTry a different USB port on the hostTry connecting to a different host8.3. In bluetoothctl, my device is constantly switching on/offThis is a common issue, especially when the device gets paired with multiple hosts. One simple fix/workaround is to re-pair the device:bluetoothctlpower offpower onblock A1:B2:C3:D4:E5:F6remove A1:B2:C3:D4:E5:F6scan ontrust A1:B2:C3:D4:E5:F6pair A1:B2:C3:D4:E5:F6connect A1:B2:C3:D4:E5:F6If the issue persists, it's worth trying to delete the cache:sudo -icd '/var/lib/bluetooth/0A:1B:2C:3D:4E:5F/cache'rm -rf 'A1:B2:C3:D4:E5:F6'exit[!NOTE]
Replace0A:1B:2C:3D:4E:5Fby your Pi's Bluetooth controller's MAC andA1:B2:C3:D4:E5:F6by your input device's MAC8.4. I have a different issueHere's a few things you could try:Check the log files (default at/var/log/bluetooth_2_usb/) for errors[!NOTE]
Logging to file requires the-fflagYou may also query the journal to inspect the service logs in real-time:journalctl -u bluetooth_2_usb.service -n 50 -fFor easier degguging, you may temporarily stop the service and run the script manually, modifying arguments as required, e.g., increase log verbosity by appending-d:sudo service bluetooth_2_usb stop && sudo bluetooth_2_usb -ad ; sudo service bluetooth_2_usb startWhen you interact with your Bluetooth devices with-dset, you should see debug output in the logs such as:user@pi0w:~/bluetooth_2_usb $sudoservicebluetooth_2_usbstop&&sudobluetooth_2_usb-ihdmi,a1:b2:c3:d4:e5:f6,/dev/input/event3-d;sudoservicebluetooth_2_usbstart23-12-16 15:52:21 [DEBUG] CLI args: device_ids=['hdmi', 'a1:b2:c3:d4:e5:f6', '/dev/input/event3'], auto_discover=False, grab_devices=False, list_devices=False, log_to_file=False, log_path=/var/log/bluetooth_2_usb/bluetooth_2_usb.log, debug=True, version=False23-12-16 15:52:21 [DEBUG] Logging to stdout23-12-16 15:52:21 [INFO] Launching Bluetooth 2 USB v0.8.023-12-16 15:52:21 [INFO] Discovering input devices...23-12-16 15:52:21 [DEBUG] Relaying devices with matching name "hdmi" or MAC "a1:b2:c3:d4:e5:f6" or path "/dev/input/event3"23-12-16 15:52:21 [DEBUG] Initializing USB gadgets...23-12-16 15:52:24 [DEBUG] Enabled USB gadgets: [mouse gadget (/dev/hidg0), keyboard gadget (/dev/hidg1), consumer control gadget (/dev/hidg2)]23-12-16 15:52:24 [INFO] Activated relay for device /dev/input/event2, name "AceRK Mouse", phys "0a:1b:2c:3d:4e:5f"23-12-16 15:52:24 [INFO] Activated relay for device /dev/input/event1, name "AceRK Keyboard", phys "0a:1b:2c:3d:4e:5f"23-12-16 15:52:24 [INFO] Activated relay for device /dev/input/event0, name "vc4-hdmi", phys "vc4-hdmi/input0"### Manually switched Pi's Bluetooth off ###23-12-16 15:53:27 [CRITICAL] Connection to AceRK Keyboard lost [OSError(19, 'No such device')]23-12-16 15:53:27 [CRITICAL] Connection to AceRK Mouse lost [OSError(19, 'No such device')]### Manually switched Pi's Bluetooth back on ###23-12-16 15:53:31 [INFO] Activated relay for device /dev/input/event2, name "AceRK Mouse", phys "0a:1b:2c:3d:4e:5f"23-12-16 15:53:31 [INFO] Activated relay for device /dev/input/event1, name "AceRK Keyboard", phys "0a:1b:2c:3d:4e:5f"23-12-16 15:54:20 [DEBUG] Received event at 1702738460.417525, code 04, type 04, val 458827 from AceRK Keyboard23-12-16 15:54:20 [DEBUG] Received key event at 1702738460.417525, 104 (KEY_PAGEUP), down from AceRK Keyboard23-12-16 15:54:20 [DEBUG] Converted evdev scancode 0x68 (KEY_PAGEUP) to HID UsageID 0x4B (PAGE_UP)23-12-16 15:54:20 [DEBUG] Pressing PAGE_UP (0x4B) on keyboard gadget (/dev/hidg1)23-12-16 15:54:20 [DEBUG] Received synchronization event at 1702738460.417525, SYN_REPORT from AceRK Keyboard23-12-16 15:54:20 [DEBUG] Received event at 1702738460.466388, code 04, type 04, val 458827 from AceRK Keyboard23-12-16 15:54:20 [DEBUG] Received key event at 1702738460.466388, 104 (KEY_PAGEUP), up from AceRK Keyboard23-12-16 15:54:20 [DEBUG] Converted evdev scancode 0x68 (KEY_PAGEUP) to HID UsageID 0x4B (PAGE_UP)23-12-16 15:54:20 [DEBUG] Releasing PAGE_UP (0x4B) on keyboard gadget (/dev/hidg1)23-12-16 15:54:20 [DEBUG] Received synchronization event at 1702738460.466388, SYN_REPORT from AceRK Keyboard23-12-16 15:54:34 [DEBUG] Received event at 1702738474.116380, code 04, type 04, val 786665 from AceRK Keyboard23-12-16 15:54:34 [DEBUG] Received key event at 1702738474.116380, 115 (KEY_VOLUMEUP), down from AceRK Keyboard23-12-16 15:54:34 [DEBUG] Converted evdev scancode 0x73 (KEY_VOLUMEUP) to HID UsageID 0xE9 (VOLUME_INCREMENT)23-12-16 15:54:34 [DEBUG] Pressing VOLUME_INCREMENT (0xE9) on consumer control gadget (/dev/hidg2)23-12-16 15:54:34 [DEBUG] Received synchronization event at 1702738474.116380, SYN_REPORT from AceRK Keyboard23-12-16 15:54:34 [DEBUG] Received event at 1702738474.117192, code 04, type 04, val 786665 from AceRK Keyboard23-12-16 15:54:34 [DEBUG] Received key event at 1702738474.117192, 115 (KEY_VOLUMEUP), up from AceRK Keyboard23-12-16 15:54:34 [DEBUG] Converted evdev scancode 0x73 (KEY_VOLUMEUP) to HID UsageID 0xE9 (VOLUME_INCREMENT)23-12-16 15:54:34 [DEBUG] Releasing VOLUME_INCREMENT (0xE9) on consumer control gadget (/dev/hidg2)23-12-16 15:54:34 [DEBUG] Received synchronization event at 1702738474.117192, SYN_REPORT from AceRK Keyboard23-12-16 15:54:36 [DEBUG] Received event at 1702738476.895033, code 04, type 04, val 589826 from AceRK Mouse23-12-16 15:54:36 [DEBUG] Received key event at 1702738476.895033, 273 (BTN_RIGHT), down from AceRK Mouse23-12-16 15:54:36 [DEBUG] Converted evdev scancode 0x111 (BTN_RIGHT) to HID UsageID 0x02 (RIGHT)23-12-16 15:54:36 [DEBUG] Pressing RIGHT (0x02) on mouse gadget (/dev/hidg0)23-12-16 15:54:36 [DEBUG] Received synchronization event at 1702738476.895033, SYN_REPORT from AceRK Mouse23-12-16 15:54:36 [DEBUG] Received event at 1702738476.943781, code 04, type 04, val 589826 from AceRK Mouse23-12-16 15:54:36 [DEBUG] Received key event at 1702738476.943781, 273 (BTN_RIGHT), up from AceRK Mouse23-12-16 15:54:36 [DEBUG] Converted evdev scancode 0x111 (BTN_RIGHT) to HID UsageID 0x02 (RIGHT)23-12-16 15:54:36 [DEBUG] Releasing RIGHT (0x02) on mouse gadget (/dev/hidg0)23-12-16 15:54:36 [DEBUG] Received synchronization event at 1702738476.943781, SYN_REPORT from AceRK Mouse23-12-16 15:54:37 [DEBUG] Received relative axis event at 1702738477.675038, REL_X from AceRK Mouse23-12-16 15:54:37 [DEBUG] Moving mouse gadget (/dev/hidg0) (x=125, y=0, mwheel=0)23-12-16 15:54:37 [DEBUG] Received synchronization event at 1702738477.675038, SYN_REPORT from AceRK Mouse^C23-12-16 15:54:50 [INFO] Received signal: SIGINT, frame: <frame at 0xb5ec9930, file '/usr/lib/python3.11/selectors.py', line 468, code select>23-12-16 15:54:50 [CRITICAL] vc4-hdmi was cancelled23-12-16 15:54:50 [CRITICAL] AceRK Keyboard was cancelled23-12-16 15:54:50 [CRITICAL] AceRK Mouse was cancelledStill not resolved? Double-check theinstallation instructionsFor more help, open anissuein theGitHub repository8.5. Everything is working, but can it help me with Bitcoin mining?Absolutely!Here's how.9. Bonus pointsAfter successfully setting up your Pi as a HID proxy for your Bluetooth devices, you may consider makingRaspberry Pi OS read-only. That helps preventing the SD card from wearing out and the file system from getting corrupted when powering off the Raspberry forcefully.10. ContributingContributions are welcome! Please read theCONTRIBUTING.mdfile for guidelines.11. LicenseThis project is licensed under the MIT License - see theLICENSEfile for details."Bluetooth 2 HID" image@PixelGordois licensed under aCreative Commons Attribution-NonCommercial 4.0 International License.12. AcknowledgmentsMike Redrobefor the idea and the basic code logic andHeuristicPerson's bluetooth_2_hidbased off this.Georgi Valkovforpython-evdevmaking reading input devices a walk in the park.The folks atAdafruitforCircuitPython HIDandBlinkaproviding super smooth access to USB gadgets.Special thanks to the open-source community for various other libraries and tools.