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
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.