3 3
guidol

BlueALSA: Bluetooth-Audio using ALSA (not PulseAudio)

Recommended Posts

Yesterday I did get a new little Bluetooth-Speaker, but without AUX - I rechecked some Bluetooth-Commands.

But could never connect,  because the Pi missed the protocol A2DP - which was only available with PulseAudio as Bluetooth-Module :(

 

So I used my 2nd OrangePi One with USB-Bluetooth-Dongle and mainline-kernel for this challenge:
ARMBIAN 5.38 stable Debian GNU/Linux 9 (stretch) 4.14.18-sunxi

Linux opi-one-main 4.14.18-sunxi #24 SMP Fri Feb 9 16:24:32 CET 2018 armv7l GNU/Linux


I like ALSA more than PulseAudio - I searched for ALSA and Bluetooth-Audio - and did found no good news about Audio and Bluetooth while using ALSA.
At the second search I did found the Project

BlueALSA (formerly know as BluezALSA) :  https://github.com/Arkq/bluez-alsa

 

While BlueALSA isnt available as .deb-package (and if then only on the RPi) I did use the following sites for documentation:

 

As Dependencies I had to install the following packages:
apt-get install libasound2-dev dh-autoreconf libortp-dev bluez bluetooth bluez-tools libbluetooth-dev libusb-dev libglib2.0-dev libudev-dev libical-dev libreadline-dev libsbc1 libsbc-dev libfdk-aac-dev

 

libfdk-aac-dev is one more than at the RPi-Documetation page.

 

For creating / compiling BlueALSA I did use the follwoing commands:
cd /home/guido
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

 

When BlueALSA has been created, then you can start BlueALSA as background task:
bluealsa --disable-hfp &

 

Now - for using your bluetooth-speaker/headset - with BlueALSA you have to intoduce the device to the bluetooth-stack using bluetoothctl:
bluetoothctl >
scan on
[NEW] Device 30:23:23:F4:48:2C TH-S10U
scan off
pair 30:23:23:F4:48:2C
trust 30:23:23:F4:48:2C
exit

 

After that power off&on the bt-speaker device and the device will connect automatically as a trusted device.

 

Before we try our first audio-command we had to export one thing to make BlueALSA mor system-friendly:
export LIBASOUND_THREAD_SAFE=0because of a message while compiling:

Spoiler

 

configure: WARNING:               *** alsa-lib >= 1.1.2 ***
configure: WARNING: Starting from alsa-lib 1.1.2, it is possible to enable
configure: WARNING: thread-safe API functions. Unfortunately, this feature
configure: WARNING: is not mature enough - software plugins may experience
configure: WARNING: random deadlocks (bluez-alsa PCM plugin is affected).
configure: WARNING: Either compile alsa-lib without this feature or export
configure: WARNING: LIBASOUND_THREAD_SAFE=0 while using bluealsa PCM.

 

 

Now you can try your first commands for getting audio out of the device:

 

Text to speech:

espeak "Hello, how are you?" -w /home/guido/espeak.wav -s145
aplay -D bluealsa:HCI=hci0,DEV=30:23:23:F4:48:2C,PROFILE=a2dp /home/guido/espeak.wav

 

Internetradio:

mpg123 -a bluealsa:HCI=hci0,DEV=30:23:23:F4:48:2C,PROFILE=a2dp -@ /home/guido/ffh80s.pls

 

For shorting the command you could create a ~/.asoundrc with the following content:

defaults.bluealsa.interface "hci0"
defaults.bluealsa.device "30:23:23:F4:48:2C"
defaults.bluealsa.profile "a2dp"
defaults.bluealsa.delay 10000

 

Now you can the shorter version of the command:

mpg123 -a bluealsa -@ /home/guido/ffh80s.pls

 

For set the volume of such a device you have to know the "real" name of the device out of the bluetoothctl and the name of the used protocol:

Device: 30:23:23:F4:48:2C TH-S10U
Protocol: A2DP
amixer -D bluealsa sset 'TH-S10U - A2DP ' 70%

 

Here as information the dmesg-part of my bluetooth-dongle:

Spoiler

[346859.531251] usb 4-1: new full-speed USB device number 2 using ohci-platform
[346859.807299] usb 4-1: New USB device found, idVendor=0a12, idProduct=0001
[346859.807317] usb 4-1: New USB device strings: Mfr=0, Product=2, SerialNumber=0
[346859.807329] usb 4-1: Product: CSR8510 A10
[346859.963820] Bluetooth: Core ver 2.22
[346859.963906] NET: Registered protocol family 31
[346859.963910] Bluetooth: HCI device and connection manager initialized
[346859.963928] Bluetooth: HCI socket layer initialized
[346859.963938] Bluetooth: L2CAP socket layer initialized
[346859.963965] Bluetooth: SCO socket layer initialized
[346859.975588] usbcore: registered new interface driver btusb
[346860.184710] Bluetooth: BNEP (Ethernet Emulation) ver 1.3
[346860.184718] Bluetooth: BNEP filters: protocol multicast
[346860.184734] Bluetooth: BNEP socket layer initialized
[346901.432957] Bluetooth: RFCOMM TTY layer initialized
[346901.432979] Bluetooth: RFCOMM socket layer initialized
[346901.432998] Bluetooth: RFCOMM ver 1.11
[347059.966606] input: 30:23:23:F4:48:2C as /devices/virtual/input/input1

 

Share this post


Link to post
Share on other sites

Hi Guido.

I have been searching for a way to play audio on my blue tooth speaker for ages and was really excited when I saw your post.

I compiled it according to your instructions and I can see and pair my speaker.But when I try to play a file I get this error

Spoiler

ALSA lib ../../../src/asound/bluealsa-pcm.c:679:(_snd_pcm_bluealsa_open) Couldn't get BlueALSA transport: No such device
aplay: main:722: audio open error: No such device

My device shows as a  ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)

I am using the latest Armbian.

Do you have any suggestions for me?

Thanks.

J.

 

Share this post


Link to post
Share on other sites
12 hours ago, Johhny Blue said:

But when I try to play a file I get this error

  Hide contents

ALSA lib ../../../src/asound/bluealsa-pcm.c:679:(_snd_pcm_bluealsa_open) Couldn't get BlueALSA transport: No such device
aplay: main:722: audio open error: No such device

My device shows as a  ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)

I am using the latest Armbian.

Do you have any suggestions for me?

First did you start blueALSA in the backgrund with bluealsa --disable-hfp &

Could you write the output of the command "devices" inside of bluetoothctl? (whats the name and id of your speaker?)

 

Did you pair & trust your BT-speaker inside of bluetoothctl?

Does BT-Speaker play the connect-sound and isnt blinking anymore?

 

ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode) seems to be the address of your USB-BT(-Dongle)

 

Did you create a  ~/.asoundrc (in the homedirectoty of your user or root if you using root) with the default-definitons of bluealsa?:

defaults.bluealsa.interface "hci0"
defaults.bluealsa.device "30:23:23:F4:48:2C"   <==== here the id of your BT-Speaker
defaults.bluealsa.profile "a2dp"
defaults.bluealsa.delay 10000

 

(the default will only apply to this one speaker which has this id - if you want to play on another speaker/device you had to add the parameters to the commandline of the audio-command)

 

If not you have to add the parameters for id and profile to the commandline.

 

I have tested it today with 2 additional speakers, and the connect didnt work directly, but after starting bluealsa and then a moment later

the BT-speaker will auto-connect if trusted:

Device 30:21:8E:AA:4C:45 ML-28U
Device 30:23:23:F4:48:2C TH-S10U
Device 00:11:67:3F:9B:18 BTLS9001

 

If you didnt want to auto-connect or it wouldnt autoconnect you can connect to a paired&trusted BT-Speaker with the command:
echo -e "connect 00:11:67:3F:9B:18\nquit" | bluetoothctl     <=== here you also need to change the id to the id of your BT-Speaker

 

Disconnect is the same:

echo -e "disconnect 00:11:67:3F:9B:18\nquit" | bluetoothctl

 

You could also change the commandline here for pairing or trusting a device like

echo -e "pair 00:11:67:3F:9B:18\trust 00:11:67:3F:9B:18\nquit" | bluetoothctl

 

Which command did you use to play a sound? (please copy the full command line)

Share this post


Link to post
Share on other sites

Hi Guidol:

 

Great post. For me everything worked great, nevertheless i can not find the way to auto connect my bluetooth speaker even as the trust command finished successfully with the bluetoothctl.

 

Any ideas?

 

Thanks, Mauricio F.

Share this post


Link to post
Share on other sites
16 minutes ago, mflorezm said:

Great post. For me everything worked great, nevertheless i can not find the way to auto connect my bluetooth speaker even as the trust command finished successfully with the bluetoothctl.

Any ideas?

 

Normally your speaker should auto-connect (after areboot) when the speaker is paired & trusted before via bluetoothctl

Or you can try it without trust and do a connect via commandline (see above) at startup (e.g. /etc/rc.local)

Share this post


Link to post
Share on other sites

Hi guidol:

 

Your suggestion did not work (adding to /etc/rc.local). And again it did not auto connect even it is already trusted.

 

Fortunately, i found a solution adding a cron job to connect

 

sudo crontab -e

@reboot sleep 5 && /bin/echo -e 'connect 00:58:56:7A:9F:0D \n quit \n' | bluetoothctl

 

Regards,

 

Mauricio F.

Share this post


Link to post
Share on other sites

..so another talking with myself .. :D

 

Here is some scripts to automate pairing and playing,

if you can use Opi as Bluetooth speaker.


script for automated pairing ...

Quote

/lib/systemd/system/bluetooth-bt-agent.service

[Unit]
Description=Bluetooth Simple Agent
Requires=bluetooth.service
After=bluetooth.service
[Service]
Type=simple
#User=root
#Group=audio
ExecStart=/usr/bin/bt-agent -c NoInputNoOutput -p /etc/bluetooth/pin.file
[Install]
WantedBy=multi-user.target

 

we need to create PIN file list
 

Quote

/etc/bluetooth/pin.file

# accept pin 1234 for all devices

* 1234

# accept user pin for selected device

11:22:33:AA:BB:CC 0911

 

and run bluealsa & bluealsa-aplay as service on startup...

 

Quote

/lib/systemd/system/bluealsa.service

[Unit]
Description=BluezAlsa proxy
Requires=bluetooth.service bluetooth-bt-agent.service
After=bluetooth.service
[Service]
Type=simple
#User=root
#Group=audio
ExecStart=/usr/bin/bluealsa -S -p a2dp-sink -p hfp-hf -p hsp-hs
[Install]
WantedBy=multi-user.target

 

 

..and play some sounds..
 

Quote

/lib/systemd/system/bluealsa-aplay.service

[Unit]
Description=BlueAlsa-Aplay on ALL DEVICES
Requires=bluetooth.service bluetooth-bt-agent.service bluealsa.service
After=bluealsa.service
[Service]
Type=simple
#User=root
#Group=audio
ExecStart=/usr/bin/bluealsa-aplay 00:00:00:00:00:00
[Install]
WantedBy=multi-user.target

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
3 3