Skip to content

Instantly share code, notes, and snippets.

@bonelifer
Forked from emmaly/gist:8267078
Created December 2, 2023 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bonelifer/5b9e7e38ce6f732b953129d5959aad2c to your computer and use it in GitHub Desktop.
Save bonelifer/5b9e7e38ce6f732b953129d5959aad2c to your computer and use it in GitHub Desktop.
Bash script to automatically switch PulseAudio sink to Bluetooth headset on connect with A2DP profile and 50% volume.
#!/bin/bash
sink_name="bluez_sink.00_42_1B_AD_FA_CE"
if [ -z "$1" ]; then
dbus-monitor --system "path=/org/bluez/777/hci0/dev_00_42_1B_AD_FA_CE, interface=org.bluez.AudioSink, member=Connected" | while read line; do
echo $line
$0 1
done
else
if pacmd list-sinks | grep -qi $sink_name; then
if pactl stat | grep -qi $sink_name; then
echo "Already done."
else
echo "Setting profile..."
pactl set-card-profile bluez_card.00_42_1B_AD_FA_CE a2dp
echo "Setting sink..."
echo "set-default-sink $sink_name" | pacmd > /dev/null
echo "Setting volume..."
pactl -- set-sink-volume $sink_name 0
pactl -- set-sink-volume $sink_name +50%
fi
else
echo "Not connected."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment