Page 2 of 2

Re: Use bluetooth audio on Raspberry PI

Posted: Sunday 22 November 2015 16:41
by marigo
If you use mplayer with the "-softvol" option then you can control the volume from the command line with "9" or "0".

I am searching for an option to control the volume within domoticz with a slider button like the dimmer option. Somehow you have to parse the keystroke "9" or "0" to the mplayer proces but at this moment I am searching on how to accomplish this.

Maybe anyone have some experience to get this working?

EDIT:
This is a Lua script with a "dimmer" to control volume, but I can change it only once:

Code: Select all

commandArray = {}

if (devicechanged['TEST'] == 'Set Level') then
        v = math.floor(otherdevices_svalues['TEST'] - 20)
        os.execute("mplayer -ao alsa:device=bluetooth -af volume='" .. v .."' /mnt/music/top40/*.mp3")
        print("radio volume " .. v)
end

return commandArray


In the log I see that the script is running for more then 10 seconds which means a loop somewhere.

Can someone advise?

EDIT2:

I have found something to control the volume and play the next track with FIFO and this is working within Domoticz

https://pbrisbin.com/posts/controlling_mplayer/

I have made three "one-push-buttons" and three scripts

1- volume up

Code: Select all

#!/bin/bash
sudo echo volume 1 > /root/.mplayer_fifo &
2- volume down

Code: Select all

#!/bin/bash
sudo echo volume - > /root/.mplayer_fifo &
3 - next track

Code: Select all

#!/bin/bash
sudo echo pausing_keep_force pt_step 1 > /root/.mplayer_fifo &
You need the following line to play your audio via your bluetooth adapter:

Code: Select all

sudo mplayer -ao alsa:device=bluetooth -softvol -af volume=-20 /mnt/music/03-Albums/*.mp3 -shuffle &

Re: Use bluetooth audio on Raspberry PI

Posted: Tuesday 04 October 2016 15:41
by fransiefrans
In order to get the izSynth working, I had to connect my bluetooth speaker first.
Therefor I used the BTaudio Wiki but it looks like the bluez package changed.
I can't get it to connect with my speaker.
Is there someone who got it the work with the new bluez?

Re: Use bluetooth audio on Raspberry PI

Posted: Wednesday 08 February 2017 16:30
by Skaven
Hello, I'm wondering if somebody can explain to my how I setup this on my raspberry pi 3 white the integrated Bluetooth chip on raspberry pi 3 board, can I use the integrated Bluetooth chip? If yes can you explain how to setting it up?

Re: Use bluetooth audio on Raspberry PI

Posted: Thursday 09 February 2017 0:40
by pvm
Skaven wrote:Hello, I'm wondering if somebody can explain to my how I setup this on my raspberry pi 3 white the integrated Bluetooth chip on raspberry pi 3 board, can I use the integrated Bluetooth chip? If yes can you explain how to setting it up?
When following the wiki., what output / errors do you get?

Re: Use bluetooth audio on Raspberry PI

Posted: Monday 06 November 2017 21:33
by curious
I am trying to follow this wiki on my Raspberry 2.
Unfortunately I am stuck .

At first I get a message after entering

Code: Select all

sudo apt-get install bluetooth bluez bluez-utils bluez-alsa

Code: Select all

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package bluez-alsa is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

Package bluez-utils is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
However the following packages replace it:
  bluez

E: Package 'bluez-utils' has no installation candidate
E: Package 'bluez-alsa' has no installation candidate
Should not be a problem, but after that there is no audio.conf nor is there a bluetooth-agent (command not found)
I use jessie-lite

My bluetooth device is recognized when entering lsusb

What to do

Re: Use bluetooth audio on Raspberry PI

Posted: Tuesday 07 November 2017 13:46
by freijn
Very interesting this one as well.. Will read..

Re: Use bluetooth audio on Raspberry PI

Posted: Saturday 11 November 2017 8:49
by tontze

Re: Use bluetooth audio on Raspberry PI

Posted: Tuesday 10 April 2018 14:15
by DewGew
Is BTAudio out of date?
I tried to install bluetooth bluez bluez-utils bluez-alsa but i get this:

Code: Select all

Package bluez-alsa is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

Package bluez-utils is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
However the following packages replace it:
  bluez

E: Package 'bluez-utils' has no installation candidate
E: Package 'bluez-alsa' has no installation candidate
Is there an other way for this?

Re: Use bluetooth audio on Raspberry PI

Posted: Monday 17 December 2018 20:14
by ledfreak3d
yep no longer works wiki needs to be update

bt audio works fine from the command line
but will never work for me when run from a lua script
cant find why.

not on a rpi3 or on a oppc+ board

Re: Use bluetooth audio on Raspberry PI

Posted: Tuesday 01 January 2019 18:49
by solo111
ledfreak3d wrote: Monday 17 December 2018 20:14 yep no longer works wiki needs to be update

bt audio works fine from the command line
but will never work for me when run from a lua script
cant find why.

not on a rpi3 or on a oppc+ board
Good day! I join you .. I have the same trouble. My script does not start when I press the button. I read that this is due to the rights of the user. But I have a user in Domoticz by default. And what user do you have?

Re: Use bluetooth audio on Raspberry PI

Posted: Tuesday 08 January 2019 23:04
by ledfreak3d
prety sure its not a user acount issue tried root and pi does not make any difference

Re: Use bluetooth audio on Raspberry PI

Posted: Tuesday 19 March 2019 13:15
by AllesVanZelf
edit: 30 juli 2019 - see Thorgal789 post below and my post right after that.
I started with BT-audio from the wiki page also, but I soon have found out that this does not work anymore. I assume, because of some changes to the bluetooth packages. I’m certainly not an expert, but did get a little bit further.
I want to use this bluetooth speaker to make some alert sounds as feedback.

I used pulseaudio. I believe this is preferred audio system by newer distro’s.

Code: Select all

sudo apt install pulseaudio pulseaudio-utils pulseaudio-module-bluetooth

sudo nano /etc/bluetooth/main.conf
(/etc/bluetooth/audio.conf is not used anymore)

add (or change if exist):

Code: Select all

# This section contains general options
    [General]
    Enable=Source,Sink,Media,Socket
Change/add the following configuration in your /etc/pulse/system.pa config.

Code: Select all

Sudo nano /etc/pulse/system.pa

# Modify: /etc/pulse/system.pa

### Automatically load driver modules for Bluetooth hardware
.ifexists module-bluetooth-policy.so
load-module module-bluetooth-policy
.endif

.ifexists module-bluez5-discover.so
load-module module-bluez5-discover
.endif
and in this same file change, or add the line:

Code: Select all

load-module module-native-protocol-unix auth-anonymous=1 socket=/tmp/pulseaudio.socket
The /tmp/socket makes it possible to connect to pulseaudio with different users.

I added/changed the same lines in /etc/pulse/default.pa

In /etc/pulse/client.conf
change, or add the lines:

Code: Select all

default-server = unix:/tmp/pulseaudio.socket
autospawn = no
I had to remove all files from: /home/pi/.config/pulse/ and /root/.config/pulse/ to make this work.
Especially the client.conf file in there.

In /etc/pulse/daemon.conf I changed/added the line:

Code: Select all

 exit-idle-time = -1
 allow-module-loading = yes
 enable-memfd = yes
First stop pulse audio:

Code: Select all

 pulseaudio -k
Next, Start pulseaudio at boot as service (or daemon? I’m not sure about the difference)
For this part I used this source: link )

Code: Select all

 sudo nano /etc/systemd/system/pulseaudio.service 
And change or add:

Code: Select all

[Unit]
Description=PulseAudio system service

[Service]
Type=notify
ExecStart=/usr/bin/pulseaudio
Restart=on-failure

[Install]
WantedBy=multi-user.target

Now enable and start this unit file by running the following commands:

Code: Select all

 sudo systemctl --system enable pulseaudio.service
sudo systemctl --system start pulseaudio.service 
It should be running now. You can check the status of the service with the command:

Code: Select all

 sudo systemctl --system status pulseaudio.service
I got an error about a cookie file. I will report how I solved this later.

Now add the user who needs access to pulseaudio to the pulse and pulse-access groups:

Code: Select all

sudo usermod -aG pulse,pulse-access <username>
And I added root too.
sudo usermod -aG pulse,pulse-access root
change <username> with pi or your normal username.
Changes to groups are effective after you logout and log back in.

Bluetooth:
On ubuntu:

Code: Select all

sudo add-apt-repository ppa:bluetooth/bluez
sudo apt update
sudo apt upgrade
sudo apt install bluez bluetooth
sudo service bluetooth restart
sudo /etc/init.d/bluetooth start
or try using the method donwloading from kernel.org as described here: link

I'm using version 5.49

Code: Select all

bluetoothctl -v
5.49
I also installed Bluealsa. But now I'm not sure if this is nescesary.
source

Code: Select all

sudo apt install bluealsa

Type:

Code: Select all

sudo bluetoothctl 
then press enter and input the administrator password.
Next, enter

Code: Select all

[Bluetooth] agent on 
and press enter. Then type

Code: Select all

[Bluetooth] default-agent 
and press enter. Type

Code: Select all

[Bluetooth] scan on 
and press enter one more time. The unique addresses of all the Bluetooth devices around will appear and look something like an alphanumeric XX:XX:XX:XX:XX:XX. If you make the device you want to pair discoverable (or put it into pairing mode), the device nickname may appear to the right of the address. If not, you will have to do a little trial and error or waiting to find the correct device.
To pair the device, type

Code: Select all

[Bluetooth] pair XX:XX:XX:XX:XX:XX 
XX:XX:XX:XX:XX:XX is the device bluetooth address. Change it with the address of your speaker (for example)
Connect to the device, type

Code: Select all

 connect XX:XX:XX:XX:XX:XX 
Then:

Code: Select all

 trust XX:XX:XX:XX:XX:XX 
type 'exit' to leave bluetoothctl.


In order to pair up with Bluetooth devices, you need to add, or change the following text to the file /etc/dbus-1/system.d/pulse.conf:

Code: Select all

 sudo nano /etc/dbus-1/system.d/pulseaudio-system.conf
Check if this file exist. In my case there where two files. One pulse.conf en this one. I merged them to pulseaudio-system.conf.

Code: Select all

<!DOCTYPE busconfig PUBLIC
 "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
        <policy user="root">
                <allow own="org.pulseaudio.Server"/>
                <allow send_destination="org.bluez"/>
                <allow send_interface="org.bluez.Manager"/>
        </policy>
        <policy user="pulse">
                <allow own="org.pulseaudio.Server"/>
                <allow send_destination="org.bluez"/>
                <allow send_interface="org.bluez.Manager"/>
        </policy>
        <policy context="default">
                <deny own="org.pulseaudio.Server"/>
                <deny send_destination="org.bluez"/>
                <deny send_interface="org.bluez.Manager"/>
        </policy>
</busconfig>
According to this same source I changed/added in the file:
/etc/dbus-1/system.d/bluetooth.conf

Code: Select all

    <allow send_interface="org.bluez.ThermometerWatcher1"/>
    <allow send_interface="org.bluez.HeartRateWatcher1"/>
    <allow send_interface="org.bluez.CyclingSpeedWatcher1"/>

 <!-- allow users of bluetooth group to communicate -->
  <policy group="bluetooth">
    <allow send_destination="org.bluez"/>
  </policy>

It’s easiest to reboot the system now.

Code: Select all

sudo shutdown -r now
After this reboot, find out the name to use for the bluetooth device to make some noise.

Code: Select all

sudo pacmd list-sinks
(I still cannot get info without sudo. That I do not understand)
find the: bluez_sink.XX_XX_XX_XX_XX_XX.a2dp_sink” part. We will need this addres.
I use paplay like this:

Code: Select all

paplay -p --device=bluez_sink.XX_XX_XX_XX_XX_XX.a2dp_sink --volume=65536 /home/pi/sounds/dingdong-1.wav 
volume 65536 is volume set to 100%. So be careful. You could set it in a range of 0 – 65536. paplay does not play mp3’s. Mplayer can.
Another method I use is by using mplayer, like this:

Code: Select all

mplayer -ao pulse::bluez_sink.XX_XX_XX_XX_XX_XX.a2dp_sink /home/pi/1.mp3 -softvol -volume 25 
This is working well for me, but just for a while. After some time my bluetooth speaker disconnects. After that I had some trouble reconnecting.
In the next post I solved this.

Edit: some corrections
edit: 29 juli 2019 - made some changes

Re: Use bluetooth audio on Raspberry PI

Posted: Tuesday 19 March 2019 21:31
by AllesVanZelf
Keep Bluetooth connection alive
I did some more research and might found a way to keep bluetooth connection to the bluetoth speaker open.
But not sure yet if this will be the solution on the long run.
All feedback is more then welcome!

source 1
source 2

Make a script to reconnect the Bluetooth device:

Code: Select all

 sudo nano /home/pi/bin/autoconnect-bluetooth-device.sh
with content:

#!/bin/bash
bluetoothctl << EOF
connect XX:XX:XX:XX:XX:XX
EOF

Code: Select all

 chmod +x /home/pi/bin/autoconnect-bluetooth-device.sh
I save my own scripts in this /home/pi/bin directory.

The next script will check if the connection is still alive, and when not – start the last script.

Code: Select all

 sudo nano /home/pi/bin/connect-bt-devs.sh

#!/bin/bash
address="XX:XX:XX:XX:XX:XX"
while (sleep 1)
do
  connected=$(hcitool con) > /dev/null
  if [[ ! $connected =~ .*${address}.* ]] ; then
    /home/pi/bin/autoconnect-bluetooth-device.sh
  fi
done

Code: Select all

chmod +x /home/pi/bin/connect-bt-devs.sh
The next step creating a service to keep checking.

Code: Select all

sudo nano /etc/systemd/system/connectbtspeaker.service
with content:
[Unit]
Description=keep connection Bluetooth speaker alive
Documentation=https://www.domoticz.com/

[Service]
# Ubuntu/Debian convention:
Type=simple
ExecStart=/home/pi/bin/connect-bt-devs.sh

[Install]
WantedBy=multi-user.target
Before the last step, make sure the device/speaker is paired and connected. Then enable the service:

Code: Select all

 sudo systemctl enable connectbtspeaker.service
sudo systemctl start connectbtspeaker.service
sudo systemctl status connectbtspeaker.service
sudo systemctl stop connectbtspeaker.service

I’m not yet sure if the next command is nescesary.

Code: Select all

sudo systemctl preset connectbtspeaker.service
preset to set the service to be auto-started at boot, if the local policy permits it.
Connecting was not enough. The speaker go to sleep. for this I found another solution.

Update:
I found out from here: source, that it is possible to keep the bluetooth connection alive with a hcitool spinq command.
Now put this simple script into /etc/init.d/

Code: Select all

sudo nano /etc/init.d/bluetooth-alive
#! /bin/sh
hcitool spinq
exit 0
Then execute these.

Code: Select all

sudo chmod +x /etc/init.d/bluetooth-alive
sudo service bluetooth-alive enable
sudo update-rc.d bluetooth-alive defaults
sudo service bluetooth-alive start
Edit (29-7-2019):
If I do something silly or stupid, please tell me!! :) But the speaker is up and running for a few month now.

Re: Use bluetooth audio on Raspberry PI

Posted: Wednesday 20 March 2019 18:02
by Thorgal789
I m sorry, I can't test it now, I have uninstalled my alexa application for the moment (and I had similar problem too, and I haven't tested so much complicated script), but be sure it will be usefull on my side ^^.
Thx for all.

Re: Use bluetooth audio on Raspberry PI

Posted: Monday 29 July 2019 11:52
by AllesVanZelf
I have edited the above post a little, because I had some new findings.
I also have found out that I have to (re)start bluetooth after pulseaudio. else it will not work here.

Code: Select all

sudo service pulseaudio restart
sudo service bluetooth restart

Re: Use bluetooth audio on Raspberry PI

Posted: Tuesday 30 July 2019 11:49
by AllesVanZelf
hmmm still no stable connection. Anyone could review my settings?

Re: Use bluetooth audio on Raspberry PI

Posted: Tuesday 30 July 2019 17:15
by Thorgal789
Nothing to see with your method, but on my side I m using Bluealsa.
My speaker is connected 24/24, I can say if the connection is stable during a week, because I move it a lot for test ATM. But I m just using this command to send sound.

For Wav, realy stable

Code: Select all

aplay -D bluealsa:HCI=hci0,DEV=11:75:58:E4:71:30,PROFILE=a2dp sound.wav
for mp3, instable

Code: Select all

mpg123 -a bluealsa:HCI=hci0,DEV=11:75:58:E4:71:30,PROFILE=a2dp sound.mp3
I already have some problem with pulseaudio.

Re: Use bluetooth audio on Raspberry PI

Posted: Tuesday 30 July 2019 18:33
by AllesVanZelf
Hello Thorgal789,
I'm very interested in this. Could you tell me what your configuration files are?

Re: Use bluetooth audio on Raspberry PI

Posted: Tuesday 30 July 2019 19:10
by Thorgal789
All by defaut ^^.
Sound use rapsberry speaker by defaut like a defaut configuration.

The speaker configuration is on the command line and by-pass the defaut configuration, I think there a way a set it by defaut, but I had lot of problem with sound core and I don't want to broke it again so I m using this solution.

Code: Select all

sudo apt-get install bluealsa
Take care, perhaps conflicts with pulseaudio., don't remember all the requierement.

Code: Select all

pi@raspberrypi: $ amixer -D bluealsa
Simple mixer control 'TimeBox-mini-audio - A2DP',0
  Capabilities: pvolume pswitch
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 127
  Mono:
  Front Left: Playback 127 [100%] [on]
  Front Right: Playback 127 [100%] [on]
I just use bluetoothctl to pair the speaker (and configure bluetooth connexion), not sure using the raspberry GUI is working all the time.

Code: Select all

pi@raspberrypi:/var/run/bluealsa $ bluetoothctl
[NEW] Controller B8:27:EB:C3:32:96 raspberrypi [default]
[NEW] Device 11:75:58:E4:71:30 TimeBox-mini-audio
[TimeBox-mini-audio]# info 11:75:58:E4:71:30
Device 11:75:58:E4:71:30
        Name: TimeBox-mini-audio
        Alias: TimeBox-mini-audio
        Class: 0x240404
        Icon: audio-card
        Paired: yes
        Trusted: yes
        Blocked: no
        Connected: yes

Re: Use bluetooth audio on Raspberry PI

Posted: Tuesday 30 July 2019 21:08
by AllesVanZelf
Earlier I ran into trouble with Alsa and bluetooth and bluez-alsa. I did know that bluealsa is in repository now. Maybe it was there all the time ;)
I removed pulseaudo:

Code: Select all

 sudo apt purge pulseaudio pulseaudio-module-bluetooth
(first made some backups of configuration files).

installed bluealsa as you said:

Code: Select all

sudo apt-get install bluealsa
created a file /etc/asound.conf

Code: Select all

sudo nano /etc/asound.conf
with content:

Code: Select all

defaults.bluealsa.service "org.bluealsa"
defaults.bluealsa.device "5D:57:C3:CB:0D:B3"
defaults.bluealsa.profile "a2dp"
defaults.bluealsa.delay 3000
And now I cam play files as:

Code: Select all

 aplay -D bluealsa /home/pi/soundfiles/3.wav
It survived a first reboot.
Thank you very much Thorgal789 for pointing this out to me!!

Esit: The last smiley is a: - D