2

I need working setup to do the following:

  1. Automaticaly connect some bluetooth audio devices (bluetooth speakers) on boot.
  2. On system startup (or by other cron background task) automatically play music on multiple audio device (including bluetooth from point 1).

Requirenment: Avoid interactive login to this system.

I have such setup on Ubuntu Desktop 22.04 using PulseAudio as system wide service. Setup is quite straightforward:

  1. Create unit file for system wide PulseAudio service /etc/systemd/system/pulseaudio.service to execute pulseaudio with "--system" flag:
[Unit]
Description=PulseAudio system server

[Service] Type=notify ExecStart=pulseaudio --daemonize=no --system --realtime --log-target=journal Restart=on-failure

[Install] WantedBy=multi-user.target

  1. Enable this unit:
sudo systemctl enable pulseaudio
  1. Add user account that will connect bluetooth device and play music to the following groups: pulse,pulse-access,audio
sudo usermod -a -G pulse,pulse-access,audio homeadmin
  1. Pair system with bluetooth audio device. This requires only once so i login to the system interactively and connect bluetooth speakers using Settings.
  2. Create script (/usr/local/bin/connect_audio.sh) that will connect bluetooth device and combine audio sinks as following:
bluetoothctl connect <blutooth device mac>
pactl load-module module-combine-sink
  1. Shedule script from point 5 and audio player using cron:
@reboot /usr/local/bin/connect_audio.sh; mpg123 <path to mp3>

But i have no success to migrate this setup to Ubuntu 24.04. It is not working. After install PulseAudio and make it as system-wide service on 24.04 (as described above) i can't get to make bluetooth audio devices available for user as system audio device without interactive login. It looks like pipewire is used as audio subsystem for Ubuntu Desktop 24.04 and it prevents this.

What i have tried to do on 24.04:

  1. To find and use config to run pipewire system wide same as PulseAudio. No working solution with bluetooth support found. It looks like it is not possible and not supported at this time. Some discussions here: https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/1610
  2. To disable pipewire (How to uninstall pipewire and go back to pulseaudio), install Pulse audio and then setup PulseAudio as system-wide service (as described above). It looks like it is not posible to disable pipewire completely because it is part of Desktop (gdm) now.

Please help me to make working setup on Ubuntu Desktop 24.04 using pipewire or PulseAudio.

UPD. The only two working options i found to workaround this issue:

  1. To use Ubuntu Server 24.04 instead of Desktop. It has no gdm (graphical interface) and thus no pipewire installed. In this case bluetooth and audio does not work "out-of-box" and requires extra step for configuration.
  2. To use Ubuntu Desktop but enable auto-login to user account with bluetooth speakers connected. This is the only case when bluetooth speakers start to be available for user as audio device in a system. But in this case my requirenment does not met.

3 Answers3

2

After a lot of research i finally found working setup that meets my need and does not need a system-wide audio configuration. Settings depend on what audio subsystem is used in distribution by default: PulseAudio (Ubuntu 22.04) or PipeWire (Ubuntu 24.04).

PulseAudio (Ubuntu 22.04)

You do not need to change PulseAudio mode from user to system-wide. Instead modify the folowing settings:

  1. Set
autospawn = yes

in

/etc/pulse/client.conf
  1. Enable login lingering:
sudo loginctl enable-linger <user account>

where <user account> is an account name by which cron with task will be running.

PulseAudio run as user services by default, which are terminated once the last session ends. Enable lingering for the user to fix this.

To check lingering status:

sudo loginctl user-status <user account>
  1. Add user account to the groups audio,pulse,pulse-access,bluetooth:
sudo usermod -G audio,pulse,pulse-access,bluetooth -a <user account>
  1. In the cron script (/usr/local/bin/connect_audio.sh) you need start to play audio file before bluetooth connection and sink combining to spawn PulseAudio process:
/usr/bin/mpg321 <path to mp3> &
sleep 5
bluetoothctl connect <blutooth device mac>
pactl load-module module-combine-sink

PipeWire (Ubuntu 24.04)

  1. Enable login lingering:
sudo loginctl enable-linger <user account>

where <user account> is an account name by which cron with task will be running.

PipeWire run as user services by default, which are terminated once the last session ends. Enable lingering for the user to fix this.

  1. Add this user account to audio,bluetooth groups:
sudo usermod -G audio,bluetooth -a <user account>
  1. Create file /etc/wireplumber/bluetooth.lua.d/50-bluez-config.lua with the following content:
bluez_monitor.properties = {
  ["with-logind"] = false,
}
sudo mkdir -p /etc/wireplumber/bluetooth.lua.d/
sudo nano /etc/wireplumber/bluetooth.lua.d/50-bluez-config.lua

When running WirePlumber with PipeWire (which is usually the case), WirePlumber runs a "logind-monitor" which enables Bluetooth on login and disables it on logout.

Links

https://wiki.archlinux.org/title/bluetooth

https://wiki.archlinux.org/title/Systemd/User#Automatic_start-up_of_systemd_user_instances

https://wiki.archlinux.org/title/WirePlumber#Keep_Bluetooth_running_after_logout_/_Headless_Bluetooth

0

I use pipewire/wireplumber with Bluetooth for my audio system. I read about loginctl enable-linger in this article. I screwed around with this for a couple of hours, and could not get it to work at all. I noted that loginctl is another invention from the same people that brought us systemd... I figured that was the cause of my problem!

Anyway - to the point of this answer: I found an alternative solution that works very reliably for my system. I use cmus as the player on my system (started using screen -S cmus cmus); I can start a playlist in cmus, then log out and the sound continues. Here's what I did:

# open the 'getty@tty1.service' file in your editor:

$ sudo systemctl edit getty@tty1.service

In the [Service] section, add the following two lines:

ExecStart= ExecStart=-/sbin/agetty --autologin pi --noclear %I $TERM

save changes and exit the editor...

$ sudo reboot

after 'reboot', you can test :

$ who pi tty1 1970-01-10 21:45 # <=== this! pi pts/0 -3386239902218585523 (192.168.1.209)

Seamus
  • 698
0

The answer by @alexeynl got me 90% of the way there in my Ubuntu 24.04 installation. However, I had to additionally:

  1. modify wireplumber.conf by adding a section:
wireplumber.profiles = {
  main = {
    monitor.bluez.seat-monitoring = disabled
  }
}
  1. remove my user from the audio group:
sudo gpasswd --delete <username> audio
stefan
  • 11