Skip to content

Instantly share code, notes, and snippets.

@nschikora
Last active July 10, 2019 20:56
Show Gist options
  • Save nschikora/01c23a7d748073235302d3f681acd807 to your computer and use it in GitHub Desktop.
Save nschikora/01c23a7d748073235302d3f681acd807 to your computer and use it in GitHub Desktop.
Raspberry Pi 3 HiFiBerry DAC+ Bluetooth audio A2DP sink

Raspberry Pi + HiFiBerry DAC+ A2DP sink

Preparation

Download a clean Raspbian Lite image from raspberrypi.org and flash it to an SD card.

For the HiFiBerry DAC+ to work we'll disable the onboard sound driver by commenting out the line

dtparam=audio=on

from /boot/config.txt if it exists. And then add this line to load the DAC+ driver instead.

dtoverlay=hifiberry-dacplus

More information on configuring the HiFiBerry hats can be found on hifiberry.com.

Create an empty file called ssh on the /boot partiotion of the SD card then instert it into the Pi and power it up.

On the Pi

ssh onto the Pi. Username is pi, default password is raspberry.

ssh pi@raspberrypi

Good practive would be to change the default password, to disable root login via SSH and to disable password login via SSH.

Update everything and reboot, then ssh onto the Pi again.

sudo apt-get update
sudo apt-get upgrade
sudo reboot

Test that the HiFiBerry DAC+ is available for use. Running aplay -l should give an output similar to this:

**** List of PLAYBACK Hardware Devices ****
card 0: sndrpihifiberry [snd_rpi_hifiberry_dacplus], device 0: HiFiBerry DAC+ Pro HiFi pcm512x-hifi-0 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0

Install the bluetooth audio module for pulse along with pulse itself and ofono.

sudo apt-get install pulseaudio-module-bluetooth ofono

Edit the bluetooth config /etc/bluetooth/main.conf. You want to change the Class to 0x00041C. And make sure that AutoEnable is set to true

Class = 0x00041C
AutoEnable = true

Continue with editing the pulse configuration. We'll start pulse with the system so go for /etc/pulse/system.pa and add the following lines.

load-module module-bluetooth-discover
load-module module-bluetooth-policy
load-module module-switch-on-connect

Then add a systemd service that automatically starts pulse on boot.

sudo nano /etc/systemd/system/pulseaudio.service

You can use this snippet. Depending on the other tasks your Pi has to do, you may want to adjust the resampling method and cpu limit. Consult man pulse-daemon.conf for more information.

[Unit]
Description=PulseAudio Daemon

[Install]
WantedBy=multi-user.target

[Service]
Type=simple
PrivateTmp=true
ExecStart=/usr/bin/pulseaudio --system --realtime --disallow-exit --no-cpu-limit --exit-idle-time=-1 --resample-method=speex-float-9

Enable the new service so it is run on boot.

sudo systemctl enable pulseaudio 

We need to make sure that pulse has permissions to use bluetooth and ofono. Edit /etc/dbus-1/system.d/bluetooth.conf and add the following policy:

<policy user="pulse">
    <allow send_destination="org.bluez"/>
    <allow send_destination="org.ofono"/>
</policy>

Finally do a reboot because why not.

sudo reboot

To connect a device do the following in a sudo bluetoothctl

pairable on
discoverable on

Discover and pair the device using your A2DP source. The device will most likely connect and immediately disconnect again like this:

[CHG] Device FC:18:AA:BB:CC:DD Connected: yes
[CHG] Device FC:18:AA:BB:CC:DD Connected: no

Trust the device like so and you should be good to go.

trust FC:18:AA:BB:CC:DD

To automatically reconnect the device when the Pi boots up you can set up another system service. Open sudo nano /etc/systemd/system/autoconnect.service and add this snippet:

[Unit]
Description=Autoconnect
After=multi-user.target

[Service]
Type=idle
ExecStart=bluetoothctl -- connect FC:18:AA:BB:CC:DD

[Install]
WantedBy=multi-user.target

And enable it running sudo systemctl enable autoconnect.service.

Automatic discovery and paring/trusting is not super easy and requires some scripting.

Sources

https://wiki.archlinux.org/index.php/Bluetooth_headset

https://www.hifiberry.com/build/documentation/configuring-linux-3-18-x/

https://raspberrypi.stackexchange.com/questions/4444/enabling-ssh-on-rpi-without-screen-keystrokes-for-raspi-config

https://raspberrypi.stackexchange.com/questions/42265/pulseaudio-a2dp-bluetooth-dont-work-in-system-mode-but-work-work-fine-under

https://bugzilla.redhat.com/show_bug.cgi?id=514992

https://linux.die.net/man/5/pulse-daemon.conf

https://gist.github.com/kingster/3784fa2f0de1f5ce4418

https://pythonhosted.org/BT-Manager/config.html

https://stackoverflow.com/questions/34709583/bluetoothctl-set-passkey

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment