For a while I has been struggling in getting my Raspberry Pi connected to bluetooth headphones. This was with the idea to use in future projects available home assistants (like Google Assistant or Amazon Alexa) via bluetooth headphone or speaker on a Raspberry PI OS Lite installation (without the need of a GUI or graphical bluetooth manager).
My tests have been frustrated by common issues not well documented in the web and issues connecting an audio bluetooth device are really complex to find and solve. Also raspi-config can’t help, since this doesn’t include any bluetooth configuration in Interfacing options. So I started to follow many guides untill I reached a configuration that simply works. Main issues has been related to Bluetooth service errors on startup and connection breaking.
Solution came out with several fixes coming from different tutorials, being partcularly important the tricks from https://www.sigmdel.ca/michel/ha/rpi/bluetooth_n_buster_01_en.html and http://youness.net/raspberry-pi/bluetooth-headset-raspberry-pi. In this tutorial I’ll try to make all them together.
According to youness.net article, with the time there has been some changes in managing audio connection with Bluetooth. Raspberry PI OS uses BlueZ as Bluetooth stack:
- BlueZ deals with pure Bluetooth tasks (pairing/connection/…)
- So, BlueZ needs additional SW components for audio management
- First Linux audio manager that comes to mind is ALSA
- But, BlueZ (>= v5.0) doesn’t support ALSA anymore
- Instead, BlueZ is now using PulseAudio (>= 5.0)
- And, PulseAudio still uses ALSA
So, the chain changes should be the following:
Before: BlueZ → ALSA
Now: BlueZ → PulseAudio → ALSA
The following tutorial will help you to correctly connect bluetooth headphones with your Raspberry PI (not only model B devices, but any RPI model with onboard bluetooth module).
What We Need
I suggest adding to your shopping chart all needed hardware, so that at the end you will be able to evaluate overall costs and decide if continuing with the project or removing them from shopping chart. So, hardware will be only:
- Raspberry PI with its power supply
- a micro SD card
- Bluetooth Headphones (I used a very old model, so it should work with newer ones)
Check hardware prices with following links:
We’ll start from standard Raspberry PI OS Lite installation with WiFi and ssh enables. So every command within the following paragraphs will be issued via ssh remote connection.
Be sure to have your OS up to date:
sudo apt-get update sudo apt-get upgrade
Adding Pi To Bluetooth Users
This fix helps to use audio files from default pi user, without having to elevate privileges for each bluetooth action. Simply use the following command to add pi user to bluetooth group:
sudo usermod -G bluetooth -a pi
Now logout/login or reboot to have this change running:
sudo reboot now
Install Bluealsa and Pulseaudio Module
This module will be useful later to interface
sudo apt-get install bluealsa pulseaudio
SAP Driver Initialization Failure
The first issue comes from SAP (SIM Access Profile) driver initialization. If you check bluetooth service starting status:
sudo systemctl status bluetooth.service
you should find the following:
● bluetooth.service - Bluetooth service Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2020-01-06 21:15:56 GMT; 2min 57s ago Docs: man:bluetoothd(8) Main PID: 424 (bluetoothd) Status: "Running" Tasks: 1 (limit: 2200) Memory: 2.7M CGroup: /system.slice/bluetooth.service └─424 /usr/lib/bluetooth/bluetoothd Jan 06 21:15:56 raspberrypi systemd: Starting Bluetooth service... Jan 06 21:15:56 raspberrypi bluetoothd: Bluetooth daemon 5.50 Jan 06 21:15:56 raspberrypi systemd: Started Bluetooth service. Jan 06 21:15:57 raspberrypi bluetoothd: Starting SDP server Jan 06 21:15:57 raspberrypi bluetoothd: Bluetooth management interface 1.14 initialized Jan 06 21:15:57 raspberrypi bluetoothd: Sap driver initialization failed. Jan 06 21:15:57 raspberrypi bluetoothd: sap-server: Operation not permitted (1) Jan 06 21:15:57 raspberrypi bluetoothd: Failed to set privacy: Rejected (0x0b)
The “Sap driver initialization failed.” notices that someting is going wrong on startup. This can be fixed simply stopping the SIM profile loading. With your favourite text editor (mine one is nano), modify “/lib/systemd/system/bluetooth.service” file to add “–noplugin=sap” option near “ExecStart=/usr/lib/bluetooth/bluetoothd” configuration:
sudo nano /lib/systemd/system/bluetooth.service
and make sure that your appear like the following (just adding –noplugin=sap as mentioned above):
[Unit] Description=Bluetooth service Documentation=man:bluetoothd(8) ConditionPathIsDirectory=/sys/class/bluetooth [Service] Type=dbus BusName=org.bluez ExecStart=/usr/lib/bluetooth/bluetoothd --noplugin=sap NotifyAccess=main #WatchdogSec=10 #Restart=on-failure CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE LimitNPROC=1 ProtectHome=true ProtectSystem=full [Install] WantedBy=bluetooth.target Alias=dbus-org.bluez.service
sudo reboot now
Privacy Setting Rejected Failure
Once rebooted, a new check on bluetooth service status shows that the third error is still occurring. Command:
sudo systemctl status bluetooth.service
should give you:
● bluetooth.service - Bluetooth service Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2020-01-06 21:38:05 GMT; 3min 9s ago Docs: man:bluetoothd(8) Main PID: 424 (bluetoothd) Status: "Running" Tasks: 1 (limit: 2200) Memory: 2.7M CGroup: /system.slice/bluetooth.service └─424 /usr/lib/bluetooth/bluetoothd --noplugin=sap Jan 06 21:38:04 raspberrypi systemd: Starting Bluetooth service... Jan 06 21:38:05 raspberrypi bluetoothd: Bluetooth daemon 5.50 Jan 06 21:38:05 raspberrypi systemd: Started Bluetooth service. Jan 06 21:38:05 raspberrypi bluetoothd: Starting SDP server Jan 06 21:38:05 raspberrypi bluetoothd: Excluding (cli) sap Jan 06 21:38:05 raspberrypi bluetoothd: Bluetooth management interface 1.14 initialized Jan 06 21:38:05 raspberrypi bluetoothd: Endpoint registered: sender=:1.6 path=/org/bluez/hci0/A2DP/SBC/Source/1 Jan 06 21:38:05 raspberrypi bluetoothd: Endpoint registered: sender=:1.6 path=/org/bluez/hci0/A2DP/SBC/Source/2 Jan 06 21:38:05 raspberrypi bluetoothd: Failed to set privacy: Rejected (0x0b)
A simple restart of bluetooth service solves this problem, but solution is only temporary untill a new reboot comes. The solution for this is editing “/lib/systemd/system/bthelper@.service” file to add the “ExecStartPre=/bin/sleep 2” configuration. For this solution credits go to Ben Windsow on https://github.com/RPi-Distro/pi-bluetooth/issues/8#issuecomment-480270680. So open the above file:
sudo nano /lib/systemd/system/bthelper@.service
and make it the same as the following:
[Unit] Description=Raspberry Pi bluetooth helper Requires=bluetooth.service After=bluetooth.service [Service] Type=simple ExecStartPre=/bin/sleep 2 ExecStart=/usr/bin/bthelper %I
Reboot again and now everything should be ok with sudo systemctl status bluetooth.service.
Check That Pulseaudio Is Running
Next step and one of most common issues is that, when attempting bluetooth connection, Pulseaudio is not running. So, before proceding to bluetooth pairing, please check that it is running.
Type, from terminal window, the following command:
ps aux | grep pulseaudio
If this command returns the following output:
pi@raspberrypi:~ $ ps aux | grep pulseaudio pi 538 0.0 0.0 7348 504 pts/0 S+ 22:01 0:00 grep --color=auto pulseaudio
then you must manually launch pulseaudio:
so that you should see the correct status from ps aux | grep pulseaudio command:
pi@raspberrypi:~ $ ps aux | grep pulseaudio pi 544 4.3 1.8 181592 17720 ? Sl 22:02 0:00 pulseaudio --start pi 564 0.0 0.0 7348 488 pts/0 S+ 22:02 0:00 grep --color=auto pulseaudio
Pair Bluetooth Device
Finally, we can try that everything really works. Let’s configure bluetooth (note that I don’t use sudo because pi has been added to bluetooth group). First of all, consider that my headphones address is 00:25:DB:78:86:98, but in your case will depend on your hardware.
Set in pairing mode your bluetooth headphones according to their user guide. Go to the terminal.
pi@raspberrypi:~ $ bluetoothctl Agent registered [bluetooth]#
[bluetooth]# power on Changing power on succeeded
[bluetooth]# agent on Agent is already registered
[bluetooth]# default-agent Default agent request successful
[bluetooth]# scan on Discovery started [CHG] Controller B8:27:EB:23:73:DF Discovering: yes [NEW] Device 00:25:DB:78:86:98 BTC7
First row is RPI on board Controller. My headphones are these listed in second row.
Command (remember to change my Headphones addres with your one from following command to the end!!!):
[bluetooth]# pair 00:25:DB:78:86:98 Attempting to pair with 00:25:DB:78:86:98 [CHG] Device 00:25:DB:78:86:98 Connected: yes [CHG] Device 00:25:DB:78:86:98 UUIDs: 00001108-0000-1000-8000-00805f9b34fb [CHG] Device 00:25:DB:78:86:98 UUIDs: 0000111e-0000-1000-8000-00805f9b34fb [CHG] Device 00:25:DB:78:86:98 ServicesResolved: yes [CHG] Device 00:25:DB:78:86:98 Paired: yes Pairing successful [CHG] Device 00:25:DB:78:86:98 ServicesResolved: no [CHG] Device 00:25:DB:78:86:98 Connected: no
[bluetooth]# trust 00:25:DB:78:86:98 [CHG] Device 00:25:DB:78:86:98 Trusted: yes Changing 00:25:DB:78:86:98 trust succeeded
[bluetooth]# connect 00:25:DB:78:86:98 Attempting to connect to 00:25:DB:78:86:98 [CHG] Device 00:25:DB:78:86:98 Connected: yes Connection successful [CHG] Device 00:25:DB:78:86:98 ServicesResolved: yes
Now you Bluetooth connection with your headphones is ready!
You will need to connect your bluetooth device at each connection. Otherwise, add following line in your /etc/rc.local file, before final “exit 0” row:
bluetoothctl connect 00:25:DB:78:86:98
(and, of course, bluetooth device will need to be powered on before Raspberry PI boot).
Test Your Headphones
From terminal, play audio example files (remember to use your headphones or bluetooth speakers address instead of mine!):
aplay -D bluealsa:DEV=00:25:DB:78:86:98,PROFILE=sco /usr/share/sounds/alsa/Front_Center.wav
You can also record your own test:
arecord -D bluealsa:DEV=00:25:DB:78:86:98,PROFILE=sco test.wav
and listen it
aplay -D bluealsa:DEV=00:25:DB:78:86:98,PROFILE=sco test.wav
to check both audio transmission and reception.
Now you should be able to correctly connect your bluetooth headphones / bluetooth speakers with your Raspberry PI.
Final step is using a common player to listen files like .mp3. This will require to create a default bluealsa profile and to specify in your player command options what output device it should use.
The default bluealsa profile can be arranged creating an .asoundrc file in pi default folder:
In this file, use following lines (changing bluetooth address with your one):
defaults.bluealsa.interface "hci0" defaults.bluealsa.device "00:25:DB:78:86:98" defaults.bluealsa.profile "sco" defaults.bluealsa.delay 10000
Save and exit. If you want to use omxplayer, which you can install with following command:
sudo apt install omxplayer
You will need the following simple command:
omxplayer -o alsa:bluealsa file_example_MP3.mp3
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?