I wrote a systemd service to turn off Bluetooth at boot time, but still wanted to be able to turn it on via the Bluetooth top panel applet icon if required.
It turns bluetooth off nicely at boot time and I thought I had tested that turning it on manually worked.
Recently I got some bluetooth speakers and I tried connecting to them.
But every time I turned bluetooth on, it turned off again (after about ten seconds) and the logs show that it was my service doing the turning off.
(What confused me more was that the first day I tried connecting, I managed to connect to the speakers long enough to play a couple of symphonies - it's only since the third day that bluetooth always turns itself off.)
Have I misunderstood? I thought the service would just run once at boot time. Do I have to specify something else?
Below is the latest version of the service.
If I comment out the Type=oneshot
, it behaves the same. I included the WantedBy=bluetooth.target
because it creates a symlink from /etc/systemd/system/bluetooth.target.wants/turn-bluetooth-off.service
to /etc/systemd/system/turn-bluetooth-off.service
and if the symlink is missing, the service does not run at all.
sudo rfkill unblock bluetooth
does not work any better at turning bluetooth on.
[Unit]
Description=Service to always turn bluetooth off at system start time
After=bluetooth.service
After=NetworkManager.service
After=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/turn-bluetooth-off.sh
[Install]
WantedBy=multi-user.target
WantedBy=bluetooth.target
and the file /usr/local/bin/turn-bluetooth-off.sh
#!/bin/bash
date > /root/disk_space_report.txt
rfkill block bluetooth
du -sh /home/ >> /root/disk_space_report.txt
I run
$ systemctl daemon-reload
$ systemctl enable turn-bluetooth-off.service
Created symlink from /etc/systemd/system/default.target.wants/turn-bluetooth-off.service to /etc/systemd/system/turn-bluetooth-off.service.
After a reboot journalctl --unit=bluetooth
shows (and some of these lines are in red):
-- Logs begin at Mon 2020-06-08 17:18:28 CEST, end at Mon 2020-06-08 17:43:45 CEST. --
Jun 08 17:18:41 Otilia systemd[1]: Starting Bluetooth service...
Jun 08 17:18:41 Otilia bluetoothd[824]: Bluetooth daemon 5.37
Jun 08 17:18:41 Otilia systemd[1]: Started Bluetooth service.
Jun 08 17:18:41 Otilia bluetoothd[824]: Starting SDP server
Jun 08 17:18:42 Otilia bluetoothd[824]: Bluetooth management interface 1.13 initialized
Jun 08 17:18:44 Otilia bluetoothd[824]: Failed to obtain handles for "Service Changed" characteristic
Jun 08 17:18:44 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:18:44 Otilia bluetoothd[824]: Error adding Link Loss service
Jun 08 17:18:44 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:18:44 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:18:44 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:18:44 Otilia bluetoothd[824]: Current Time Service could not be registered
Jun 08 17:18:44 Otilia bluetoothd[824]: gatt-time-server: Input/output error (5)
Jun 08 17:18:44 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:18:44 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:18:44 Otilia bluetoothd[824]: Sap driver initialization failed.
Jun 08 17:18:44 Otilia bluetoothd[824]: sap-server: Operation not permitted (1)
Jun 08 17:19:38 Otilia bluetoothd[824]: Failed to obtain handles for "Service Changed" characteristic
Jun 08 17:19:38 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:19:38 Otilia bluetoothd[824]: Error adding Link Loss service
Jun 08 17:19:38 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:19:38 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:19:38 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:19:38 Otilia bluetoothd[824]: Current Time Service could not be registered
Jun 08 17:19:38 Otilia bluetoothd[824]: gatt-time-server: Input/output error (5)
Jun 08 17:19:38 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:19:38 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:19:38 Otilia bluetoothd[824]: Sap driver initialization failed.
Jun 08 17:19:38 Otilia bluetoothd[824]: sap-server: Operation not permitted (1)
Jun 08 17:19:39 Otilia bluetoothd[824]: Endpoint registered: sender=:1.48 path=/MediaEndpoint/A2DPSource
Jun 08 17:19:39 Otilia bluetoothd[824]: Endpoint registered: sender=:1.48 path=/MediaEndpoint/A2DPSink
and journalctl --unit=turn-bluetooth-off
shows:
-- Logs begin at Mon 2020-06-08 17:18:28 CEST, end at Mon 2020-06-08 17:43:45 CEST. --
Jun 08 17:18:51 Otilia systemd[1]: Starting Service to always turn bluetooth off at system start time...
Jun 08 17:21:21 Otilia systemd[1]: Started Service to always turn bluetooth off at system start time.
and rfkill list all
shows
0: Toshiba Bluetooth: Bluetooth
Soft blocked: no
Hard blocked: no
1: phy0: Wireless LAN
Soft blocked: no
Hard blocked: no
3: hci0: Bluetooth
Soft blocked: yes
Hard blocked: no
N.B. Sometimes hci0
is not shown and the soft block is shown against one of the other items.
Now I try turning bluetooth on using the Bluetooth top panel applet. The applet displays ON and then after about ten seconds it displays OFF again.
New output from journalctl --unit=bluetooth
Jun 08 20:43:29 Otilia bluetoothd[824]: Failed to obtain handles for "Service Changed" characteristic
Jun 08 20:43:29 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 20:43:29 Otilia bluetoothd[824]: Error adding Link Loss service
Jun 08 20:43:29 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 20:43:29 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 20:43:29 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 20:43:29 Otilia bluetoothd[824]: Current Time Service could not be registered
Jun 08 20:43:29 Otilia bluetoothd[824]: gatt-time-server: Input/output error (5)
Jun 08 20:43:29 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 20:43:29 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 20:43:29 Otilia bluetoothd[824]: Sap driver initialization failed.
Jun 08 20:43:29 Otilia bluetoothd[824]: sap-server: Operation not permitted (1)
Jun 08 20:43:29 Otilia bluetoothd[824]: Endpoint registered: sender=:1.48 path=/MediaEndpoint/A2DPSource
Jun 08 20:43:29 Otilia bluetoothd[824]: Endpoint registered: sender=:1.48 path=/MediaEndpoint/A2DPSink
Jun 08 20:43:31 Otilia bluetoothd[824]: Endpoint unregistered: sender=:1.48 path=/MediaEndpoint/A2DPSource
Jun 08 20:43:31 Otilia bluetoothd[824]: Endpoint unregistered: sender=:1.48 path=/MediaEndpoint/A2DPSink
New output from journalctl --unit=turn-bluetooth-off
Jun 08 20:43:30 Otilia systemd[1]: Starting Service to always turn bluetooth off at system start time...
Jun 08 20:43:33 Otilia systemd[1]: Started Service to always turn bluetooth off at system start time.
and rfkill list all
shows
0: Toshiba Bluetooth: Bluetooth
Soft blocked: yes
Hard blocked: no
1: phy0: Wireless LAN
Soft blocked: no
Hard blocked: no
I can't see what is causing my service to be run again. Help!