Fixed: Connect Bluetooth Headphones with your Raspberry PI

3.9
(45)

Last Updated on 15th January 2020 by peppe8o

For a while, I have 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 headphones or speakers 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 on 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 until I reached a configuration that simply works. The main issues have been related to Bluetooth service errors on startup and connection breaking.

The solution came out with several fixes coming from different tutorials, being particularly 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 of them together.

contemporary earphones and headphones on white table near smartphone


According to the youness.net article, with time there have been some changes in managing the audio connection with Bluetooth. Raspberry PI OS uses BlueZ as a Bluetooth stack:

  • BlueZ deals with pure Bluetooth tasks (pairing/connection/…)
  • So, BlueZ needs additional SW components for audio management
  • The 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 an onboard Bluetooth module).

What We Need

Raspberry PI 4 model B image

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 continue with the project or remove them from the shopping cart. So, hardware will be only:

Check hardware prices with the following links:

Amazon raspberry pi boards box
Amazon raspberry pi Zero W box
Amazon Micro SD box
Amazon bluetooth headphones box

Step-By-Step Guide

Install OS

We’ll start from standard Raspberry PI OS Lite installation with WiFi and ssh enabled. 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 the default pi user, without having to elevate privileges for each Bluetooth action. Simply use the following command to add the pi user to the 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 for the interface Bluetooth:

sudo apt-get install bluealsa pulseaudio

SAP Driver Initialization Failure

The first issue comes from SAP (SIM Access Profile) driver initialization. If you check the 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[1]: Starting Bluetooth service...
Jan 06 21:15:56 raspberrypi bluetoothd[424]: Bluetooth daemon 5.50
Jan 06 21:15:56 raspberrypi systemd[1]: Started Bluetooth service.
Jan 06 21:15:57 raspberrypi bluetoothd[424]: Starting SDP server
Jan 06 21:15:57 raspberrypi bluetoothd[424]: Bluetooth management interface 1.14 initialized
Jan 06 21:15:57 raspberrypi bluetoothd[424]: Sap driver initialization failed.
Jan 06 21:15:57 raspberrypi bluetoothd[424]: sap-server: Operation not permitted (1)
Jan 06 21:15:57 raspberrypi bluetoothd[424]: Failed to set privacy: Rejected (0x0b)

The “Sap driver initialization failed.” notices that something is going wrong on startup. This can be fixed simply by 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 one appears 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

Now reboot

sudo reboot now

Privacy Setting Rejected Failure

Once rebooted, a new check on the 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[1]: Starting Bluetooth service...
Jan 06 21:38:05 raspberrypi bluetoothd[424]: Bluetooth daemon 5.50
Jan 06 21:38:05 raspberrypi systemd[1]: Started Bluetooth service.
Jan 06 21:38:05 raspberrypi bluetoothd[424]: Starting SDP server
Jan 06 21:38:05 raspberrypi bluetoothd[424]: Excluding (cli) sap
Jan 06 21:38:05 raspberrypi bluetoothd[424]: Bluetooth management interface 1.14 initialized
Jan 06 21:38:05 raspberrypi bluetoothd[424]: Endpoint registered: sender=:1.6 path=/org/bluez/hci0/A2DP/SBC/Source/1
Jan 06 21:38:05 raspberrypi bluetoothd[424]: Endpoint registered: sender=:1.6 path=/org/bluez/hci0/A2DP/SBC/Source/2
Jan 06 21:38:05 raspberrypi bluetoothd[424]: Failed to set privacy: Rejected (0x0b)

A simple restart of the Bluetooth service solves this problem, but the solution is only temporary until 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

The next step and one of the most common issues is that, when attempting Bluetooth connection, Pulseaudio is not running. So, before proceeding to Bluetooth pairing, please check that it is running.

Type, from the 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:

pulseaudio --start

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 the pi user has been added to the 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.

Command:

bluetoothctl

Expected output:

pi@raspberrypi:~ $ bluetoothctl
Agent registered
[bluetooth]#

Command:

power on

Expected output:

[bluetooth]# power on
Changing power on succeeded

Command:

agent on

Expected output:

[bluetooth]# agent on
Agent is already registered

Command:

default-agent

Expected output:

[bluetooth]# default-agent
Default agent request successful

Command:

scan on

Expected output:

[bluetooth]# scan on
Discovery started
[CHG] Controller B8:27:EB:23:73:DF Discovering: yes
[NEW] Device 00:25:DB:78:86:98 BTC7

The first row is RPI onboard Controller. My headphones are listed in the second row.

Command (remember to change my Headphones address with your one  from the following command to the end!!!):

pair 00:25:DB:78:86:98

Expected output:

[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

Command:

trust 00:25:DB:78:86:98

Expected output:

[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

Command:

connect 00:25:DB:78:86:98

Expected output:

[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 your Bluetooth connection with your headphones is ready!

You will need to connect your Bluetooth device at each connection. Otherwise, add the following line in your /etc/rc.local file, before the final “exit 0” row:

bluetoothctl connect 00:25:DB:78:86:98

(and, of course, the Bluetooth device will need to be powered on before Raspberry PI boots).

Test Your Headphones

From the 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 to 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.

The final step is using a common player to listen to files like .mp3. This will require creating 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 by creating a .asoundrc file in the pi default folder:

nano .asoundrc

In this file, use the 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, you can install it with the following command:

sudo apt install omxplayer

You will need the following simple command:

omxplayer -o alsa:bluealsa file_example_MP3.mp3

What’s Next

If you are interested in more Raspberry PI projects, take a look at my Raspberry PI tutorials.

Enjoy!

How useful was this post?

Click on a star to rate it anonymously!

Average rating 3.9 / 5. Vote count: 45

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?