You are not logged in.

#1 2018-10-06 17:39:43

sjensen
Member
From: Germany
Registered: 2018-08-08
Posts: 31

(Re)connect a paired bluetooth speaker

Hi,...

I want to automatically (re)connect to my Bluetooth-Speaker on login. Therefor I created the following systemd unit:

$systemctl --user cat sony-srs-x3-connect.service

# /home/xxxxx/.config/systemd/user/sony-srs-x3-connect.service
[Unit]
Description=Connect to the Sony SRS-X3 Bluetooth Speaker
Documentation=

After=bluetooth.service pulseaudio.service

[Service]
Type=oneshot
RemainAfterExit=yes

ExecStart=/bin/sh -c '/usr/bin/echo -e "connect xx:xx:xx:xx:xx:xx" | /usr/bin/bluetoothctl --timeout 5'
ExecStop=/bin/sh -c '/usr/bin/echo -e "disconnect xx:xx:xx:xx:xx:xx" | /usr/bin/bluetoothctl --timeout 5'

[Install]
WantedBy=default.target

Manually starting/stopping works fine, but on login the unit is started way to early. Despite the "After=bluetooth.service pulseaudio.service" the unit is started before pulseaudio. I tried "After=bluetooth.target sound.target" without luck. The unit is started always before Pulseaudio.

$journalctl --no-hostname --no-pager --full --quiet --priority=debug --boot 0 --user|grep Started

Okt 06 18:30:32 systemd[1626]: Started Fetchmail - Remote-mail retrieval utility.
Okt 06 18:30:32 systemd[1626]: Started Update XDG user dir configuration.
Okt 06 18:30:32 systemd[1626]: Started Autostart - start a bunch of other services.
Okt 06 18:30:32 systemd[1626]: Started Connect to the Sony SRS-X3 Bluetooth Speaker.
Okt 06 18:30:34 systemd[1626]: Started D-Bus User Message Bus.
Okt 06 18:30:34 systemd[1626]: Started Accessibility services bus.
Okt 06 18:30:34 systemd[1626]: Started Virtual filesystem service.
Okt 06 18:30:35 systemd[1626]: Started Sound Service.
Okt 06 18:30:36 systemd[1626]: Started Evolution source registry.
Okt 06 18:30:37 systemd[1626]: Started Virtual filesystem service - disk device monitor.
Okt 06 18:30:37 systemd[1626]: Started Virtual filesystem service - Media Transfer Protocol monitor.
Okt 06 18:30:37 systemd[1626]: Started Virtual filesystem service - GNOME Online Accounts monitor.
Okt 06 18:30:37 systemd[1626]: Started Virtual filesystem service - digital camera monitor.
Okt 06 18:30:38 systemd[1626]: Started Tracker metadata database store and lookup manager.
Okt 06 18:30:39 systemd[1626]: Started Evolution calendar service.
Okt 06 18:30:39 systemd[1626]: Started Evolution address book service.
Okt 06 18:30:47 systemd[1626]: Started GNOME Terminal Server.

I cant figure out, what am I doing wrong here? Why is the unit started before Pulseaudio?

Last edited by sjensen (2018-10-15 18:33:48)

Offline

#2 2018-10-06 23:26:28

sjensen
Member
From: Germany
Registered: 2018-08-08
Posts: 31

Re: (Re)connect a paired bluetooth speaker

I got it working.  With some slight modification the unit behaves correctly now:

$systemctl --user cat sony-srs-x3-connect.service

# /home/xxxxx/.config/systemd/user/sony-srs-x3-connect.service
[Unit]
Description=Connect to the Sony SRS-X3 Bluetooth Speaker
Documentation=

After=pulseaudio.service

[Service]
Type=simple
RemainAfterExit=yes

ExecStart=/bin/sh -c '/usr/bin/echo -e "connect xx:xx:xx:xx:xx:xx" | /usr/bin/bluetoothctl --timeout 5'
ExecStop=/bin/sh -c '/usr/bin/echo -e "disconnect xx:xx:xx:xx:xx:xx" | /usr/bin/bluetoothctl --timeout 5'

[Install]
WantedBy=pulseaudio.service

That will successfully reconnect the speaker, when I login and after pulseaudio is started.

$journalctl --no-hostname --no-pager --full --quiet --priority=debug --boot 0 --user|grep -i started

Okt 07 01:06:19 systemd[1574]: Started Fetchmail - Remote-mail retrieval utility.
Okt 07 01:06:19 systemd[1574]: Started Update XDG user dir configuration.
Okt 07 01:06:20 systemd[1574]: Started Autostart - start a bunch of other services.
Okt 07 01:06:21 systemd[1574]: Started D-Bus User Message Bus.
Okt 07 01:06:21 systemd[1574]: Started Accessibility services bus.
Okt 07 01:06:22 systemd[1574]: Started Virtual filesystem service.
Okt 07 01:06:22 systemd[1574]: Started Sound Service.
Okt 07 01:06:22 systemd[1574]: Started Connect to the Sony SRS-X3 Bluetooth Speaker.
Okt 07 01:06:24 systemd[1574]: Started Evolution source registry.
Okt 07 01:06:24 systemd[1574]: Started Virtual filesystem service - disk device monitor.
Okt 07 01:06:25 systemd[1574]: Started Virtual filesystem service - Media Transfer Protocol monitor.
Okt 07 01:06:25 systemd[1574]: Started Virtual filesystem service - GNOME Online Accounts monitor.
Okt 07 01:06:25 systemd[1574]: Started Virtual filesystem service - digital camera monitor.
Okt 07 01:06:26 systemd[1574]: Started Tracker metadata database store and lookup manager.
Okt 07 01:06:27 systemd[1574]: Started Evolution calendar service.
Okt 07 01:06:27 systemd[1574]: Started Evolution address book service.
Okt 07 01:06:28 gnome-shell[1647]: GNOME Shell started at Sun Oct 07 2018 01:06:25 GMT+0200 (CEST)
Okt 07 01:06:31 systemd[1574]: Started GNOME Terminal Server.

edit: removed hardware addresses

Last edited by sjensen (2018-10-07 00:41:52)

Offline

#3 2018-10-15 18:33:17

sjensen
Member
From: Germany
Registered: 2018-08-08
Posts: 31

Re: (Re)connect a paired bluetooth speaker

Removed the "solved", because this unit file doesn't work reliable. Sometimes it works, sometimes not.  Maybe I'm doing it all wrong and systemd is not the right tool for this? Anyway, here is what happens, when it fails:

Okt 15 19:57:55 systemd[1607]: Started Connect to the Sony SRS-X3 Bluetooth Speaker.
Okt 15 19:57:55 sh[2142]: Agent registered
Okt 15 19:57:55 sh[2142]: [bluetooth]# connect xx:xx:xx:xx:xx:xx
Okt 15 19:57:55 sh[2142]: Attempting to connect to xx:xx:xx:xx:xx:xx
Okt 15 19:57:55 sh[2142]: [bluetooth]#
Okt 15 19:58:01 bluetoothd[1272]: /org/bluez/hci0/dev_xx_xx_xx_xx_xx_xx/fd0: fd(23) ready
Okt 15 19:58:01 rtkit-daemon[1302]: Supervising 3 threads of 1 processes of 1 users.
Okt 15 19:58:01 rtkit-daemon[1302]: Successfully made thread 2155 of process 1703 (/usr/bin/pulseaudio) owned by '1000' RT at priority 5.
Okt 15 19:58:01 rtkit-daemon[1302]: Supervising 4 threads of 1 processes of 1 users.
Okt 15 19:58:01 bluetoothd[1272]: No pending request, ignoring message
Okt 15 19:58:11 bluetoothd[1272]: connect error: Connection reset by peer (104)
Okt 15 19:58:16 bluetoothd[1272]: Suspend: Connection timed out (110)
Okt 15 19:58:21 bluetoothd[1272]: Transaction label doesn't match
Okt 15 19:58:22 bluetoothd[1272]: SUSPEND request rejected: Bad State (49)
Okt 15 19:58:22 dbus-daemon[551]: [system] Rejected send message, 1 matched rules; type="method_return", sender=":1.167" (uid=1000 pid=1703 comm="/usr/bin/pulseaudio --daemonize=no ") interface="(unset)" member="(unset)" error name="(unset)" requested_reply="0" destination=":1.34" (uid=0 pid=1272 comm="/usr/lib/bluetooth/bluetoothd ")
Okt 15 19:58:22 gsd-media-keys[1842]: Unable to get default sink
Okt 15 19:58:22 kernel: input: xx:xx:xx:xx:xx:xx as /devices/virtual/input/input21
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: (II) config/udev: Adding input device xx:xx:xx:xx:xx:xx (/dev/input/event18)
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: (**) xx:xx:xx:xx:xx:xx: Applying InputClass "libinput keyboard catchall"
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: (**) xx:xx:xx:xx:xx:xx: Applying InputClass "system-keyboard"
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: (II) Using input driver 'libinput' for 'xx:xx:xx:xx:xx:xx'
Okt 15 19:58:22 /usr/lib/gdm-x-session[894]: (II) config/udev: Adding input device xx:xx:xx:xx:xx:xx (/dev/input/event18)
Okt 15 19:58:22 /usr/lib/gdm-x-session[894]: (**) xx:xx:xx:xx:xx:xx: Applying InputClass "libinput keyboard catchall"
Okt 15 19:58:22 /usr/lib/gdm-x-session[894]: (**) xx:xx:xx:xx:xx:xx: Applying InputClass "system-keyboard"
Okt 15 19:58:22 /usr/lib/gdm-x-session[894]: (II) Using input driver 'libinput' for 'xx:xx:xx:xx:xx:xx'
Okt 15 19:58:22 systemd-logind[550]: Watching system buttons on /dev/input/event18 (xx:xx:xx:xx:xx:xx)
Okt 15 19:58:22 /usr/lib/gdm-x-session[894]: (II) systemd-logind: got fd for /dev/input/event18 13:82 fd 18 paused 1
Okt 15 19:58:22 /usr/lib/gdm-x-session[894]: (II) systemd-logind: releasing fd for 13:82
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: (II) systemd-logind: got fd for /dev/input/event18 13:82 fd 54 paused 0
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: (**) xx:xx:xx:xx:xx:xx: always reports core events
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: (**) Option "Device" "/dev/input/event18"
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: (**) Option "_source" "server/udev"
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: (II) event18 - xx:xx:xx:xx:xx:xx: is tagged by udev as: Keyboard
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: (II) event18 - xx:xx:xx:xx:xx:xx: device is a keyboard
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: (II) event18 - xx:xx:xx:xx:xx:xx: device removed
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: (**) Option "config_info" "udev:/sys/devices/virtual/input/input21/event18"
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: (II) XINPUT: Adding extended input device "xx:xx:xx:xx:xx:xx" (type: KEYBOARD, id 16)
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: (**) Option "xkb_layout" "de"
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: (II) event18 - xx:xx:xx:xx:xx:xx: is tagged by udev as: Keyboard
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: (II) event18 - xx:xx:xx:xx:xx:xx: device is a keyboard
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: The XKEYBOARD keymap compiler (xkbcomp) reports:
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: > Warning:          Unsupported high keycode 372 for name <I372> ignored
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: >                   X11 cannot support keycodes above 255.
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: >                   This warning only shows for the first high keycode.
Okt 15 19:58:22 /usr/lib/gdm-x-session[1621]: Errors from xkbcomp are not fatal to the X server
Okt 15 19:58:35 kernel: Bluetooth: hci0: last event is not cmd complete (0x0f)

In this state, the speaker indicates, that it is connected, bluetoothctl shows it is connected, even gnome-control-center shows "connected", but when I select the speaker, pulseaudio thinks it is not connected:

Okt 15 19:58:42 pulseaudio[1703]: W: [pulseaudio] module-bluez5-device.c: Refused to switch profile to a2dp_sink: Not connected

Is there somehow a better way to do this, maybe over some dbus magic? It seems to me, as if gnome becomes not (fully) aware of the change made by bluetoothctl. When I do it from within gnome-control-centert it always works fine.

Offline

Board footer

Powered by FluxBB