User avatar
Douglas6
Posts: 5047
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

[Deprecated: do not use] [Guide] Stream audio to the Raspberry Pi over Bluetooth

Mon Aug 05, 2019 1:24 pm

[This post is deprecated and of no use on recent Pi's. Oct. 9, 2021]

Here's a quick guide to streaming audio to a Raspberry Pi over Bluetooth. PulseAudio is not required or desired. If you want your Pi to act as an audio sink, that is, to act as a Bluetooth speaker, follow the steps.

1. Unpair your Pi. Use bluetoothctl to remove any pairings it may have. On your phone, or other source, forget any pairings.
2. On the Pi, edit the bluealsa service unit

Code: Select all

sudo nano /lib/systemd/system/bluealsa.service
EdIt the ExecStart line to add to the end of the line '--profile=a2dp-sink'. The line should look like

Code: Select all

ExecStart=/usr/bin/bluealsa --profile=a2dp-sink
Save the file.
3. Now, you're also going to want to start bluealsa-aplay. Systemd works good for that.

Code: Select all

sudo nano /etc/systemd/system/aplay.service
Insert into this file the following:

Code: Select all

[Unit]
Description=BlueALSA aplay service
After=bluetooth.service
Requires=bluetooth.service
 
[Service]
ExecStart=/usr/bin/bluealsa-aplay 00:00:00:00:00:00
 
[Install]
WantedBy=multi-user.target
Yes, use '00's for the <bdaddr>. Save the file. Enable the service with

Code: Select all

sudo systemctl enable aplay
Now, reboot.

Now you can pair and trust your phone (etc.) with the Pi, connect, and the phone's audio will play over the audio jack of the Pi.[/s]

[Most of this information comes from https://scribles.net/, which has many excellent guides for Bluetooth and BLE on the Pi]
Last edited by Douglas6 on Sat Oct 09, 2021 4:48 pm, edited 1 time in total.

User avatar
Pulsar33
Posts: 174
Joined: Sun Aug 25, 2019 4:06 pm
Location: Bordeaux France

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Sun Sep 22, 2019 5:53 pm

Hello

I was happy to find this guide because my first attempt when discovering the Pi4 was to install pulseaudio (as I used to do on my desktop). Unfortunately, it doesn't seem to be a good idea because many things don't work and the worst is that all sounds are set to mono instead stereo, which is unacceptable. However, I installed A2DP Sink with pulseaudio and it worked easily (but without stereo). So my Phone and my Pi4 can do the job.

So I tried your solution with ALSA on a fresh installed SD card. All is clear and easy to do, and seems to go well, except that there's no sound :(
At the same time, I can play audio with VLC for example, so the sound works on the Pi4. My Phone sees the Pi4, connects to it and displays "bluetooth audio" confirmation, with the corresponding ajusted audio level. The Pi4 sees my phone and register it as connected. All seems to be ok, but there's no sound.

I can see that :
$ systemctl list-units | grep ALSA
aplay.service loaded active running BlueALSA aplay service
bluealsa.service loaded active running BluezALSA proxy

Please, does someone have an idea of the problem ?
Best regards.
Pulsar33
Pi 4 with 4Gb memory, DVB TV pHAT ------------------- Desktop : Core i5 Linux MINT 19.3

User avatar
Douglas6
Posts: 5047
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Sun Sep 22, 2019 8:33 pm

Is your phone Android or iOS?

User avatar
Pulsar33
Posts: 174
Joined: Sun Aug 25, 2019 4:06 pm
Location: Bordeaux France

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Sun Sep 22, 2019 9:49 pm

None of these, but Ubuntu touch.
As said above, my phone works with A2DP Sink on Pi4 board if I install pulseaudio, but in mono and pulseaudio has other drawbacks.
BR. Pulsar33
Pi 4 with 4Gb memory, DVB TV pHAT ------------------- Desktop : Core i5 Linux MINT 19.3

Radiofreak1041
Posts: 3
Joined: Sun Sep 29, 2019 9:53 pm

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Mon Sep 30, 2019 5:20 pm

I got audio streaming to the Pi3B+ to work via Bluetooth (A2DP) using Bluealsa, but in mono. Is there anyone who got this to work with stereo audio? And is it possible to route this audio to another program like Stereo Tool that can modify and output it to the audio jack?

User avatar
Pulsar33
Posts: 174
Joined: Sun Aug 25, 2019 4:06 pm
Location: Bordeaux France

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Mon Sep 30, 2019 6:14 pm

Hello,

Would be happy to know how to make this work.
Someone has an idea ?

Best regards
Pulsar33
Pi 4 with 4Gb memory, DVB TV pHAT ------------------- Desktop : Core i5 Linux MINT 19.3

User avatar
Douglas6
Posts: 5047
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Mon Sep 30, 2019 6:25 pm

My Pi4 A2DP sink is working in stereo. No additional steps than the ones posted above.

Radiofreak1041
Posts: 3
Joined: Sun Sep 29, 2019 9:53 pm

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Wed Oct 02, 2019 6:19 pm

It's very nice to hear that it works for someone! Can you maybe provide some information about your setup? Do you use ALSA as well, or PulseAudio? And have you specified an audio playback device somewhere in a config file? What RPi and phone OS's are you using?
Sorry for all these questions, I'm just excited to hear it IS possible :D

User avatar
Douglas6
Posts: 5047
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Wed Oct 02, 2019 6:52 pm

More detail. I took a fresh Raspbian Buster image from the Raspberry website, updated and upgraded it on my Pi4 (this was 2 months ago). I then performed the steps listed in the first post of this thread. Exactly nothing more; exactly nothing less.

After reading your post, I found a stereo test audio file on the Web. With my Android 9 phone, I played the mp3 sample to my Pi with M-Audio AV40 speakers plugged into the audio jack. I clearly heard the words "front center" centered between the speakers, "side left" coming from (only) the left speaker, and "side right" coming from (only) the right speaker. The test file I used was the first link on this page: http://www.aoakley.com/articles/2018-08 ... o-test.php.

I think that's everything.

impersson
Posts: 1
Joined: Sun Oct 06, 2019 1:29 pm

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Sun Oct 06, 2019 1:32 pm

Douglas6 wrote:
Wed Oct 02, 2019 6:52 pm
More detail. I took a fresh Raspbian Buster image from the Raspberry website, updated and upgraded it on my Pi4 (this was 2 months ago). I then performed the steps listed in the first post of this thread. Exactly nothing more; exactly nothing less.

After reading your post, I found a stereo test audio file on the Web. With my Android 9 phone, I played the mp3 sample to my Pi with M-Audio AV40 speakers plugged into the audio jack. I clearly heard the words "front center" centered between the speakers, "side left" coming from (only) the left speaker, and "side right" coming from (only) the right speaker. The test file I used was the first link on this page: http://www.aoakley.com/articles/2018-08 ... o-test.php.

I think that's everything.
Hey I have the raspberry pi 4 and get the error

"Failed to enable unit: File aplay.service: Bad message"

when entering

"sudo systemctl enable aplay"

I tried some other guide before that didn't work so maybe I messed up my installation? Im running raspbian.

User avatar
Douglas6
Posts: 5047
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Mon Oct 07, 2019 11:26 am

Double check the the contents of aplay.service. You have mis-typed something.

foft
Posts: 1
Joined: Sun Dec 22, 2019 9:25 pm

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Sun Dec 22, 2019 9:32 pm

I tried this guide and its working well, many thanks.

I ran into a few issues and had to make these changes to get it working:
* stop rfkill from blocking bluetooth
* add User=pi to the new systemd service
* use bluetoothctl and set 'discoverable on' before searching with the iphone. Then type "trust 11:11:11..." (use the actual iphone id here).

vishwapinnawala
Posts: 1
Joined: Fri Dec 27, 2019 1:53 am

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Fri Dec 27, 2019 1:59 am

Hello, i tried this twise but the problem is after i execute "sudo systemctl enable aplay" and reboot it works but then if i restart my pi again it says "connection failed no usable service on this device"

FlixFlix
Posts: 1
Joined: Sun Jan 05, 2020 12:31 am

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Sun Jan 05, 2020 12:38 am

At one point we have to edit a file at

Code: Select all

/etc/systemd/system/aplay.service
but there's no such file at that location. Could it be somewhere else?

Running the official "Raspbian Buster with desktop and recommended software" from September 2019.

User avatar
Douglas6
Posts: 5047
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Sun Jan 05, 2020 3:52 pm

Create the file.

Man in a van
Posts: 292
Joined: Sat Dec 27, 2014 2:41 pm
Location: Staffordshire, UK.

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Mon Apr 06, 2020 9:48 am

@Douglas6

Thanks for this, ran first time, simples ;)

ronnie

sep945366
Posts: 61
Joined: Fri May 31, 2019 3:59 pm

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Sun Apr 12, 2020 11:39 pm

Hi guys, I have a question about the audio jack. I ran the code above and everything installed and worked nicely. But after I restarted my pi, the audio switched to my Tv / monitors speakers instead of the audio jack. Is there a way to make it go to the audio jack by default?

sep945366
Posts: 61
Joined: Fri May 31, 2019 3:59 pm

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Mon Apr 13, 2020 2:26 pm

I figured out the audio jack. Thanks though!

rsiquijor
Posts: 1
Joined: Thu May 14, 2020 5:15 pm

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Thu May 14, 2020 5:22 pm

Hi! I've been getting crazy with this issue. I'm using an RPI3A+ and I have a facial recognition program that talks when a face is detected. It's working when I put these configuration in my asoundrc:

defaults.bluealsa.interface "hci0"
defaults.bluealsa.device "xx:xx:xx:xx:xx:xx"
defaults.bluealsa.profile "a2dp"
defaults.bluealsa.delay 10000

However, I lose the voice of my Google Assistant using the bluetooth speaker running on the same device. If I connect a jack to a speaker, the Google can spit-out the voice on the speaker.

Following the instructions, I can make the Google assistant talk using the bluetooth speaker, but then I lose the voice of the facial recognition system.

I would really appreciate any help.

Thanks in advance!

siki95
Posts: 1
Joined: Sat Oct 03, 2020 5:22 pm

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Sat Oct 03, 2020 5:31 pm

Hi,

great guide, thanks! I got it working, but only with HDMI. It is not working with the 3.5mm jack. Any hints on how to get that working?

The 3.5mm is jack is working with any application on the Pi (e.g. youtube in chromium) other than bluetooth therefore I know it's not the 3.5mm jack itself. I can even play something over the jack and the bluetooth signal over hdmi in parallel. I am using the latest Raspbian Buster (2020-08-20) on the Raspberry Pi 4.

I would appreciate any help in getting the 3.5mm jack working with bluetooth. Thanks in advance!

Greetings,

littletinman
Posts: 6
Joined: Mon Jan 25, 2016 3:58 am

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Tue Oct 20, 2020 2:56 pm

Great guide! However, as others have said, I cannot get the audio to go out the 3.5 jack. If someone who figured out how to fix that could post a solution, that would be wonderful!

ockers
Posts: 3
Joined: Mon Jun 24, 2019 3:33 am

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Sun Nov 29, 2020 1:07 am

This was very helpful. I had ALSA set up on the Raspberry Pi 3B+ with a HiFiBerry AMP+ card connected to some speakers. I wanted to use the speakers as a bluetooth audio sink.

I had to add these lines to /etc/rc.local at startup to turn on bluetooth discoverability and enable pairing:
/bin/hciconfig hci0 piscan &
/bin/hciconfig hci0 sspmode 1 &

sspmode 1 is pairing with no passcode. Infuriatingly I can't get the passcode auth (mode 0) to work with a pin.conf on the bt-agent command line, every time I pair and enter the passcode on the android phone, I have to also start bluetoothctl and enter the same passcode at the bash prompt. This is super stupid since I want the use of this as a smart speaker to NOT require interacting with the linux command line.

I found that bluealsa sometimes won't start on the first try, so I added these lines to the service section of the aplay.service file as created above:

Code: Select all

[Service]
ExecStart=/usr/bin/bluealsa-aplay -v 00:00:00:00:00:00
Restart=on-failure
Restart=always
RestartSec=30
This makes it always restart if the bluealsa-aplay dies for any reason.

I also have the following /etc/systemd/system/bt-agent.service file:

Code: Select all

$ cat bt-agent.service 
[Unit]
Description=Bluetooth Auth Agent
After=bluetooth.service
PartOf=bluetooth.service

[Service]
Type=simple
ExecStart=/usr/bin/bt-agent -c NoInputNoOutput
ExecStartPost=/bin/sleep 1
ExecStartPost=/bin/hciconfig hci0 piscan
ExecStartPost=/bin/hciconfig hci0 sspmode 1


[Install]
WantedBy=bluetooth.target
I also ran these commands to start them and make sure they start at boot:

Code: Select all

sudo systemctl enable bt-agent
sudo systemctl enable bluealsa-aplay
sudo systemctl start bt-agent
sudo systemctl start bluealsa-aplay
Changing the audio output volume using the volume keys on the phone DOES NOT WORK. There seems to be no way to set the volume. I checked bluetooth events by watching the output of "sudo dbus-monitor --system" and I see all the pairing, connect, playback start/stop events, but no events when I press the volume up/down. If there was any event I could listen on, I can change the volume 1+ and 1- using a command like this: "amixer sset 'Master' 1+" But so far there seems to be no way to make this happen when I press volume keys on the Android phone while playing media.

I wonder if this patch to bluealsa-aplay will eventually work if it gets merged in and shows up in the raspbian build: https://github.com/hmbedded/bluez-alsa/ ... 78ebc4c76a

hec
Posts: 1
Joined: Sun Dec 20, 2020 9:05 am

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth

Sun Dec 20, 2020 9:15 am

Pulsar33 wrote:
Sun Sep 22, 2019 5:53 pm
Hello

I was happy to find this guide because my first attempt when discovering the Pi4 was to install pulseaudio (as I used to do on my desktop). Unfortunately, it doesn't seem to be a good idea because many things don't work and the worst is that all sounds are set to mono instead stereo, which is unacceptable. However, I installed A2DP Sink with pulseaudio and it worked easily (but without stereo). So my Phone and my Pi4 can do the job.

So I tried your solution with ALSA on a fresh installed SD card. All is clear and easy to do, and seems to go well, except that there's no sound :(
At the same time, I can play audio with VLC for example, so the sound works on the Pi4. My Phone sees the Pi4, connects to it and displays "bluetooth audio" confirmation, with the corresponding ajusted audio level. The Pi4 sees my phone and register it as connected. All seems to be ok, but there's no sound.

I can see that :
$ systemctl list-units | grep ALSA
aplay.service loaded active running BlueALSA aplay service
bluealsa.service loaded active running BluezALSA proxy

Please, does someone have an idea of the problem ?
Best regards.
Pulsar33


Hello Pulsar33,

I have the SOLUTION to your problem (at this pooint of time I am not sure if you already solved it anyway). The problem is that you need to specify the user in the aplay.service file. The example given here did not do it. The following worked for me:

Code: Select all

[Unit]
Description=BlueALSA aplay service
After=bluetooth.service
Requires=bluetooth.service

[Service]
User=pi
ExecStart=/usr/bin/bluealsa-aplay 00:00:00:00:00:00
Restart=always

[Install]
WantedBy=multi-user.target
I discovered this solution because I stopped aplay.service and ran bluealsa-aplay 00:00:00:00:00:00 directly and it worked.

Hope it helps.

Regards,
Hector

Muskateer10
Posts: 17
Joined: Tue Apr 10, 2018 6:06 pm

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth [solved]

Tue Apr 20, 2021 4:44 pm

This did not work for me on Raspbian Stretch (I updated to the latest stretch on 4/19/2021)
**I won't get into all the trouble that upgrade caused. LoL
System
Raspberry PI 3B+ with Samsung 250GB SSD drive (booting off of it).

Although systemctl status aplay showed that the aplay.service was "dead," it showed it as being enabled.

I added the following statements after ExecStart (located in the /etc/systemd/system/aplay.service file).
Restart=on-failure
RestartSec=1 ( I arbitrarily selected this number of seconds).

After rebooting everything is now active (enabled and started) as it should be.
And I streamed 1 .mpg file over HDMI (I didn't hear anything out of my headphones though when I plugged them in to the Pi3B).

Note 1: I also added under [Unit] : After=multi-user.target (although this may not be necessary).
Note 2: I have a Pi 4 with Buster installed (same SSD setup) but I have not tried this on that system yet.

MechWheels
Posts: 2
Joined: Wed May 19, 2021 6:28 pm

Re: [Guide] Stream audio to the Raspberry Pi over Bluetooth [solved]

Wed May 19, 2021 6:32 pm

Really helpful guide! I'm trying to do the opposite-- feed a microphone plugged into my Pi. What do I need to change to accomplish this?
Last edited by MechWheels on Wed May 19, 2021 7:13 pm, edited 1 time in total.

Return to “Graphics, sound and multimedia”