Skip to content

Instantly share code, notes, and snippets.

@tinywrkb

tinywrkb/btswitch

Last active Jul 28, 2021
Embed
What would you like to do?
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"
@irimi1

This comment has been minimized.

Copy link

@irimi1 irimi1 commented Mar 7, 2021

Just a note: While the current pipewire version (I’m running 0.3.23) does switch the default sink automatically, it does not seem to move the sink inputs when doing so (see btswitch line 24).

@tinywrkb

This comment has been minimized.

Copy link
Owner Author

@tinywrkb tinywrkb commented Mar 7, 2021

@irimi1 it's working fine for me since 0.3.21 release. As long as you didn't set the application/sink-input to a specific output device/sink then PipeWire should automatically move the sink-input to the updated default sink.
Look at ~/.config/pipewire/media-session.d/restore-stream, if the application has a target-node set then this means you changed the default and you specifically set the output device for the application.
For proper testing, I recommend removing this DBus rule, clean ~/.config/pipewire/media-session.d/, and restart the PipeWire systemd user services.

If this still doesn't work for you then it's a bug that should be reported.

@irimi1

This comment has been minimized.

Copy link

@irimi1 irimi1 commented Mar 7, 2021

@tinywrkb Thanks for the explanation! I tried, but it changed nothing for me. This not working prompted me to fiddle around with it in the first place. Disconnecting the bluetooth headset switches back smoothly to my speakers however.
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?

@tinywrkb

This comment has been minimized.

Copy link
Owner Author

@tinywrkb 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.

@irimi1

This comment has been minimized.

Copy link

@irimi1 irimi1 commented Mar 8, 2021

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

This comment has been minimized.

Copy link

@geckolinux geckolinux commented Apr 11, 2021

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?

@irimi1

This comment has been minimized.

@geckolinux

This comment has been minimized.

Copy link

@geckolinux geckolinux commented Apr 11, 2021

@irimi1 Thanks, I appreciate it!

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