Skip to content

Instantly share code, notes, and snippets.

@Juul
Last active September 20, 2020 17:03
Show Gist options
  • Select an option

  • Save Juul/a13733997f0dad5b234dd754f3a78af8 to your computer and use it in GitHub Desktop.

Select an option

Save Juul/a13733997f0dad5b234dd754f3a78af8 to your computer and use it in GitHub Desktop.
Set up raspberry pi 3 as bluetooth sink (raspbian buster)

If you want to use pulseaudio, see BaReinhard's repo.

If you want to use bluealsa and avoid pulseaudio, keep reading.

So far this solution has two problems I haven't solved:

  • A ~450 ms delay (which can be compensated for in pavucontrol on the client)
  • If more than one source connects at the same time bluealsa-aplay crashes and bluealsa also needs restarting

If you have pulseaudio installed, make sure /etc/pulse/default.pa has the following two lines commented out and restart pulseaudio:

#load-module module-bluetooth-policy

#load-module module-bluetooth-discover

Install bluealsa:

apt install bluealsa

Get bluealsa to autostart like so:

https://github.com/Arkq/bluez-alsa/wiki/Systemd-integration

But add the following line to /etc/default/bluealsa:

LIBASOUND_THREAD_SAFE=0

Get bluealsa-aplay to restart by adding /etc/systemd/system/bluealsa-aplay.service:

[Unit]
Description=Bluealsa-aplay daemon
Documentation=https://github.com/Arkq/bluez-alsa/
After=bluealsa.service
Requires=bluealsa.service
StopWhenUnneeded=true

[Service]
Type=simple
Environment="LIBASOUND_THREAD_SAFE=0"
ExecStart=/usr/bin/bluealsa-aplay 00:00:00:00:00:00

[Install]
WantedBy=multi-user.target

Then:

systemctl daemon-reload
systemctl enable bluealsa-aplay.service
systemctl start bluealsa-aplay.service

In /etc/bluetooth/main.conf:

# Default device class. Only the major and minor device class bits are
# considered. Defaults to '0x000000'.
Class = 0x200414

# How long to stay in discoverable mode before going back to non-discoverable
# The value is in seconds. Default is 180, i.e. 3 minutes.
# 0 = disable timer, i.e. stay discoverable forever
DiscoverableTimeout = 0

# How long to stay in pairable mode before going back to non-discoverable
# The value is in seconds. Default is 0.
# 0 = disable timer, i.e. stay pairable forever
PairableTimeout = 0

Now you'll need a script to auto-trust connecting clients.

Use this script to auto-trust:

/etc/init.d/bluetooth-agent

which needs these two files:

/usr/local/bin/simple-agent.autotrust

/usr/local/bin/bluezutils.py

Copy these to your local filesystem, then make it auto-start:

git clone https://github.com/BaReinhard/Super-Simple-Raspberry-Pi-Audio-Receiver-Install
cd Super-Simple-Raspberry-Pi-Audio-Receiver-Install/
sudo cp -a init.d/bluetooth-agent /etc/init.d/
sudo cp -a usr/local/bin/simple-agent.autotrust /usr/local/bin/
sudo cp -a usr/local/bin/bluezutils.py /usr/local/bin/
sudo update-rc.d bluetooth-agent defaults

Make sure you have python 2.7 installed (yeah, i know...) and remove the reference to pulseaudio at the top of bluetooth-agent.

If using an external sound card, set default alsa sound card by first listing which cards are present:

cat /proc/asound/cards

Noting which number your preferred card has (probably 1) and then creating /etc/asound.conf with:

pcm.!default {
    type hw
    card 1
}

ctl.!default {
    type hw           
    card 1
}

Changing the card number if needed.

To set audio volume on boot use alsamixer to change it and then save with:

 sudo alsactl store

It should now all work. However if you're sending from a pulseaudio client then it might not work. Stop bluealsa-aplay and start it manually:

sudo bluealsa-aplay 00:00:00:00:00:00

Then try to play from the client again and if you get this output:

bluealsa: Unsupported RTP payload type: 1

Then you've run into this bug in the pulseaudio client.

It's been fixed, but as of Ubuntu 18.04 the bug is still there. There's a work-around but it requires recompiling bluez-alsa (bluealsa).

Here's how to recompile with the workaround:

Add this line to /etc/apt/sources.list:

# for libfdk-aac-dev
deb http://www.deb-multimedia.org/ buster main non-free

Then run:

gpg --keyserver pool.sks-keyservers.net --recv-keys 5C808C2B65558117
gpg --armor --export 5C808C2B65558117 | apt-key add -
apt update
apt install autoconf libtool libasound2-dev libbluetooth-dev libdbus-1-dev libfaad-dev libglib2.0-dev ofono-dev libsbc-dev libfdk-aac-dev
git clone https://github.com/Arkq/bluez-alsa
cd bluez-alsa/
autoreconf --install
mkdir build && cd build
../configure --enable-aac --enable-ofono --enable-debug --disable-payloadcheck
make
make install
# restart everything
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment