diff -r 6518c336fcd3 -r 829c1edbf253 src/eric7/MicroPython/Devices/EspDevices.py --- a/src/eric7/MicroPython/Devices/EspDevices.py Wed Mar 08 17:40:14 2023 +0100 +++ b/src/eric7/MicroPython/Devices/EspDevices.py Wed Mar 08 19:27:43 2023 +0100 @@ -1183,6 +1183,75 @@ return out.strip() == b"True" + def getDeviceScan(self, timeout=10): + """ + Public method to perform a Bluetooth device scan. + + @param timeout duration of the device scan in seconds (defaults + to 10) + @type int (optional) + @return tuple containing a dictionary with the scan results and + an error string + @rtype tuple of (dict, str) + """ + from ..BluetoothDialogs.BluetoothAdvertisement import BluetoothAdvertisement + + command = """ +def ble_scan(): + import bluetooth + import time + import ubinascii + + IRQ_SCAN_RESULT = 5 + IRQ_SCAN_DONE = 6 + + def _bleIrq(event, data): + if event == IRQ_SCAN_RESULT: + addr_type, addr, adv_type, rssi, adv_data = data + if addr: + print({{ + 'address': ubinascii.hexlify(addr,':').decode('utf-8'), + 'rssi': rssi, + 'adv_type': adv_type, + 'advertisement': bytes(adv_data), + }}) + + ble = bluetooth.BLE() + + ble_active = ble.active() + if not ble_active: + ble.active(True) + + ble.irq(_bleIrq) + ble.gap_scan({0} * 1000, 1000000, 50000, True) + time.sleep({0}) + + if not ble_active: + ble.active(False) + +ble_scan() +del ble_scan +""".format( + timeout + ) + out, err = self._interface.execute( + command, mode=self._submitMode, timeout=(timeout + 5) * 1000 + ) + if err: + return {}, err + + scanResults = {} + for line in out.decode("utf-8").splitlines(): + res = ast.literal_eval(line) + address = res["address"] + if address not in scanResults: + scanResults[address] = BluetoothAdvertisement(address) + scanResults[address].update( + res["adv_type"], res["rssi"], res["advertisement"] + ) + + return scanResults, "" + def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): """