Bluetooth on the Raspberry PIPosted: June 1, 2016
For anyone with a Raspberry Pi3, this topic may or may not be useful. If like me you own a Raspberry Pi2, you may be facing the challenge of using Bluetooth with your Pi and wondering how on earth you get things started. Depending on how Linux savvy you are, this can be either a walk in the park or a utter nightmare that will leave your pulling your hair out and crying in a corner.
Now thanks to a good friend introducing me to Linux, I’ve been enjoying various distros for well over seven years, my favorite being Xubuntu. But that isn’t to say that I’m by any means a Guru. There are times Linux honestly leaves me baffled, but I felt pretty confident that setting up a keyboard on the RPi would be a walk in the park. After all, the latest addition, the PI3 comes with Bluetooth built in, so you’d think it worked pretty much out the box. When I bought an ultra slim bluetooth keyboard from Pimoroni, I was a little shocked to spend the next two days fighting with it. No matter what I tried the Bluetooth software in x.org refused to work with the keyboard. Thankfully the Raspberry Pi forum came to the rescue and after an hour searching for a solution, I finally had my answer.
If like me you’ve been reading guides telling you to use bluez-simple-agent to set up your keyboard, then you know that such a command no longer works on Raspbian Jessie, Bluez has been updated since those guides were written. Fortunately however Archlinux has a great Wiki page dedicated to using Bluetooth in Linux and how to get things working using the new command “Bluetoothctl“. From what I can tell the process isn’t that dissimilar to “bluez-simple-agent“.
Now I’m going to assume you’ve already installed Bluez on your machine. If you haven’t, then I suggest you get cracking.
– At the command prompt, type “bluetoothctl” and press enter.
– By default your controller is turned off, to switch it on enter “Power on“.
– Next, lets have a look at what your Pi can see, enter the command “Devices“. The computer will list any visible bluetooth devices, along with their MAC address. If you see the device your wanting to use, make a note of the MAC address now, as you’ll need it later.
– If your device wasn’t listed, try putting your pi in discovery mode with the “Scan on” command. Hopefully this will add any device that weren’t already discovered.
– Now turn agent on by typing the command “Agent on“. If successful your Pi should report: “agent registered“.
– Time to try connecting to your device using the address you jotted down earlier. Enter the command “Pair MAC address“. The PI should now attempt to pair to your device, so make sure you entered the address correctly, otherwise it will fail.
– If you’re using a device without a pin, you can manually instruct the PI to trust the device. This is sometimes necessary when your pi needs to reconnect to it. You can do this by entering the following command “Trust MAC xx.xx.xx“. Note after MAC, you should enter the address you have written down.
– Now lets finally establish a connection using the command “Connect MAC xx.xx.xx.xx“.
Hopefully your PI connected successfully to your keyboard, so why not try typing something? Did anything appear on your screen? If it didn’t, try retracing your steps. If you used the “Trust” command, you will have to use the command ‘Remove xx.xx.xx.xx’ to make your Pi forget the device. Once removed, try going through the tutorial again, making sure you didn’t miss any steps.
If typing on the keyboard resulted in letters appearing on the screen, congratulations! You successfully connected your keyboard to your Raspberry Pi. If you reboot your computer now, it will forget everything you have done so far and your Pi and keyboard will no longer be connected. To make sure this doesn’t happen and your keyboard remains connected, we’ll need to create a udev rule. This is our way of telling the Pi to look for our bluetooth device each time it boots up and if present, reconnect to it.
First quit bluetoothctl by typing ‘Quit’ and pressing the enter key, you should now be returned to Terminal. For the next step, begin by typing “nano /etc/udev/rules.d/10-local.rules“. Nano should load up a blank document, which we will use to fill with the following.
# Set bluetooth power up ACTION==”add”, KERNEL==”hci0″, RUN+=”/usr/bin/hciconfig hci0 up”
To save, press “CTRL+O” followed by the ‘ENTER’ key. Next press “CTRL+X” to exit Nano, your computer will now remember to reconnect to your device the next time you reboot.
The above guide is an adaptation of information found @ “wiki.archlinux.org/index.php/bluetooth#bluetoothctrl”