Replies: 4 comments 9 replies
-
So I read a lot about udev rules and the need for the full path in everything. I changed the one assignment to be: Any ideas why it does not run? all the functions I am running are in /usr/bin (bluealsa-aplay, grep, cut, tr) my guess is that bluealsa-aplay will not run from a udev rule. I tried sending its output to a log and it always reports nothing. Why does this not run from the udev rule? |
Beta Was this translation helpful? Give feedback.
-
I see a number of potential issues with your udev rules and script:
This rule will work only with Broadcom UART adapters (eg RPi on-board adapter), but not with USB adapters, nor with adapters by any other manufacturer. It would be easier to move to other machines if made more generic. (see my suggestion below). But the main issue is with the script. Your script is invoked immediately any bluetooth device connects, which means it most likely runs before BlueALSA has created any PCMs. So bluealsa-aplay -L reports nothing - exactly the result you are seeing. You need to wait for the PCMs to be created before listing them. I recommend using /etc/udev/rules.d/90-bt-connect.rules:
/srv/http/command/bt-connect.sh: #!/bin/sh
# bt-connect.sh
# shell script to switch off mpd so that a source connected
# will play through alsa system to the default output.
# also if a speaker/headset is added, it will set it as the default
# output in mpd.conf
case "$1" in
add)
# wait for PCM(s) to be created, with absolute timeout of 5 seconds.
timeout 5 bluealsa-cli -q monitor | grep -q -m 1 ^PCMAdded || exit 0
#
case "$(bluealsa-cli -q list-pcms | grep -o -E '(sink|source)$')" in
*sink*) # attached speaker(s)
mpc stop
# set default output in mpd.conf to BT
;;
*source*) # attached phone
mpc stop
# this is enough to allow a device to play through alsa
;;
esac
;;
remove)
# remove the BT default if it was added to mpd.conf
mpc play
;;
esac |
Beta Was this translation helpful? Give feedback.
-
I rebuilt with
Changed to your suggested rules and script and it does a very similar thing. I added a few log writes and see that the rule is working for the phone I am playing with. I send 'add' and get that far, but nothing past the 'timeout' line runs when I connect. If I manually 'mpc stop' then 'remove' by disconnecting from the phone the 'remove' part of the script runs fine and turns mpd back on. my edited line that is not executed: any further ideas appreciated. |
Beta Was this translation helpful? Give feedback.
-
For the issue with SCO PCMs breaking your script, either run
I suggested |
Beta Was this translation helpful? Give feedback.
-
I mentioned this a while ago and was asked to post when I figured out something. Well, I figured out how to tell it when I connect/disconnect with udev rules. I noticed that all audio devices I have added all advertise as 'hci_uart_bcm" and "bluetooth". These rules do work and call my script correctly. Now to flesh out the script to parse out the type of device with bluealsa-aplay as the second line returns if it is a source or a sink. Is there a better way of doing this? Any help appreciated.
this calls my script and I am terrible at writing and debugging scripts, but this is where I am now. It does not actually yet test for source or sink, yet, but I am working on it.
I can run it from cli 'bt-connect.sh start' and it does what is is supposed to but when triggered from the udev rule on start, it does not. If I disconnect, the second case (stop) works. If simplify the rule to only be start and stop and remove the nested 'type' case switch, it works from udev...
Any ideas?
Beta Was this translation helpful? Give feedback.
All reactions