src/eric7/MicroPython/Devices/EspDevices.py

branch
eric7
changeset 10032
102b79b2a8cd
parent 10022
a95800b414b7
child 10069
435cc5875135
equal deleted inserted replaced
10031:6d4b46787f30 10032:102b79b2a8cd
1372 1372
1373 if self.hasCircuitPython(): 1373 if self.hasCircuitPython():
1374 return self.__cpyDevice.getDeviceScan(timeout) 1374 return self.__cpyDevice.getDeviceScan(timeout)
1375 1375
1376 command = """ 1376 command = """
1377 _scan_done = False
1378
1377 def ble_scan(): 1379 def ble_scan():
1378 import bluetooth 1380 import bluetooth
1379 import time 1381 import time
1380 import ubinascii 1382 import ubinascii
1381 1383
1382 IRQ_SCAN_RESULT = 5 1384 IRQ_SCAN_RESULT = 5
1383 IRQ_SCAN_DONE = 6 1385 IRQ_SCAN_DONE = 6
1384 1386
1385 def _bleIrq(event, data): 1387 def _bleIrq(event, data):
1388 global _scan_done
1386 if event == IRQ_SCAN_RESULT: 1389 if event == IRQ_SCAN_RESULT:
1387 addr_type, addr, adv_type, rssi, adv_data = data 1390 addr_type, addr, adv_type, rssi, adv_data = data
1388 if addr: 1391 if addr:
1389 print({{ 1392 print({{
1390 'address': ubinascii.hexlify(addr,':').decode('utf-8'), 1393 'address': ubinascii.hexlify(addr,':').decode('utf-8'),
1391 'rssi': rssi, 1394 'rssi': rssi,
1392 'adv_type': adv_type, 1395 'adv_type': adv_type,
1393 'advertisement': bytes(adv_data), 1396 'advertisement': bytes(adv_data),
1394 }}) 1397 }})
1398 elif event == IRQ_SCAN_DONE:
1399 _scan_done = True
1395 1400
1396 ble = bluetooth.BLE() 1401 ble = bluetooth.BLE()
1397 1402
1398 ble_active = ble.active() 1403 ble_active = ble.active()
1399 if not ble_active: 1404 if not ble_active:
1400 ble.active(True) 1405 ble.active(True)
1401 1406
1402 ble.irq(_bleIrq) 1407 ble.irq(_bleIrq)
1403 ble.gap_scan({0} * 1000, 1000000, 50000, True) 1408 ble.gap_scan({0} * 1000, 1000000, 50000, True)
1404 time.sleep({0}) 1409 while not _scan_done:
1410 time.sleep(0.2)
1405 1411
1406 if not ble_active: 1412 if not ble_active:
1407 ble.active(False) 1413 ble.active(False)
1408 1414
1409 ble_scan() 1415 ble_scan()
1410 del ble_scan 1416 del ble_scan, _scan_done
1411 """.format( 1417 """.format(
1412 timeout 1418 timeout
1413 ) 1419 )
1414 out, err = self.executeCommands( 1420 out, err = self.executeCommands(
1415 command, mode=self._submitMode, timeout=(timeout + 5) * 1000 1421 command, mode=self._submitMode, timeout=(timeout + 5) * 1000

eric ide

mercurial