---
layout: post
title:  "Get a Bluetooth keyboard work with Arch Linux"
date:   2016-07-07 14:49:18 +0200
categories: linux
---
<p>
I've recently got a Rapoo E6100. This is a minimal and space saving Bluetooth 3.0 keyboard. If you pair it with Windows 10, it will remain paired after reboot, giving the possibility to use it since the login screen. After installing the Bluetooth stack on my Arch via the <code>bluez</code> and <code>bluez-utils</code> packages I thought the pairing process would be as simple as Windows if I used the KDE GUI menus for Bluetooth management. That's not true. The keyboard, once paired, will reconnect automatically just after <code>plasmashell</code> loaded, leaving me without keyboard during the SDDM login screen and, of course, during a non-graphical session.
</p>

<p>
  As usual, i've searched help in the ArchWiki, founding <a href="https://wiki.archlinux.org/index.php/Bluetooth_keyboard">this</a> article. With that, i've succesfully reconnected my Bluetooth keyboard using the <code>bluetoothctl</code> utility. The next step was configuring the service for auto connection during boot. I've created the <code>btkbd.conf</code> and the <code>btkbd.service</code> files, enabling the last one with systemd. Let's give a look to the service file:
</p>

<p>
{% highlight bash linenos %}
[Unit]
Description=systemd Unit to automatically start a Bluetooth keyboard
Documentation=https://wiki.archlinux.org/index.php/Bluetooth_Keyboard
Requires=dbus-org.bluez.service
After=dbus-org.bluez.service
ConditionPathExists=/etc/btkbd.conf
ConditionPathExists=/usr/bin/hcitool
ConditionPathExists=/usr/bin/hciconfig

[Service]
Type=oneshot
EnvironmentFile=/etc/btkbd.conf
ExecStart=/usr/bin/hciconfig ${HCIDEVICE} up
# ignore errors on connect, spurious problems with bt?
# so start next command with -
ExecStart=-/usr/bin/hcitool cc ${BTKBDMAC}

[Install]
WantedBy=multi-user.target
{% endhighlight %}
</p>

<p>
Line 13 enables the Bluetooth dongle, and line 16 connects it to the keyboard we gave the mac address in <code>/etc/btkbd.conf</code>. This should work flawlessly, right? Of course it doesn't. The service starts before the <code>dbus-org.bluez.service</code> is loaded and fails. However, if the service is started manually after login the Bluetooth keyboard works. After hours of trying figuring out what was wrong I've almost asked for a return on Amazon! The last attempt I made was with sddm disabled and involved built from scratch service:
</p>

<p>
{% highlight bash linenos %}
[Unit]
Description=systemd Unit to automatically start a Bluetooth keyboard

[Service]
Type=oneshot
ExecStart=/bin/hciconfig hci0 up
ExecStart=/bin/hcitool cc 00:11:22:33:44:55

[Install]
WantedBy=bluetooth.target
{% endhighlight %}
</p>

<p>
This incredibly worked. I think the problem was that <code>multi-user.target</code> that needs to be reached earlier than <code>bluetooth.target</code>. I got rid of all the tidiness of the ArchWiki solution just to be sure that was not the problem, but I think you can use all of that just correcting <code>WantedBy=</code>. Currently I haven't an ArchWiki account nor a forum one, but as soon as I'll register I'll correct the article.
</p>

<p>
  Last thing: I discovered that my Bluetooth dongle is CSR 8510 A10 based so expect some ramblings about <a href="http://www.0xf8.org/2014/02/the-crux-of-finding-a-hid-proxy-capable-usb-bluetooth-adapter/">hid proxy</a>.
</p>
