Skip to content

Instantly share code, notes, and snippets.

@tinywrkb
Last active February 6, 2024 10:09
Show Gist options
  • Save tinywrkb/04e7fd644afa9b92d33a3a99ab07ee9e to your computer and use it in GitHub Desktop.
Save tinywrkb/04e7fd644afa9b92d33a3a99ab07ee9e 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.*.a2dp.sink'|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
#
# ~/.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
#
# /etc/udev/rules.d/pipewire-pulse-bt.rules
#
# phys: bt controller mac
# name: bt device name
SUBSYSTEM=="input",ATTRS{phys}=="0c:54:15:b5:cf:ec",ATTR{name}=="Bose SoundSport (AVRCP)",ACTION=="add",TAG+="systemd",ENV{SYSTEMD_USER_WANTS}+="pipewire-pulse-bt-switch.service"
@tinywrkb
Copy link
Author

tinywrkb commented Mar 8, 2021

While the applications don’t have a target node according to restore-stream, I did use pactl to set the default sink to the a2dp one of my headphones. Any chances that caused any problems here?

No, I don't think so. I tried it the other day and if I remove the entry with the target-node or even just remove target-node plus value and restart the services then PipeWire just select the default output for the application and then switch as expected to the newly connected BT device.

Reproduce the issue with detailed logs as explained in PipeWire's README.md and report this as an issue. ATM PipeWire's development is very fast-paced, we have a new stable release every two weeks, so it would be a shame not to report this and have it fixed quickly.

@manu-cyber
Copy link

Reproduce the issue with detailed logs as explained in PipeWire's README.md and report this as an issue. ATM PipeWire's development is very fast-paced, we have a new stable release every two weeks, so it would be a shame not to report this and have it fixed quickly.

Alright, will do. Thank you so much! :)

@geckolinux
Copy link

Hi, I have the same issue as @Irimi1 on openSUSE Tumbleweed with PipeWire 0.3.24. By any chance was a bug report already filed for this?

@manu-cyber
Copy link

@geckolinux
Copy link

@Irimi1 Thanks, I appreciate it!

@speguero
Copy link

speguero commented Aug 3, 2021

Just now, I noticed that my default sink wasn't set to my USB speaker automatically when connected. The speaker is listed as a sink and I can set it as the default manually using pactl set-default-sink <sink_name>, but I'm wondering whether this is worth mentioning in an official bug report also. I'm running Pipewire v0.3.32 on Fedora 34.

@tinywrkb
Copy link
Author

tinywrkb commented Aug 3, 2021

@stutteringsteve you should drop by the #pipewire IRC channel on OFTC and ask.
In general, it should work, but a recent change was introduced for compatibility with some desktop environments, so maybe you hit this edge case and next release, 0.3.33, will fix it.

@speguero
Copy link

speguero commented Aug 3, 2021 via email

@MrDisguised
Copy link

How do I get ATTRS{phys} & ATTR{name} of my bluetooth device ?

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