1

I have a Raspberry Pi 4B running the latest version of Buster. I am going to run the system headless booting directly to a command prompt (but not the lite version). I have written a python program to control my jukebox and wanted to add a Bluetooth sink as an option that can be turned on and off. It will pair with any device (does not have to be trusted) not requiring a pin or anything to be done on the Raspberry Pi to accept the connection. I turn it on and off in the python code by using the following sub-process commands:

#Turn Bluetooth off
subprocess.call(['sudo', 'hciconfig', 'hci0', 'noscan'])
subprocess.call(['sudo', 'hciconfig', 'hci0', 'down'])

#Turn Bluetooth on
subprocess.call(['sudo', 'hciconfig', 'hci0', 'up'])
subprocess.call(['sudo', 'hciconfig', 'hci0', 'piscan'])

I used the following websites as a guide:

https://www.raspberrypi.org/forums/viewtopic.php?t=247892
https://sigmdel.ca/michel/ha/rpi/bluetooth_n_buster_01_en.html
https://www.raspberrypi.org/forums/viewtopic.php?t=235519

All this seems to work well. I'm able to turn bluetooth on and off and stream audio from my phone to the Raspberry Pi to play over hardwired speakers.

My issue is that during shutdown and restarts I get to a point that says: "A stop job is running for Bluetooth Auth Agent (x's / 1min 30s)" and it takes the full minute and a half to shut down. Here is what I did to enable the bluetooth audio sink:

1. sudo apt install bluealsa
2. sudo nano /lib/systemd/system/bluealsa.service
3. edit the ExecStart line and add "--profile=a2dp-sink" to the end of the line
4. once changes have been made, hit Ctrl-X to save and exit
5. sudo adduser pi bluetooth
6. sudo nano /etc/systemd/system/aplay.service
    [Unit]
    Description=BlueALSA aplay service
    After=bluetooth.service
    Requires=bluetooth.service
     
    [Service]
    User=pi 
    ExecStart=/usr/bin/bluealsa-aplay 00:00:00:00:00:00
    Restart=on-failure  
    Restart=always
    RestartSec=30
     
    [Install]
    WantedBy=multi-user.target

7. sudo systemctl enable aplay
8. sudo apt-get install bluez-tools
9. sudo nano /etc/systemd/system/bt-agent.service
    [Unit]
    Description=Bluetooth Auth Agent
    After=bluetooth.service
    PartOf=bluetooth.service

    [Service]
    Type=simple
    ExecStart=/usr/bin/bt-agent -c NoInputNoOutput
    ExecStartPost=/bin/sleep 1
    ExecStartPost=/bin/hciconfig hci0 piscan
    ExecStartPost=/bin/hciconfig hci0 sspmode 1


    [Install]
    WantedBy=bluetooth.target

10. sudo nano /etc/bluetooth/main.conf
11.  Uncomment out:
    Class = 0x41C
    DiscoverableTimeout = 0
12. sudo nano /etc/machine-info
    PRETTY_HOSTNAME=Jukebox
13. Reboot

Unfortunately, I don't know enough about Linux to troubleshoot what's going on. I'm hoping this forum can help me. I'm guessing I have an issue with the bt-agent.service but I don't know how to fix it. Even though it seems to work, did I miss something to configure this correctly? Can I kill a service before issuing the shutdown command? It's not like there is data going over the bluetooth stream that could get corrupted.

Any help/guidance would be greatly appreciated. I'm stumped.

After some troubleshooting, it definitely has something to do with step 9. If I rename that file, the shutdown command works fine (it doesn't wait for over a minute). The issue is then it doesn't auto accept connections. Is there another way to run "/usr/bin/bt-agent -c NoInputNoPutput" if not in a "service file"? If I run from a command prompt it works it accepts connections like I want, but it doesn't return to the prompt (is this why the shutdown command behaves the way it does?).

3 Answers 3

1

You can use sudo shutdown -f to force the system not to wait for services to stop.

See How safe is sudo halt -f to shutdown a RPi?

1
  • Thanks. that will solve the immediate problem. Is there any troubleshooting I should be doing to figure out what is causing this behavior?
    – Brian
    Apr 6, 2021 at 20:52
0

Try to use KillSignal=SIGUSR1 for bt-agent.service, like in this example:

[Unit]
Description=Bluetooth Auth Agent
Requires=bluetooth.service
After=bluetooth.service

[Service]
ExecStart=/usr/bin/bt-agent -c DisplayOnly -p /etc/bluetooth/bluez_pin.txt
Type=simple
Restart=always
KillSignal=SIGUSR1

[Install]
WantedBy=multi-user.target
1
  • @Brian, can you check this solution? Nov 12, 2021 at 13:18
0

I just had a similar problem and solved it by adding a TimeoutStopSec of a few seconds in the Bluetooth-Agent-service.

sudo nano /etc/systemd/system/bt-agent.service
...
[Service]
Type=simple
ExecStart=/usr/bin/bt-agent -c NoInputNoOutput
TimeoutStopSec=5
...
sudo systemctl daemon-reload

sudo systemctl restart bt-agent

for details see https://linuxconfig.org/how-to-create-systemd-service-unit-in-linux or https://www.freedesktop.org/software/systemd/man/systemd.service.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.