Created
February 23, 2021 14:01
-
-
Save jewzaam/b1b367b6ceda46519e3cf5e952f2d7dc to your computer and use it in GitHub Desktop.
Force my headphones to connect if they're on
This file contains 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 | |
# 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