Ubuntu – How to 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:




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

Best Answer

  • 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:

        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
    while (sleep 1)
    connected=`sudo hidd --show` > /dev/null
    if [[ ! $connected =~ .*${address}.* ]] ; then
    sudo hidd --connect ${address} > /dev/null 2>&1

    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.

    /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.