Raspbian - Connecting a bluetooth keyboard using the command line


So... I initially had some issues attaching my bluetooth keyboard to my Pi, many articles out there suggested carrying this out using a graphical bluetooth manager, however, I didn't have a spare USB keyboard or mouse that I could "bootstrap" the process with. I finally managed to get it attached using ssh to connect to the command line with the following.


Firstly, we need to install bluetooth and bluez-utils as follows:

apt-get install bluetooth bluez-utils

This may take some time since it installs quite a few printer ppd files for bluetooth printers. Once complete we need to check that our bluetooth device has been recognised:

# hcitool dev
        hci0    00:09:DD:50:8D:98

This shows that the MAC address of our bluetooth dongle. We can also scan for bluetooth devices that are in range as follows:

# hcitool scan
Scanning ...
        20:42:06:20:04:DF       Bluetooth Keyboard
        00:1D:4F:93:DB:E9       Steven’s Computer

This shows the MAC address and name of the bluetooth keyboard that we wish to use.

Now we need to edit '/etc/default/bluetooth' and enable the option 'HID2HCI_ENABLED', this enables the dongle to use Human Interface Devices - HID - as a Host Controller Interface, thus allowing the dongle to use bluetooth functionality rather than just HID functionality.


We then need to add the following to '/etc/bluetooth/hcid.conf', this file provides the options needed by the Bluetooth Host Controller Interface daemon. See the hcid.conf man page for additional information.

device $(20:42:06:20:04:DF) {
        name "Bluetooth Keyboard";
        auth enable;
        encrypt enable;

Once complete we then need to restart bluetooth as follows:

# /etc/init.d/bluetooth restart 
[ ok ] Stopping bluetooth: rfcomm /usr/sbin/bluetoothd. 
[ ok ] Starting bluetooth: bluetoothd switching to HID/HCI no longer done in init script, see /usr/share/doc/bluez/NEWS.Debian.gz rfcomm.

We are now ready to pair our bluetooth keyboard using the 'bluez-simple-agent' command, this will then ask us for a PIN code for the device, once entered in the terminal we will then need to enter the same PIN on the keyboard, 'bluez-simple-agent' will then confirm the device has been paired. NOTE: I believe there are instances where the keyboard will have a default PIN code, in which case, use this instead.

# bluez-simple-agent hci0 20:42:06:20:04:DF
RequestPinCode (/org/bluez/1841/hci0/dev_20_42_06_20_04_DF)
Enter PIN Code: 0000
New device (/org/bluez/1841/hci0/dev_20_42_06_20_04_DF)

Finally we connect to the device using:

# bluez-test-input connect 20:42:06:20:04:DF


Reconnect on reboot


Many sources suggested that the method presented above is adequate, and that this should allow the device reconnect automatically on startup... this was not the case with my tests. I believe that this is caused by the recent changes in the management of HID devices, in particular, the use of the 'HID2HCI_ENABLED' and the fact that "bluetoothd switching to HID/HCI no longer done in init script". From what I can see this error shown above is due to our init script trying to establish the HID2HCI interface in the init script.

Watch this space...

Additional commands and notes

Along with the above command I also found that we can make the device trusted with the following command (Before we connect using 'bluez-test-input'). However, when I tried this option I found that the device would not work after connecting.

 bluez-test-device trusted 20:42:06:20:04:DF yes

To Disconnect a device:

bluez-test-input disconnect 20:42:06:20:04:DF

To forget a device:

bluez-simple-agent hci0 20:42:06:20:04:DF remove


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Enter the characters shown in the image.
By submitting this form, you accept the Mollom privacy policy.