How to get started with Raspberry Pi :: Part III :: Connecting Bluetooth Devices

In previous article (Part II - Connecting NVMe) we looked how to connect our internal NVMe drive to our Raspberry Pi 5. In this article we will explore how to connect bluetooth devices, specifically a bluetooth keyboard and a. mouse. If you don’t plan on connecting bluetooth devices to your Raspberry Pi, feel free to skip these steps and go to the next article in the series
Introduction
Bluetooth is a short-range wireless protocol for connecting devices to your system. There are plenty of devices that could be useful with your Raspberry Pi hardware, such as speakers and headphones for media output, mobile phones as a remote control for your Raspberry Pi, sensors for data collection, game controllers, and many other use cases we won’t be focusing on for now.
Adding Bluetooth devices is not critical if you are going to run Raspberry Pi in a headless mode and accessing through ssh
only. However, if you wanted to play and connect your Raspberry Pi to a screen you will need a keyboard (and maybe a mouse). There are USB (wired) mouse and keyboards or the ones with USB dongle (wireless). Those should work automatically, out of the box once you connect them to your Raspberry Pi. However, my keyboard and mouse are both bluetooth only devices - i.e. no dongles, so I had to learn more about how Raspberry Pi OS handles Bluetooth devices.
Managing Bluetooth Devices on Raspberry Pi
Managing Bluetooth devices on a system is easily done through a GUI. However, we will be using our Raspberry Pi mostly in terminal only (ssh
) mode, so we will need to use CLI tool bluetoothcli
.
Raspberry Pi 5 comes with Bluetooth protocol turned on, but in some cases that might have been disabled. Check if the wireless connections with rfkill list
:
root@pi:~# rfkill list
0: hci0: Bluetooth
Soft blocked: no
Hard blocked: no
1: phy0: Wireless LAN
Soft blocked: no
Hard blocked: no
In case your Bluetooth was blocked, you can unblock it with rfkill unblock bluetooth
.
Let’s start the bluetoothctl
utility to turn on bluetooth and scanning. You should see all devices that are discoverable:
root@pi:~# bluetoothctl
Agent registered
[CHG] Controller 2C:CF:67:AA:BB:CC Pairable: yes
[bluetooth]# power on
Changing power on succeeded
[bluetooth]# scan on
Discovery started
[CHG] Controller 2C:CF:67:AA:BB:CC Discovering: yes
[NEW] Device D4:B0:7B:AA:BB:CC MX Master 3S
[NEW] Device D1:3D:70:AA:BB:CC MX KEYS S
Connecting Bluetooth Keyboard
I have Logitech MX Keys S keyboard that is a bluetooth only devices. It can connect to three different devices (3 different profiles).
Probably, a good idea to read your hardware MAC address of the device, in my case that was D1:3D:70:AA:BB:CC
. If you haven’t turned on the pairing mode, please turn that on by a long press on a profile button. This should correspond to the scanned MAC device address:
[NEW] Device D1:3D:70:AA:BB:CC MX KEYS S
Let’s pair this device with pair D1:3D:70:AA:BB:CC
[bluetooth]# pair D1:3D:70:AA:BB:CC
Attempting to pair with D1:3D:70:AA:BB:CC
[CHG] Device D1:3D:70:AA:BB:CC Connected: yes
You will see a PASSKEY
on the screen, e.g. 637926
that you need to type on the keyboard for verification, followed by ENTER
key:
[agent] Passkey: 637926
[CHG] Device D1:3D:70:AA:BB:CC Bonded: yes
[CHG] Device D1:3D:70:2AA:BB:CC Paired: yes
Pairing successful
[CHG] Device D1:3D:70:AA:BB:CC Modalias: usb:v046DpB12345678
[CHG] Device D1:3D:70:AA:BB:CC ServicesResolved: yes
Let’s add the trust for the device with trust D1:3D:70:AA:BB:CC
:
[MX KEYS S]# trust D1:3D:70:AA:BB:CC
[CHG] Device D1:3D:70:AA:BB:CC Trusted: yes
Changing D1:3D:70:2AA:BB:CC trust succeeded
and connect to it with connect D1:3D:70:AA:BB:CC
:
[MX KEYS S]# connect D1:3D:70:AA:BB:CC
Attempting to connect to D1:3D:70:AA:BB:CC
Connection successful
Connecting Bluetooth Mouse
I regularly use Logitech MX Master 3S mouse for my other laptop. The mouse is also bluetooth only devices, and has also 3 profiles you can connect to.
Similarly, like in Bluetooth keyboard, we need to read the mouse MAC address, e.g. D3:EB:A5:AA:BB:CC
so we can pair it up. If your mouse is not in pairing mode, press the long press on the profile button to turn on the discover mode.
Let’s add the commands for pairing our Bluetooth mouse:
pair D3:EB:A5:AA:BB:CC
trust D3:EB:A5:AA:BB:CC
connect D3:EB:A5:AA:BB:CC
Bluetooth Commands
There are plenty of other commands you could explore in bluetoothctl
utility, The most important ones are:
power on - to turn on the Bluetooth
power off - to turn off the Bluetooth
scan on - to start scanning for Bluetooth devices
scan off - to stop scanning for Bluetooth devices
pair <MAC ADDRESS> - to pair up a device
remove <MAC ADDRESS> - to remove (unpair) a davice
trust <MAC ADDRESS> - to add a trust for a device
untrust <MAC ADDRESS> - to remove trust for a device
connect <MAC ADDRESS> - to connect a device
disconnect <MAC ADDRESS> - to disconnect a device
info <MAC ADDRESS> - to get more information on a device
devices - to list all paired/bonded/trusted/connected devices
exit - to exit the utility
So, let’s exit the bluetoothctl
utility with exit
.
🏆 Congratulations! Your Bluetooth settings should be saved and you should be able to connect to your Raspberry Pi whenever you turn on your devices.
Summary
In this article we looked at how to connect Bluetooth devices to your Raspberry Pi, specifically how to connect a Bluetooth keyboard and a mouse. There are many other bluetooth devices that could be useful to connect to your Raspberry Pi. Managing Bluetooth devices on Raspberry Pi is very simple from both GUI and CLI.
References
Bluetooth (Wikipedia article)
How to Use Bluetooth on Raspberry Pi: GUI & Command Guide (community article)
Subscribe to my newsletter
Read articles from Neven Cvetkovic directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
