This is my code. loop is called every second.
Code:
def bt_mainthread():
global bt_busy
thread.start_new_thread(bt_scan,())
time.sleep(1)
while bt_busy and (bt_scan_time < 120):
time.sleep(1)
if bt_scan_time > 119:
blues.off()
time.sleep(5)
blues.on()
time.sleep(5)
bt_busy = False
def bt_scan():
global btscan, btquery_list, bt_busy, bt_scan_time
bt_busy = True
bt_scan_time = 0
try:
btscan = lightblue.finddevices()
except:
btscan = []
btquery_list = []
for i in range(len(btscan)):
btquery_list.append(query_bt(btscan[i]))
bt_busy = False
def loop():
global bt_busy, bt_scan_time
if config['BT_SCAN'] > 0 and not bt_busy:
bti += 1
if (bti > config['BT_SCAN']*1) and not bt_busy:
bti = 0
thread.start_new_thread(bt_mainthread,())
if bt_busy:
bt_block += 1
bt_scan_time += 1