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.
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:
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).
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:
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
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
This module will be useful later for the interface Bluetooth:
sudo apt-get install bluealsa pulseaudio
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
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.
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
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).
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
If you are interested in more Raspberry PI projects, take a look at my Raspberry PI tutorials.
Enjoy!
In this tutorial, we will use two Arduino Uno to demonstrate the Serial Peripheral Interface…
In this tutorial, we will be making an automatic irrigation system (AIS) with Arduino and…
This tutorial will show you how to use Python Virtual Environment with Raspberry PI computer…
This tutorial will show you how to get betting odds with Raspberry PI by using…
This tutorial will show you how to perform the backup of Raspberry PI (computer board)…
This tutorial will show you how to install Honeygain on a Raspberry PI computer board…
View Comments
don't think you need pulseaudio here
bluealsa is able to connect without pulse.
Also both working together is not recommended
This solution worked for me for the 27 May 2020 RasPiOS build that was not working. THANK YOU for this solution!
I ran through these instructions and at the end i get no sound.
Not sure what to do at this point.
Hi Douglas.
Bluetooth is really hard to work in Raspbian Lite and results can change from device to device. I'm going to write you to try more help in troubleshooting.
I have never been able to get sound out of 2 different RPi 3s via BT now and have tried everything I can find online for the last two years now.
I am beginning to disbelieve it works for anybody.
Multiple different speakers always pair, and then connect, and are able to be seen from the pi but no sound ever comes out of the speakers.
There must be another mixer setting, or switch to enable BT output.
....Larry
Hi Larry,
thank you for your feedback. With my CellularLine bluetooth headset I also had to work hard to make it working. Some keys which gave me results:
- check pulseaudio is working: even if installed, pulseaudio could be not running. Once started, verify that its process is alive
- bluetooth pairing seems to be working easily in Rasbpian Lite. But check - when you connect a device - that it remains connected
- aplay worked to me with sco profile and a wav file previously recorded with same library and "arecord" command. so try this way first. If your device is an "output only" one, try downloading a wav file or let me know to generate one and make it available to you in my download area
A different solution is listed in Gus comment. You can also try this one.
If you have still problems, feel free to mail me (my contacts are in my Info page)
Thanks for your reply.
I did some noise out of the speaker using the PROFILE=A2DP in the aplay command line. The .wav file sounded like it was playing at about 1/10 speed, and the .mp3 file did output some random noise. This indicates most things are working, so far.
Every other attempt no sound will come out of the speaker. Today I have rechecked pulseaudio is running but still no sound. Using A2DP or SCO I get silence until after about 45 seconds I get aplay kicking back with
aplay: main:828: audio open error: no such device
The speaker still hasn't reported "disconnecting Bluetooth" at that point so the connection is still valid from the speaker's point of view. Pulseaudio is still showing running in the ps list.
This is the same results I got many times with the Alexa speaker also. They pair, connect and everything seems OK but will never produce sound.
I wrote some pythin3 code to open a socket via BT and the speaker reports paired, and connected. Same thing with it. After trying many methods with Alexa and GH "right out of the book", I assumed those brands would not allow other brands to interoperate with them.
This tiny speaker I am using now connects and plays music from my Samsung S7, and my laptop, just fine. Something is missing on these RPi methods yet. Maybe I need to connect up a monitor and KB for a RPi desktop trial to prove it isn't just a Raspbian command line thing
**SIGH**
If you are receiving some sound (even if a simple noise), this is a good signal: this means that bluetooth is paired and connected. This also could mean that in this moment you could have a simple coding problem. So, your next test it the one you are going to do: try with a deskop version able to let you have also more advanced music applications (like the famous vlc player) which are more likely to have a complete library for media decoding.
Another trik: in terminal, you should be able to use "alsamixer" command, which enables a terminal console to manage volume for your audio devices.
Thanks. The alsamixer only shows analogue devices, namely just one.
The noise indicates the volume is loud enough. I'll have to make a bench setup to get a monitor and kb on this Rpi. I would even be happy just to use a simple earphone jack speaker for this application but having trouble finding one of them too. They have incorporated video into the same jack making it non-standard.
With xrdp you can control your Raspbian Desktop from Windows Remote Desktop, installing it in headless mode (without monitor and keyboard). Please try this guide: http://peppe8o.com/install-raspbian-buster-desktop-in-your-raspberry-pi-with-optional-remote-desktop/
same and i cant fix it! can anyone help me undo this?
Hi kendun,
undo-ing it is simple:
- sudo nano /lib/systemd/system/bthelper@.service
-> remove "ExecStartPre=/bin/sleep 2" row
- sudo nano /lib/systemd/system/bluetooth.service
-> remove " --noplugin=sap" option
- sudo apt-get purge bluealsa pulseaudio
Just for curiosity, are you getting errors or simply you can't get sound?
Awesome post thank you. I followed it and I have a few suggestions for improvements after having tried (and retried on a brand new SD card) the steps myself:
1- I started with a fresh Pi Zero W, with a brand new Raspbian Lite (buster) installation
2- I didn't have to install PulseAudio. BlueAlsa alone worked. I skipped the section "Check That Pulseaudio Is Running" altogether
3- For convenience, I created a file .asoundrc in my home directory for convenience, it included a profile called "bluealsa" that I referred to from the media players command line (see below)
# Bluetooth headset
defaults.bluealsa {
interface "hci0" # host Bluetooth adapter
# device "AA:00:A7:00:B8:D4" Vidonn - doesnt support A2DP! Commenting out
device "D0:8A:55:08:FC:BE"
profile "a2dp"
}
4- I learned that "SCO" and "A2DP" refer to the possible 'services' that your bluetooth audio device supports. If you have a speaker or high end headphones, they likely support A2DP . If they're simple telephone headsets (like my Vidonn bone conduction headphones) they do not support A2DP but rather SCO (which is optimized for VOIP calls). They're called "profiles", replace SCO with A2DP where necessary.
5- aplay expects a wav file, not an mp3 file :) I jumped from my seat when aplay started playing the encoded data found in the mp3 file into my headphones (it was all noise)
6- for omxplayer to be able to play over bluetooth you need to call it like the following (I wanted omxplayer because I needed to play video, not just audio). "bluealsa' here refers to the name of the profile created inside the ~/.asoundrc file like I described above: omxplayer -o alsa:bluealsa
Hi!
Thanks so much for this post. It really saved me.
SCO works for my buetooth speaker
and a2dp for my headphones!
Cheers.*
Glad to have been helpful
I setup the .asoundrc as you suggested and that works fine for omxplayer. Any thoughts on how to get SoX to use the bluetooth output?
Thanks. I followed your instructions, then my Jabra BT2080 works fine on my pi3 B+.
I skipped chapter "Check That Pulseaudio Is Running". Pulseaudio is not necessary here.
AWESOME!! Thanks a lot for this post. It worked for me.
Thank you, Gama, for your feedback!
pi@raspberrypi:~ $ sudo systemctl status bluetooth.service
● bluetooth.service - Bluetooth service
Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset
Active: active (running) since Sun 2020-06-28 11:36:45 EDT; 2min 34s ago
Docs: man:bluetoothd(8)
Main PID: 607 (bluetoothd)
Status: "Running"
Tasks: 1 (limit: 4915)
Memory: 2.1M
CGroup: /system.slice/bluetooth.service
└─607 /usr/lib/bluetooth/bluetoothd --noplugin=sap
Jun 28 11:36:45 raspberrypi bluetoothd[607]: Bluetooth daemon 5.50
Jun 28 11:36:45 raspberrypi systemd[1]: Started Bluetooth service.
Jun 28 11:36:45 raspberrypi bluetoothd[607]: Starting SDP server
Jun 28 11:36:45 raspberrypi bluetoothd[607]: Excluding (cli) sap
Jun 28 11:36:45 raspberrypi bluetoothd[607]: Bluetooth management interface 1.14
Jun 28 11:36:45 raspberrypi bluetoothd[607]: Endpoint registered: sender=:1.24 p
Jun 28 11:36:45 raspberrypi bluetoothd[607]: Endpoint registered: sender=:1.24 p
Jun 28 11:37:03 raspberrypi bluetoothd[607]: Endpoint registered: sender=:1.45 p
Jun 28 11:37:03 raspberrypi bluetoothd[607]: Endpoint registered: sender=:1.45 p
Jun 28 11:37:03 raspberrypi bluetoothd[607]: RFCOMM server failed for Headset Vo
##########################
RFCOMM server failed for Headset Vo
##########################
Everything went smooth until very last step, then I got the error mentioned above.
Any ideas about this issue?
Hi joseph. Your error seems to be truncated. Please check with journalctl -xe for complete error label
Hello! Thanks for the article. I have been following youness's article but still not successful.
So my main question is DOES THIS WORK WITH HFP PROFILE HEADPHONES? Because all the Pulseaudio related articles talk about using ofono with hfp profiles, which is proving to be another headache. Please let me know if it works.
Thanks
Hi Ebrahim. I'll try to test it as soon as possible. Bluetooth world has a number of different hardware and profiles so huge that procedures working with some hardware could have issue with different hardware.
In the meanwhile, you could try with a Raspberry PI OS Desktop installation, so having other packages that could visually help you.
Very good tutorial. I prefer setup without pulseaudio.
U are God!!!!
So I went all the way through the tutorial and got to the end, ran the tests and the audio came through and the recording worked. However, I cannot use it for anything else (browser, app ect.) Is this expected or did I do something weird?
Hi,
I just added a final section explaining how to use a common player (like omxplayer). What is important (and changes from app to app) is that you need to tell your software which output audio device use, pointing to bluealsa. In omxplayer it can be done with "-o" option. also defining tha .asoundrc file simplify bluealsa usage because it point by default to correct configuration.