Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@emmaly
Created January 5, 2014 11:21
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save emmaly/8267078 to your computer and use it in GitHub Desktop.
Save emmaly/8267078 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
@nsirolli
Copy link

Here's an alternative I wrote, which switches between the Bluetooth headset and my notebook's speakers.

#!/bin/bash
sink_addr=“F0_13_C3_27_0B_40”
mac_addr=“F0:13:C3:27:0B:40”
alsa_card="alsa_card.pci-0000_00_1b.0"
if bluetoothctl <<< “info $mac_addr” | grep -qi “Connected: no”; then
      bluetoothctl <<< “connect $mac_addr”
      pactl set-card-profile $alsa_card off
      sleep 6s
      pactl set-card-profile bluez_card.$sink_addr a2dp
      pactl set-sink-volume bluez_sink.$sink_addr 50%
else
      bluetoothctl <<< “disconnect $mac_addr”
      pactl set-card-profile $alsa_card output:analog-stereo+input:analog-stereo
fi

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