Skip to content

Instantly share code, notes, and snippets.

@MrDisguised
Forked from tinywrkb/btswitch
Last active June 17, 2023 08:15
Show Gist options
  • Save MrDisguised/5546843a92ed7dd4cbced90c372a1a1b to your computer and use it in GitHub Desktop.
Save MrDisguised/5546843a92ed7dd4cbced90c372a1a1b to your computer and use it in GitHub Desktop.
NOT NEEDED WITH 0.3.21 OR GIT VERSION | PipeWire Pulse Server Bluetooth Auto-Switch
#!/bin/bash
#
# ~/.local/bin/btswitch
#
# wait a second this is too fast for udev
sleep 1
btdev="$(pactl list sinks|grep Name|grep 'bluez.*'|sed 's/Name: //'|sed 's/\s//')"
if [ -n "$btdev" ]; then
echo "Found bluetooth device: $btdev"
else
echo "Could not find bluetooth device, exiting..."
exit 1
fi
echo "Setting default sink to the found bluetooth device..."
echo "Running pactl set-default-sink $btdev"
pactl set-default-sink "$btdev"
#echo "Setting default volume of sink to 26%"
#pactl set-sink-volume @DEFAULT_SINK@ 26%
echo "Moving all sink inputs to the default sink..."
for i in $(pactl list sink-inputs|grep 'Sink Input'|sed 's/Sink Input #//'); do
echo "Moving sink input $i to the default sink..."
pactl move-sink-input $i @DEFAULT_SINK@
done
### Sound does not automatically switch to Bluetooth headphones (https://wiki.archlinux.org/title/PipeWire#Sound_does_not_automatically_switch_when_connecting_a_new_device)
echo "Setting the default sink"
sinkno=$(wpctl status | grep "Airdopes" | grep "*" | awk -F . '{print $1}' | awk '{print $3}')
wpctl set-default $sinkno
#
# ~/.config/systemd/user/pipewire-pulse-bt-switch.service
#
[Unit]
Description=Pipewire Pulse Server Auto Switch to Bluetooth
[Service]
ExecStart=%h/.local/bin/btswitch
[Install]
WantedBy=default.target
#
# ~/.config/systemd/user/pipewire-pulse-bt-switch.timer
#
[Unit]
Description=Run the pipewire set default service on a timely basis mentioned below.
[Timer]
OnCalendar=15min
[Install]
WantedBy=timers.target
#
# /etc/udev/rules.d/pipewire-pulse-bt.rules
#
# phys: bt controller mac
# name: bt device name
SUBSYSTEM=="input",ATTRS{phys}=="e8:48:b8:c8:20:00",ATTR{name}=="Airdopes 141 (AVRCP)",ACTION=="add",TAG+="systemd",ENV{SYSTEMD_USER_WANTS}+="pipewire-pulse-bt-switch.service"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment