Skip to content

Instantly share code, notes, and snippets.

@jewzaam
Created February 23, 2021 14:01
Show Gist options
  • Save jewzaam/b1b367b6ceda46519e3cf5e952f2d7dc to your computer and use it in GitHub Desktop.
Save jewzaam/b1b367b6ceda46519e3cf5e952f2d7dc to your computer and use it in GitHub Desktop.
Force my headphones to connect if they're on
#!/bin/bash
# assumes bluetooth is turned on
# assumes name of device
# sudo dnf install python3-bluez
# pip3 install bluetooth_battery --user
BOSE_CONNECT=$(bt-device -l | egrep -e "Bose QC 35" -e "PLT BBFIT3150" | sort | sed 's/.*[(]\(.*\)[)].*/connect \1 \n/g')
echo -e "$BOSE_CONNECT \nquit" | bluetoothctl
# find bluetooth devices
SINKS=$(pactl list short sinks | grep -v -e Thunderbolt | grep bluez | awk '{print $2}')
# try for 120 seconds
COUNT=120
while [ "$SINKS" == "" ] && [ $COUNT -gt 0 ];
do
sleep 1
SINKS=$(pactl list short sinks | grep -v -e Thunderbolt | grep bluez | awk '{print $2}')
COUNT=$((COUNT-1))
done
if [ "$SINKS" != "" ];
then
# pause music cause it could be super loud
# playerctl pause
# find preferred sink...
for SINK in $SINKS;
do
if [ $(echo $SINK | wc -l) -gt 0 ];
then
# set it as default sink
echo pactl set-default-sink $SINK
pactl set-default-sink $SINK
break
fi
done
notify-send Bluetooth "Device connected."
else
notify-send Bluetooth "Unable to connect."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment