How to Use Bluetooth on Raspberry Pi OS

Jinku Hu Feb 02, 2024
  1. Use the bluetoothctl Command Line Control Tool in Raspberry Pi
  2. Managing Multiple Controllers on Raspberry Pi
  3. Use the bluetoothctl to Remove Saved Devices in Raspberry Pi
  4. Make Your Raspberry Pi Undiscoverable Using bluetoothctl
How to Use Bluetooth on Raspberry Pi OS

This article will introduce a command-line interface for Bluetooth connectivity on Raspberry Pi OS.

Use the bluetoothctl Command Line Control Tool in Raspberry Pi

The bluetoothctl is a handy command-line utility for interface with the Bluetooth functionality in Raspberry Pi OS.

The utility is usually provided in the current Pi OS releases, but if for some reason your system can’t find the command try installing it:

sudo apt install bluez bluez-utils

If you have executed the bluetoothctl command successfully, there should be a message like “Agent registered” and [bluetooth]# prompt displayed:

Agent registered
[bluetooth]# 

At this point, we can execute the scan on command in the prompt, which will start scanning for reachable devices.

scan on

Successful execution should yield the output similar to the following lines and transfer the control to the bluetoothctl command prompt.

Discovery started
[CHG] Controller B8:27:EB:ff:ff:ff Discovering: yes
[bluetooth]#

Additionally, it will list available Bluetooth devices with corresponding MAC addresses and names. Once you see the needed device, you can run the following command to initiate the pairing:

pair ff:ff:ff:ff:ff:ff

If you already have trusted the given device, Raspberry Pi might connect it automatically. However, you can also force the connection using the next command:

connect ff:ff:ff:ff:ff:ff

Notice that we use a generic MAC address in the above examples, but you should replace it using the device address you want to pair with.

The connection can be terminated using the disconnect command and passing the corresponding address as the argument.

Managing Multiple Controllers on Raspberry Pi

If you have additional USB Bluetooth adapters connected to the Pi, you should select one of them before executing the previous commands.

The controllers (hardware that controls Bluetooth radio communication) have unique MAC addresses, which can be retrieved using the list command on the bluetoothctl command prompt.

Once you have identified the controller for the current Bluetooth connection, run the select command with the corresponding MAC address as an argument.

Note that the next command will list all available controllers in the system:

list

After this, you can move on to other commands as needed. Generally, built-in Raspberry Pi controllers addresses start with B8:27:EB.

The bluetoothctl provides several other commands listed using the help command.

Notice that these commands must be executed in the bluetoothctl command prompt or appended to the bluetoothctl command itself if you want to execute from the default shell.

list
// ALTERNATIVELY
bluetoothctl list

Use the bluetoothctl to Remove Saved Devices in Raspberry Pi

Bluetooth devices that have been paired or trusted using the Pi controller are listed with the paired-devices command in the bluetoothctl command prompt.

If you want to remove a specific device from the list, execute the remove command with the corresponding MAC address as follows:

remove ff:ff:ff:ff:ff:ff

Make Your Raspberry Pi Undiscoverable Using bluetoothctl

Sometimes, you might want to stop broadcasting the Pi controller Bluetooth advertising information. The latter can be done with the discoverable command.

It takes on or off arguments based on the controller’s current state. In this case, we need the next command to make the Pi undiscoverable:

discoverable off
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - Raspberry Pi