roshanr
Posts: 2
Joined: Tue Feb 27, 2018 3:13 am

Bluetooth RFCOMM Server not working when using SystemD

Tue Feb 27, 2018 3:29 am

Hi all,

I've been having some issues with the Bluetooth RFCOMM Server I've written for an open source project I've been working on in the particular case that it is used within a daemon.

We have a Python Bluetooth server that seems to work perfectly well, when I run the server directly from the CLI using the pi user, it works fine and am able to access from our app (Cordova/Java Bluetooth plugin). When run from the CLI, it transfers the data from the app, through the Bluetooth server and to the web hook.

However, we need this script to be daemonized as it'll be used mainly headless and having to SSH via an ethernet tether will become cumbersome.

When the server is run using the below daemon it "runs" the server fine with no "errors" but clearly something is off. The tablet thinks it's connected and has no errors; I have also tested the app when the daemon is not running, and it can clearly distinguish the two states even though the daemon has no output.The daemon has no logs of connection nor does it connect to the webhook whereas running from CLI does. I'm led to believe that's it's something different about running in the sandboxed root env provided by systemd as opposed to the local pi user, yet I can't seem to figure out what that may be. It's almost as if line 31 in the server script below hangs when run in the systemd environment, but not so when run via CLI.

Link to Server: https://github.com/FRCteam4909/The-Gree ... -worker.py
Link to Daemon: https://github.com/FRCteam4909/The-Gree ... er.service

Relevant Bluetooth/Daemon Install/Config: https://github.com/FRCteam4909/The-Gree ... ALL.sh#L75

Any help would be greatly appreciated!

BBUK
Posts: 138
Joined: Tue Dec 18, 2012 10:34 am

Re: Bluetooth RFCOMM Server not working when using SystemD

Wed Feb 28, 2018 4:53 pm

I see that the daemon uses stdout. I am not sure where that will end up when executed via systemd and your daemon could well be hanging at any stdout writes.

Have you tried the daemon code in a form that does not use stdout (no daemon would normally be attached to a terminal)? As a quick and dirty test you could use screen or tmux in the systemd file (that *will* make stdout available to the code). Something like:

Code: Select all

ExecStart=/usr/bin/screen -S daemon -d -m sh -c "( /usr/bin/python3 /home/pi/the-green-alliance/bluetooth-worker/bluetooth-worker.py 2>&1) | tee -a /var/log/daemon.log"
ExecStop=/usr/bin/screen -S daemon -X daemon
(untested)

BBUK

roshanr
Posts: 2
Joined: Tue Feb 27, 2018 3:13 am

Re: Bluetooth RFCOMM Server not working when using SystemD

Thu Mar 01, 2018 12:13 am

Thanks for your quick response, I was able to find that it was a JSON parsing error, only that the daemon would quit too quick for the stdout buffer to log the issue; thus the connection would appear to not work. All set now, thanks again!

Return to “Advanced users”