Bluetooth speaker connects but the system does not switch the output device automatically

  Kernel: 5.10.89-1-MANJARO x86_64 bits: 64 compiler: gcc v: 11.1.0 Desktop: GNOME 41.2
    Distro: Manjaro Linux base: Arch Linux
  Type: Laptop System: LENOVO product: 82B5 v: Lenovo Legion 5 15ARH05
    serial: <superuser required>
  Mobo: LENOVO model: LNVNB161216 v: SDK0J40688 WIN serial: <superuser required>
    UEFI-[Legacy]: LENOVO v: EUCN19WW date: 05/12/2020
  ID-1: BAT0 charge: 37.5 Wh (78.0%) condition: 48.1/60.0 Wh (80.2%) volts: 15.8 min: 15.4
    model: Celxpert L19C4PC0 status: Discharging
  Info: 8-core model: AMD Ryzen 7 4800H with Radeon Graphics bits: 64 type: MT MCP arch: Zen 2
    rev: 1 cache: L1: 512 KiB L2: 4 MiB L3: 8 MiB
  Speed (MHz): avg: 1575 high: 2545 min/max: 1400/2900 boost: enabled cores: 1: 1796 2: 1568
    3: 2544 4: 2545 5: 1272 6: 1272 7: 1272 8: 1272 9: 1272 10: 1272 11: 2151 12: 1640 13: 1397
    14: 1345 15: 1272 16: 1323 bogomips: 92658
  Flags: avx avx2 ht lm nx pae sse sse2 sse3 sse4_1 sse4_2 sse4a ssse3 svm
  Device-1: NVIDIA TU117M [GeForce GTX 1650 Ti Mobile] vendor: Lenovo driver: nvidia v: 470.94
    bus-ID: 01:00.0
  Device-2: AMD Renoir vendor: Lenovo driver: amdgpu v: kernel bus-ID: 05:00.0
  Device-3: Chicony Integrated Camera type: USB driver: uvcvideo bus-ID: 1-3:2
  Display: x11 server: driver: loaded: amdgpu,nvidia
    resolution: <missing: xdpyinfo>
  Message: Unable to show advanced data. Required tool glxinfo missing.
  Device-1: NVIDIA driver: snd_hda_intel v: kernel bus-ID: 01:00.1
  Device-2: AMD Raven/Raven2/FireFlight/Renoir Audio Processor vendor: Lenovo driver: N/A
    bus-ID: 05:00.5
  Device-3: AMD Family 17h HD Audio vendor: Lenovo driver: snd_hda_intel v: kernel
    bus-ID: 05:00.6
  Sound Server-1: ALSA v: k5.10.89-1-MANJARO running: yes
  Sound Server-2: JACK v: 1.9.19 running: no
  Sound Server-3: PulseAudio v: 15.0 running: yes
  Sound Server-4: PipeWire v: 0.3.42 running: yes
  Device-1: Realtek RTL8111/8168/8411 PCI Express Gigabit Ethernet vendor: Lenovo driver: r8169
    v: kernel port: 2000 bus-ID: 03:00.0
  IF: eno1 state: up speed: 1000 Mbps duplex: full mac: <filter>
  Device-2: Intel Wi-Fi 6 AX200 driver: iwlwifi v: kernel bus-ID: 04:00.0
  IF: wlp4s0 state: down mac: <filter>
  Device-1: Intel AX200 Bluetooth type: USB driver: btusb v: 0.8 bus-ID: 3-3:3
  Report: rfkill ID: hci0 rfk-id: 3 state: up address: see --recommends
  Local Storage: total: 476.94 GiB used: 233.34 GiB (48.9%)
  ID-1: /dev/nvme0n1 vendor: Samsung model: MZVLB512HBJQ-000L2 size: 476.94 GiB temp: 39.9 C
  ID-1: / size: 459.79 GiB used: 233.34 GiB (50.8%) fs: ext4 dev: /dev/nvme0n1p1
  ID-1: swap-1 type: partition size: 8.8 GiB used: 0 KiB (0.0%) dev: /dev/nvme0n1p2
  System Temperatures: cpu: 39.6 C mobo: N/A gpu: amdgpu temp: 38.0 C
  Fan Speeds (RPM): N/A
  Processes: 381 Uptime: 22m Memory: 15.07 GiB used: 4.31 GiB (28.6%) Init: systemd Compilers:
  gcc: 11.1.0 Packages: 1528 Client: gnome-shell v: 41.2 inxi: 3.3.11

Bluetooth seems to be working fine, except for a small problem. When I turn on the speaker, it automatically connects and I can see that the output device is set to the speaker. But the audio still comes out of the built-in speaker or headphones if I have it connected. I have to switch the output device to the built-in speaker/headphones, then switch it back to the bluetooth speaker before the audio actually comes out of it. Is there a way to automatically switch the output device properly?

I’ve tried this, but it doesn’t seem to work.