Moode Forum

Full Version: Detect if Bluetooth is active/playing using python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How can I, using python(3), check if the Bluetooth connection is active and, and if a device is connected?
The active flags are below. "1" = active.

Code:
pi@rp2:~ $ moodeutl -q "select * from cfg_system where param like '%active%'"
17|rbactive|0
50|aplactive|0
83|btactive|0
94|slactive|0
103|spotactive|0
120|inpactive|0
pi@rp2:~ $

To get the name of a connected Bluetooth device use the command below and parse the output.

Code:
# No device connected
pi@rp2:~ $ sudo /var/www/command/bt.sh -c
** Connected devices
**
**
pi@rp2:~ $

# Device connected
pi@rp2:~ $ sudo /var/www/command/bt.sh -c
** Connected devices
**
** XX:XX:XX:XX:XX:XX Tim's iPhone
**
pi@rp2:~ $ 
Is it in any way possible to do this in Python?
For sqlite: https://docs.python.org/3/library/sqlite3.html
For sudo /var/www/command/bt.sh -c, you can use subprocess: https://docs.python.org/fr/3/library/subprocess.html