Some links in this post may be affiliate links. We may get paid if you buy something or take an action after clicking one of these, but without addictional costs for you compared to direct buying.

Fixed: Connect Bluetooth Headphones with your Raspberry PI

  • by

Before start reading: please consider, if this article is useful to you or if you just appreciate my work on it, to support me just sharing this article with your friends by your favourite Social Network. This would be a really appreciated help. Have a nice reading!

Last Updated on

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 on a Raspbian Lite installation.

My tests have been frustrated by common issues not well documented in the web. 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 fixes from different online articles, 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.


According to youness.net article, with the time there has been some changes in managing audio connection with Bluetooth. Raspbian 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 guide will help you to correctly connect Bluetooth headphones with your Raspberry PI (any model with integrated 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:

Step-By-Step Guide

Install OS

We’ll start from standard Raspbian Buster 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[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 someting is going wrong on startup. This can be fixed simply stopping the SIM profile loading. Edit “/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

Now reboot

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[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 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

One of most common issues is that when attempting to connect bluetooth headphones Pulseaudio is not running. So, before proceding to bluetooth pairing, please check that it is running.

Type 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 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.

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

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!!!):

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 you Bluetooth headphones are connected!

Test Your Headphones

From terminal, play audio example files (remember to use your headphones address intead 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 with your Raspberry PI

Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *