I have the following udev rule:
ACTION=="add", SUBSYSTEM=="bluetooth", RUN+="/usr/local/bin/a2dp-fix-wrapper"
that fires off the following script:
PID=$(pgrep pulseaudio)
USER=$(grep -z USER= /proc/$PID/environ | sed 's/.*=//')
USER_ID=$(id -u $USER)
HOME=$(echo $(getent passwd $USER )| cut -d : -f 6)
export XDG_RUNTIME_DIR=/run/user/$USER_ID
export XAUTHORITY=$HOME/.Xauthority
export DISPLAY=:0
export PULSE_RUNTIME_PATH=$XDG_RUNTIME_DIR/pulse/
sleep 5
sudo -u $USER -E /usr/local/bin/a2dp-fix &> /udev_output.txt
and a2dp-fix is the following:
bt_device_addr=$(pacmd list-cards | grep -i 'name:.*bluez_card' | sed -E 's/.*<?bluez_card\.([A-Z0-9_]+)>?/\1/')
device_mac=$(echo $bt_device_addr | sed 's/_/:/g')
a2dp_available=$(pacmd list-cards | grep -A30 bluez | grep "A2DP Sink" | sed -E 's/.* available: ([a-z]+)\)/\1/g')
if [[ "$a2dp_available" == "no" ]]
then
echo -e "connect $device_mac\nquit" | bluetoothctl
sleep 5
pacmd set-card-profile bluez_card.$bt_device_addr off
pacmd set-card-profile bluez_card.$bt_device_addr a2dp_sink
pacmd set-default-sink bluez_sink.$bt_device_addr.a2dp_sink
fi
After lots of meddling with env variables, I got the pacmd
stuff to work. However, now the bluetoothctl
part hangs with “Waiting to connect to bluetoothd…”
. This works just fine if I invoke it manually from shell with sudo /usr/local/bin/a2dp-fix-wrapper
but not when udev kicks it off. I imagine it must be some sort of env variable that prevents bluetoothctl
from finding the running bluetoothd
instance.
Anyone have any thoughts?