Last active
March 31, 2020 00:28
-
-
Save Knetic/4fbf62dfb59a5c0a0feac34b818ce621 to your computer and use it in GitHub Desktop.
Ensures bluetooth audio device is connected and using a2dp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# headset.sh | |
# Created; April 2018 | |
# Author; George Lester | |
# Under Mint Linux 17 and 18, on multiple devices, I consistently found that | |
# my bluetooth headset wouldn't reliably use a2dp, instead favoring the phone profile, | |
# and would completely refuse to switch to a2dp. | |
# This replicates the actions i would take every morning to fix the problem in the UI; | |
# 1. Restart bluetooth | |
# 2. Connect to the device | |
# 3. try to set a2dp | |
# if it fails, go back and do it again | |
# I don't know why i have to do this, but i do, so here we are. | |
# | |
MAC=$1 | |
IDPA=$(echo "${MAC}" | sed -e 's/:/_/g') | |
CARD="bluez_card.${IDPA}" | |
SINK="bluez_sink.${IDPA}" | |
$(pactl list | grep -qi 'Active Profile: a2dp_sink') | |
a2dpUsed=$? | |
# may need to restart bluetooth multiple times to "unstick" it. Not sure why. | |
# this loops until a2dp is _actually used_ on the given MAC. | |
while [ ${a2dpUsed} -ne 0 ]; | |
do | |
# | |
echo "Restarting bluetooth." | |
rfkill unblock bluetooth | |
sudo service bluetooth restart | |
# reconnect | |
echo -e "power on\nconnect ${MAC}" | bluetoothctl | |
# | |
echo "Waiting for headset to be connected..." | |
btConnected=1 | |
while [ ${btConnected} -gt 0 ]; | |
do | |
sudo hciconfig hci0 down | |
sudo hciconfig hci0 up | |
echo -e "connect ${MAC}" | bluetoothctl | |
sleep 4 | |
$(bluetoothctl <<< "info ${MAC}" | grep -qi "Connected: yes") | |
btConnected=$? | |
done | |
# | |
echo "Bluetooth connected, waiting for profiles to register" | |
cardFound=1 | |
while [ ${cardFound} -ne 0 ]; | |
do | |
sleep .1 | |
$(pactl list | grep -qi "${CARD}") | |
cardFound=$? | |
done | |
# | |
echo "Setting bluetooth a2dp profile" | |
pactl set-card-profile ${CARD} a2dp_sink | |
$(pactl list | grep -qi 'Active Profile: a2dp_sink') | |
a2dpUsed=$? | |
done | |
echo "a2dp is working." | |
pacmd set-default-sink ${SINK} | |
inputs=($(pacmd list-sink-inputs | grep index | awk '{print $2}')) | |
for i in ${inputs[*]}; | |
do | |
pacmd move-sink-input ${i} ${SINK} &> /dev/null | |
done | |
echo "default device changed to a2dp device." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment