I am using the bluetooth module for python import bluetooth
which I believe is the PyBluez package. I am able to connect, send, and receive just fine from the bluetooth.BluetoothSocket class but my application is completely blind when it comes to the status of the connection.
I want my application to disable certain functionality when the device is disconnected but there does not seem to be any BluetoothSocket.is_connected() methods of any kind. I would like it to detect changes in the bluetooth status as soon as they occur.
Usually there are multiple topics about something as simple as this, so apologies if this is a duplicate. I have searched this site multiple times for an answer but found nothing specific to python.
If your SO is linux, you could use the hcitool, which will tell you the status of your bluetooth devices.
Please find below a small Python snippet that can accomplish your need. You will need to know your bluetooth device's MAC:
import subprocess as sp
stdoutdata = sp.getoutput("hcitool con")
if "XX:XX:XX:XX:XX:XX" in stdoutdata.split():
print("Bluetooth device is connected")
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With