Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Udev service for temporary solve bug 92102 https://bugs.freedesktop.org/show_bug.cgi?id=92102
# /etc/udev/rules.d/20-bt-auto-enable-a2dp.rules
# hxss
SUBSYSTEM=="bluetooth", ACTION=="add", RUN+="/home/hxss/.config/scripts/xorg/bt-auto-enable-a2dp.sh"
#!/bin/sh
# Dependencies:
# * bluez-tools
# * expect
# * perl
function enable_a2dp() {
# run connect command in bluetoothctl and wait for resolve of services
expect <<< "
spawn bluetoothctl
send \"connect $mac\r\"
log_user 0
expect -re \".*Device $mac ServicesResolved: yes\"
"
# enable card in pulseaudio
pactl set-card-profile $pulsecard a2dp_sink
logger -p info "mac $mac enabled"
headsetname=`bt-device -l | perl -ne '/(.*) \('$mac'\)/ and print "$1\n"'`
notify-send 'Headset connected' "$headsetname" --icon=blueman-headset
}
function search_headsets() {
sleep 1
# in all added devices
for mac in `bt-device -l | perl -ne '/.*\((.*)\)/ and print "$1\n"'`
do
# search for connected device with AudioSink service
if [[ `bt-device -i $mac | perl -00 -ne '/.*Trusted: 1.*\n\s*Blocked: 0.*\n\s*Connected: 1\n\s*UUIDs: .*AudioSink.*/ and print "1\n"'` ]]; then
logger -p info "found mac: $mac"
# convert mac to pulse card name
pulsecard=`perl -pe 's/:/_/g' <<< "bluez_card.$mac"`
enable_a2dp
fi
done
echo "search done"
}
logger -p info "${BASH_SOURCE[0]}"
# get script owner name
user=`stat -c %U $0`
if [ "$user" == `whoami` ]; then
# if script runned by owner - start main function
search_headsets
elif [ "`w -hs $user`" ]; then
# else if user session exist(to prevent running on system startup) - run script from user
machinectl shell --uid=$user .host ${BASH_SOURCE[0]}
fi
@impankratov

This comment has been minimized.

Copy link

@impankratov impankratov commented Jun 17, 2018

Works great in Manjaro linux. Thanks for that script!

@baizon

This comment has been minimized.

Copy link

@baizon baizon commented Jul 27, 2018

Side note: For me (Manjaro) it wasn't the package bluez-tools. I needed bluez-utils.
Also thank you very much for that script <3

@jraygauthier

This comment has been minimized.

Copy link

@jraygauthier jraygauthier commented Sep 2, 2018

Nice, I should try this. Would you mind adding a license to your gist, if it work well (and your license allows), I might package it for nixos. Here's an example of what people seem to do to license their gist:

https://gist.github.com/martinbuberl/c0de29e623a1e34d1cda7e817d18bafe

@inffy

This comment has been minimized.

Copy link

@inffy inffy commented Jan 23, 2019

I don't know what I am missing but I can get this to work on Manjaro. My headphones always default to SBC even if I have needed ldac modules etc installed.

@dandefou

This comment has been minimized.

Copy link

@dandefou dandefou commented Jul 31, 2019

Great workaround! For my case (using Linux Mint 19.1, based on Ubuntu), I had to add some extra steps:
a. changing line 1 of the script #!/bin/sh to #!/bin/bash (as sh in Mint/Ubuntu is a very basic shell interpreter, unable to follow your script)
b. creating a systemd service pointing to your script named "auto-enable-a2dp.service" (see: https://www.pcsuggest.com/run-shell-scripts-from-udev-rules/)
c. changing line 3 of the rule, replacing RUN with TAG+="systemd", ENV{SYSTEMD_WANTS}="auto-enable-a2dp.service"
d. installing a last dependency 'sudo apt install systemd-container'

It is still possible to debug the service by tapping 'systemctl status auto-enable-a2dp.service'

@TimLucaTuran

This comment has been minimized.

Copy link

@TimLucaTuran TimLucaTuran commented Aug 31, 2019

Worked for me. Thanks :)

@piiskop

This comment has been minimized.

Copy link

@piiskop piiskop commented Jan 25, 2020

peacecop@peacecop-HP-ProBook-4520s:~$ ./bt-auto-enable-a2dp.sh
./bt-auto-enable-a2dp.sh: 8: Syntax error: "(" unexpected

@frostRed

This comment has been minimized.

Copy link

@frostRed frostRed commented Apr 13, 2020

It Works well on Manjaro KDE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment