How can I have a Bluetooth Keyboard Auto-Connect at Startup?

I have a bluetooth keyboard for my HTPC. I pair it with my computer via bluetooth, and it works fine. However, when I turn off the keyboard or my computer, it has to connect again (which requires me entering a passcode on the computer and keyboard) when I turn them back on. I've looked through many forums and through that I have found that I'm missing some seemingly vital bluetooth files:

/etc/default/bluetooth

/etc/init.d/bluetooth

/etc/bluetooth/hcid.conf

In the instructions that I've found online so far, I must change certain lines in any of these files. I am unable to, since they do not exist.

Am I missing a dependency or something? I've also tried installing Blueman but that didn't help at all.

I'm running Ubuntu 10.10 (Maverick).

Original post: http://ubuntuforums.org/showthread.php?t=1643386&highlight=bluetooth


This answer comes from a tutorial written by Andrew Martin on thetechdepo.com. See the link below for the original post, however this is a complete rewrite. It is written specifically for an apple keyboard, however I have used it for multiple devices so it is not apple, or keyboard specific. Simply follow the instructions and you will have any bluetooth device auto connect in Ubuntu.

Go to System Settings > Bluetooth and follow the instructions to add your newly connected bluetooth device.

Follow the instructions and enter the pin when requested, then after the device is connected and working, power off the device. Now onto the auto connect part.

Open your terminal and enter the following commands:

$ killall bluetooth-applet 
$ sudo /etc/init.d/bluetooth restart 
$ sudo hcitool dev

The last command will display the output for your device such as:

Devices:
    hci0    00:00:00:00:00:00

Install a new package to allow you to connect bluetooth via the command line.

$ sudo apt-get install bluez-compat

Once this has completed, you will now have hidd program installed. At this point, turn on your keyboard and enter the following command. It will output the MAC Address of your keyboard, which you will need to copy and paste into the subsequent command, as seen below:

$ hcitool scan        
Scanning ...
    AA:BB:CC:DD:EE:FF   Bluetooth Device Name
$ sudo hidd --connect AA:BB:CC:DD:EE:FF

Now we have to create a startup file to auto connect the device when the pc starts. Via the terminal...

$ gedit ~/.keyboard.sh

Enter the following text, substituting AA:BB:CC:DD:EE:FF with the hardware ID you saw above. Finally, save the file.

#! /bin/bash

address="AA:BB:CC:DD:EE:FF"

while (sleep 1)
do
connected=`sudo hidd --show` > /dev/null
if [[ ! $connected =~ .*${address}.* ]] ; then
sudo hidd --connect ${address} > /dev/null 2>&1
fi
done

Now create a new startup file. Substitute "keyboard" for whatever device name you choose. This is simply the name of the file.

$ sudo gedit /etc/init.d/keyboard

Paste the following, which tells your Ubuntu to run the file you just created at startup.

#!/bin/sh
/home/username/.keyboard.sh &

exit 0

Now set both files permissions to executable via the terminal.

$ sudo chmod +x /etc/init.d/keyboard
$ chmod +x ~/.keyboard.sh
$ sudo update-rc.d keyboard defaults

Restart your computer and your device should auto connect and be working without you having to do anything.

I hope this helps a few people, it took me a long time to find a solution to my problem and I wanted to cement this tutorial a little deeper into the web.

The original link can be found here.


I also tried ryandlf's answer by myself but found another easy setup which will eventually do the same work.

Main idea is that once the remote device is paired and trusted, hcitool spinq will initiate periodic inquiry which will let every available devices be connected.

First, initiate sudo Bluetoothctl -a and follow steps below. These are from ArchLinux wiki but proven to work same in Ubuntu as well.

[bluetooth]# power on
[bluetooth]# agent KeyboardOnly
[bluetooth]# pairable on
[bluetooth]# scan on

Last command will show all available (waiting for pairing) bluetooth keyboards like this.

Discovery started
[CHG] Controller 01:02:03:04:05:06 Discovering: yes

Note that address of your keyboard and continue with it.

[bluetooth]# pair 01:02:03:04:05:06
[bluetooth]# trust 01:02:03:04:05:06
[bluetooth]# connect 01:02:03:04:05:06
[bluetooth]# quit

Now put this simple script into /etc/init.d/keyboard

#! /bin/sh
sudo hcitool spinq
exit 0

Then execute these.

sudo chmod +x /etc/init.d/keyboard
sudo service keyboard enable
sudo update-rc.d keyboard defaults
sudo service keyboard start

That's it. Now whenever you turn your Bluetooth keyboard on, it'll be connected to your Linux automatically.


I had a very similar issue with an Apple BlueTooth keyboard. I bought it because I loved the feeling of the keys. However, I could not keep it connected. The problem seemed to be in the keyboard identification. Once it was matched to an actual MAC computer, where it got an actual name, I no more had any issue with connecting it to Linux or Windows. My wife now uses it with no problem.

I don't know how this can be transposed in your situation, but this can be a starting point. From other friends, it seems that the device identification is the main issue here.