0

I'm trying to execute a script on a raspberry pi to control a bluetooth device via a url cgi (apache2) script. There may be other ways to do this, but I've struggled to get php installed without bricking the OS (several hours wasted) and I'm not interested in anything complicated.

I have a working bash script that controls the bluetooth device from the raspberry pi. However, when this same script is called via a url (i.e. /cgi-bin/example5.sh) bluetoothctl returns "No default controller available" instead. I can run simple scripts with no problem, but somehow bluetoothctl isn't executing the same way via the url. Is there possibly an ownership or environment variable problem" The script has 755 permissions and owned by root. Any help in debugging would be appreciated.

My script is /usr/lib/cgi-bin/example5.sh and simply calls tries to connect to the bluetooth device via bluetoothctl (real bluetooth device mac address removed because, you know, people).

#!/bin/bash -e
echo Content-type: text/plain
echo
bluetoothctl -- connect "XX:XX:XX:XX:XX:XX"

From the command line this works correctly:

pi@raspberrypi:/usr/lib/cgi-bin $ ./example5.sh
Content-type: text/plain

Attempting to connect to XX:XX:XX:XX:XX:XX
Connection successful

When I enter http://<my local IP address>/cgi-bin/example5.sh I get the following in the webpage.

No default controller available
10
  • 1
    #!/bin/bash echo ./i.sh 2 is that actually what you have in the file/. that's not a valid invocation of bash Commented Feb 4, 2023 at 17:29
  • #!/bin/bash when you do this, your script won't end on errors. Use #!/bin/bash -e usually so that your script will "error out" rather than continuing with further commands which will probably also fail Commented Feb 4, 2023 at 17:31
  • 1
    Please take a look at How to create a Minimal, Complete, and Verifiable example. Commented Feb 4, 2023 at 17:36
  • 1
    Everyone, sorry for the poor formatting, but I am trying, thank you for your patience. erik258 - thanks, I've added the -e Cyrus/Shelter - I've tried your suggestions, but it still gives an error about formatting as code... I've deleted most of the web output, because it's irrelevant after the "No default controller available" error. Is it any better? Commented Feb 4, 2023 at 23:20
  • 1
    shelter, based on your comment on expect I made a very simple script without it and called it directly without using two scripts, but it yields the same result. I updated the body with the scripts and results. Commented Feb 5, 2023 at 15:07

1 Answer 1

1

SOLVED! It turns out the default Apache2 user www-data didn't have permission to run bluetoothctl. I added the following to /etc/dbus-1/system.d/bluetooth.conf

  <policy user="www-data">
    <allow send_destination="org.bluez"/>
    <allow send_interface="org.bluez.Agent1"/>
    <allow send_interface="org.bluez.GattCharacteristic1"/>
    <allow send_interface="org.bluez.GattDescriptor1"/>
    <allow send_interface="org.freedesktop.DBus.ObjectManager"/>
    <allow send_interface="org.freedesktop.DBus.Properties"/>
  </policy>

Thanks to everyone for helping out.

Sign up to request clarification or add additional context in comments.

1 Comment

Well done! You can accept your own answer after 48 hrs and gain valuable reputation points! Keep posting, and keep it as simple as possible, but no less! (-;! (You might want to understand that restriction and why it is there. If this is just your home server, little risk, but if at your job and a production system, there is likely a security risk that cause Apache to "turrn it off".).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.