1

i have connected a bluetooth controller to control a rover. i am able to ping the controller using sudo l2ping 84:30:95:06:C6:6C on the terminal. basically i want to execute certain code when ping is not available. i.e., when controller is disconnected. i tried this:

import bluetooth

while True:
    if bluetooth.lookup_name('84:30:95:06:C6:6C'):
        print("do nothing")

    else:
        print("do something")

but this has bit of delay, it takes around 2-3 seconds to give me the output when the controller is disconnected. is there any other way of doing this in python ?

siddharth
  • 57
  • 7

1 Answers1

0

so we can use hcitool to know the status of the bluetooth device. here is a small snippet that worked for me. there is no delay to get the output and this works absolutely fine.

import subprocess as sp

dev_addr = '84:30:95:06:C6:6C'
stdoutdata = sp.getoutput("hcitool con")

while True:
    if dev_addr in stdoutdata.split():
        print("do nothing")

    if dev_addr not in stdoutdata.split():
        print("do something")
siddharth
  • 57
  • 7
  • `hcitool` is one of the eight tools that got [deprecated](https://git.kernel.org/pub/scm/bluetooth/bluez.git/commit/?id=b1eb2c4cd057624312e0412f6c4be000f7fc3617) back in 2017 so will disappear at some point. – ukBaz Dec 25 '20 at 06:59