Here we enter the murky world known colloquially, by me, as “Oh, Bluetooth, why do you mock me?”
This has been the biggest pain in the butt to get working, but hey, no-one ever said that the RPi was going to be easy.
Nice logo, isn’t it. Took me all of 30 seconds to search for it on Google images.
Installing the Bluetooth stack
Let’s remember, folks, Bluez is the official bluetooth stack for Linux! So it should work on everything, yes? Yeah, right. Here we go with the steps…
- Start the Pi, login as root.
- apt-get install bluetooth bluez-utils blueman
- From left to right, that’s:
- The stack. (A very large thing indeed)
- Command line utils. (A very useful thing indeed)
- X-windows interface. (A very slow thing indeed).
- Go and make yourself a coffee. This takes ages, especially over wi-fi. In fact, you could probably go and cook dinner.
Scanning and connecting
Onto the good bit. Getting your bluetooth device connected. In my case, I have a Rii bluetooth keyboard/touchpad.
My one came with a nano USB bluetooth dongle. In other words, it’s very small. Which is good.
- Okay, so you have the stack installed.
- Turn your Rii on and put it in discoverable mode.
- On your Pi, type “hcitool scan” to see if you can find your device.
- You should get a list of devices back in your area. The Rii comes up as “Macro Keyboard”. A stunning name, to be sure.
- Make a careful note of the string of numbers, colons and letters.
- Do the following to create a connection script:
- cd ~
- mkdir bin
- cd bin
- Create a file called auto_connect_bluetooth.sh and open it in your editor. Add the following lines. Put the string of numbers, colons and letters that you noted down before instead of XX:XX etc.
#!/bin/sh/usr/bin/bluez-test-device trusted XX:XX:XX:XX:XX:XX yes/usr/bin/bluez-test-input connect XX:XX:XX:XX:XX:XX
- Save the file and exit from the editor.
- chmod +x auto_connect_bluetooth.sh
- (This makes the file executable so you can do the next bit).
- Open /etc/rc.local in the editor. Add the following command before the exit command at the end of the file.
- sudo -u root /root/bin/auto_connect_bluetooth.sh
- This should run your connection script before the login prompt comes up.
- Before you do a reboot, make sure your bluetooth device is still in discoverable mode.
- Reboot and see if your device connects!
- Well… it doesn’t.
- You need to go into X windows (startx) and use the blueman-manager applet to pair up the device, trust it and connect to the input service.
- Once you’ve done that, and reboot, and it should connect this time.