This is a revised version of the original post by phweyland with some minor fixes and latest version of Bluez. You can find the original post here:
volumio-bluez-alsa-a2dp-bluetooth-support-t6130.html
What this can do:
Allow Raspberry Pi to receive Bluetooth audio (from phone) and play though your DAC.
What this can’t do:
Output audio to Bluetooth Speakers.
Limitations:
No pairing interface, you will need to pair manually.
You will need to disconnect your phone’s Bluetooth->Volumio connection yourself if you want to play from other sources after.
You can skip the latest Bluez version if you want but I found it is more stable to use.
I have tested this on Rpi 3, Rpi Zero W and Rpi 1 model B with CSR8510 dongle.
I recommend you use SSH from a computer it will make this very much a copy and paste exercise.
Login as volumio with your password you should be in /home/volumio
Install dependencies:
sudo apt-get update
sudo apt-get install dh-autoreconf libasound2-dev libortp-dev pi-bluetooth
sudo apt-get install libusb-dev libglib2.0-dev libudev-dev libical-dev libreadline-dev libsbc1 libsbc-dev
Compile Bluez 5.48: (The make process will take around 25min, just so you know)
[code]git clone git://git.kernel.org/pub/scm/bluetooth/bluez.git
cd bluez
git checkout 5.48
./bootstrap
./configure --enable-library --enable-experimental --enable-tools
make
sudo make install
sudo ln -s /usr/local/lib/libbluetooth.so.3.18.16 /usr/lib/arm-linux-gnueabihf/libbluetooth.so
sudo ln -s /usr/local/lib/libbluetooth.so.3.18.16 /usr/lib/arm-linux-gnueabihf/libbluetooth.so.3
sudo ln -s /usr/local/lib/libbluetooth.so.3.18.16 /usr/lib/arm-linux-gnueabihf/libbluetooth.so.3.18.16[/code]
Compile Bluez-Alsa
cd
git clone https://github.com/Arkq/bluez-alsa.git
cd bluez-alsa
autoreconf --install
mkdir build && cd build
../configure --disable-hcitop --with-alsaplugindir=/usr/lib/arm-linux-gnueabihf/alsa-lib
make
sudo make install
Configure Bluetooth subsystem:
Create file /etc/bluetooth/audio.conf
sudo nano /etc/bluetooth/audio.conf
add
[General]
Class = 0x20041C
Enable = Source,Sink,Media,Socket
Update file /etc/bluetooth/main.conf
sudo nano /etc/bluetooth/main.conf
add
[General]
Class = 0x20041C
Automate BluezAlsa:
Set BlueAlsa as a service
Create file /lib/systemd/system/bluealsa.service
sudo nano /lib/systemd/system/bluealsa.service
Add
[Unit]
Description=BluezAlsa proxy
Requires=bluetooth.service
After=bluetooth.service
[Service]
Type=simple
User=root
Group=audio
ExecStart=/usr/bin/bluealsa
[Install]
WantedBy=multi-user.target
Enable BluezAlsa starts from boot:
sudo systemctl daemon-reload
sudo systemctl enable bluealsa.service
Set bluealsa-aplay as a service:
Create file /lib/systemd/system/bluealsa-aplay@.service
hw1:0 is the hifiberry audio device I want to use. There may be better way to link it.
sudo nano /lib/systemd/system/bluealsa-aplay@.service
Add
[Unit]
Description=BlueAlsa-Aplay %I -dhw:1,0
Requires=bluetooth.service bluealsa.service
[Service]
Type=simple
User=volumio
Group=audio
ExecStart=/usr/bin/bluealsa-aplay %I -dhw:1,0
[Install]
WantedBy=multi-user.target
Add UDEV rules
Create file /etc/udev/rules.d/99-input.rules
sudo nano /etc/udev/rules.d/99-input.rules
Add
KERNEL=="input[0-9]*", RUN+="/home/volumio/a2dp-autoconnect"
Tell Bluetooth how to create a bluetooth->Alsa connection:
Create file /home/volumio/a2dp-autoconnect
nano /home/volumio/a2dp-autoconnect
Add
[code]#!/bin/bash
at each BT connection/disconnection start/stop the service bluealsa-aplay
function log {
sudo echo “[$(date)]: $*” >> /var/log/a2dp-autoconnect
}
BTMAC=${NAME//"/}
if [ echo $BTMAC | egrep "^([0-9A-F]{2}:){5}[0-9A-F]{2}$"
]
then
if [ $ACTION = “remove” ]
then
log "Stop Played Connection " $BTMAC
sudo systemctl stop bluealsa-aplay@$BTMAC
elif [ $ACTION = “add” ]
then
log "Start Played Connection " $BTMAC
sudo systemctl start bluealsa-aplay@$BTMAC
else
log "Other action " $ACTION
fi
fi[/code]
Set the right access permissions:
sudo chmod a+rwx /home/volumio/a2dp-autoconnect
sudo touch /var/log/a2dp-autoconnect
sudo chmod a+rw /var/log/a2dp-autoconnect
REBOOT
After reboot, use bluetoothctl to connect to your bluetooth music source:
[code]sudo bluetoothctl
power on
agent on
default-agent
scan on => xx:xx of your device
pair xx:xx
trust xx:xx
exit[/code]
On your mobile, connect volumio. Should work.
Once the device is connected you should be able to play something …
6.Checking
To check if the services are all up and running: $ systemctl | grep blue
You should get something like that:
sys-subsystem-bluetooth-devices-hci0.device loaded active plugged /sys/subsystem/bluetooth/devices/hci0
sys-subsystem-bluetooth-devices-hci0:11.device loaded active plugged /sys/subsystem/bluetooth/devices/hci0:11
bluealsa-aplay@68:FB:7E:24:25:52.service loaded active running BlueAlsa-Aplay 68:FB:7E:24:25:52 -dhw:1,0
bluealsa.service loaded active running BluezAlsa proxy
bluetooth.service loaded active running Bluetooth service
system-bluealsa\x2daplay.slice loaded active active system-bluealsa\x2daplay.slice
bluetooth.target loaded active active Bluetooth