Setting up Bluetooth on the Raspberry Pi 3

Pairing Bluetooth Devices

The term "pairing" refers to the mechanism by which Bluetooth devices discover, connect with, and trust each other. The process of discovery requires that your own Bluetooth device actually be discoverable. The discovery mode is activated via the discoverable on Bluetoothctl command. This makes the Rasp Pi discoverable for other devices until the function is turned off. The user can specify a length of time for this mode to remain enabled in the /etc/bluetooth/main.conf configuration file. The pairable on command allows the Rasp Pi to pair with other devices. A so-called agent, which has been integrated into Bluetoothctl, is required for successful acceptance.

The command agent on starts the agent, and default-agent configures it as the default tool responsible for all connections. At this point, everything is ready for the device to accept a connection. All that remains is to enable the discoverable mode for the participating device. Smartphones have a special menu to do this, and the option for activating pairing mode on current Android devices is commonly found in Settings | Bluetooth. During testing, I used a Bluetooth keyboard with a mouse pad from 1byone (model ODE00-0713) [8]. The key combination Fnc+C enables pairing mode (Figure 3).

Figure 3: Some Bluetooth keyboards let you activate the pairing mode with a function key combination.

The Rasp Pi initiates pairing with pair <Bluetooth address>. The keyboard then generates a one-time PIN and sends it to the computer to be displayed by the agent. The PIN gets entered via the keyboard, thus insuring that both devices remain under your control. To save the connection permanently, you should then enter trust <Bluetooth address>. Listing 2 shows the entire procedure.

Listing 2

Pairing Devices

$ bluetoothctl
[NEW] Controller B8:27:EB:DC:B4:BC raspberrypi [default]
# discoverable on
Changing discoverable on succeeded
[CHG] Controller B8:27:EB:DC:B4:BC Discoverable: yes
# pairable on
Changing pairable on succeeded
# agent on
Agent registered
# default-agent
Default agent request successful
[NEW] Device 6F:81:25:93:AC:3E 6F-81-25-93-AC-3E
# scan on
Discovery started
[CHG] Controller B8:27:EB:DC:B4:BC Discovering: yes
[NEW] Device C4:3A:BE:06:98:8A XperiaZ3
# pair C4:3A:BE:06:98:8A
Attempting to pair with C4:3A:BE:06:98:8A
[CHG] Device C4:3A:BE:06:98:8A Connected: yes
[agent] Confirm passkey 782895 (yes/no): yes
[CHG] Device C4:3A:BE:06:98:8A Modalias: usb:v0FCEp01BAd0010
[CHG] Device C4:3A:BE:06:98:8A UUIDs:
[CHG] Device C4:3A:BE:06:98:8A Paired: yes
[CHG] Device C4:3A:BE:06:98:8A Connected: no
[CHG] Controller B8:27:EB:DC:B4:BC Discoverable: no
# trust C4:3A:BE:06:98:8A
[CHG] Device C4:3A:BE:06:98:8A Trusted: yes
Changing C4:3A:BE:06:98:8A trust succeeded

After restarting, the two devices automatically connect to one another as soon as they come within range; that is, you only have to go through the steps described here one time. However, you might have to be patient because the RPi3 boots so fast that Bluetooth can't always keep up. To delete a saved connection, use the remove <MAC address> command.

Convenient GUI

The procedure describe above represents the one possibility that exists for connecting a Bluetooth device to a Rasp Pi that is not permanently connected to a display monitor (running headless). If your Rasp Pi has a graphical interface and is hooked up to a monitor, the GUI makes the process of connecting somewhat more convenient. The blueman package, which you need for this approach, is found in the Raspbian package sources:

$ sudo apt update
$ sudo apt install blueman

Blueman is a good choice for any user not operating a headless system. Although still not fully mature or entirely stable, this utility usually functions very well, and it came through my everyday testing without crashing. One issue, however, was pairing, which was not always reliable.

After installation, the Blueman Bluetooth manager appears in the menu and has an applet for quick access in the taskbar. The program lets you discover nearby devices and their pairing status, whether trust is on or off, and so on. Additionally, you can modify various settings, change the visibility of the Bluetooth adapter (Figure 4), and even send a file to a connected device by initiating transmission via the interface; however, I was unable to make this work on the command line.

Figure 4: Blueman lets you administer Bluetooth devices via a graphical interface.

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content