1181 if err: |
1181 if err: |
1182 raise OSError(self._shortError(err)) |
1182 raise OSError(self._shortError(err)) |
1183 |
1183 |
1184 return out.strip() == b"True" |
1184 return out.strip() == b"True" |
1185 |
1185 |
|
1186 def getDeviceScan(self, timeout=10): |
|
1187 """ |
|
1188 Public method to perform a Bluetooth device scan. |
|
1189 |
|
1190 @param timeout duration of the device scan in seconds (defaults |
|
1191 to 10) |
|
1192 @type int (optional) |
|
1193 @return tuple containing a dictionary with the scan results and |
|
1194 an error string |
|
1195 @rtype tuple of (dict, str) |
|
1196 """ |
|
1197 from ..BluetoothDialogs.BluetoothAdvertisement import BluetoothAdvertisement |
|
1198 |
|
1199 command = """ |
|
1200 def ble_scan(): |
|
1201 import bluetooth |
|
1202 import time |
|
1203 import ubinascii |
|
1204 |
|
1205 IRQ_SCAN_RESULT = 5 |
|
1206 IRQ_SCAN_DONE = 6 |
|
1207 |
|
1208 def _bleIrq(event, data): |
|
1209 if event == IRQ_SCAN_RESULT: |
|
1210 addr_type, addr, adv_type, rssi, adv_data = data |
|
1211 if addr: |
|
1212 print({{ |
|
1213 'address': ubinascii.hexlify(addr,':').decode('utf-8'), |
|
1214 'rssi': rssi, |
|
1215 'adv_type': adv_type, |
|
1216 'advertisement': bytes(adv_data), |
|
1217 }}) |
|
1218 |
|
1219 ble = bluetooth.BLE() |
|
1220 |
|
1221 ble_active = ble.active() |
|
1222 if not ble_active: |
|
1223 ble.active(True) |
|
1224 |
|
1225 ble.irq(_bleIrq) |
|
1226 ble.gap_scan({0} * 1000, 1000000, 50000, True) |
|
1227 time.sleep({0}) |
|
1228 |
|
1229 if not ble_active: |
|
1230 ble.active(False) |
|
1231 |
|
1232 ble_scan() |
|
1233 del ble_scan |
|
1234 """.format( |
|
1235 timeout |
|
1236 ) |
|
1237 out, err = self._interface.execute( |
|
1238 command, mode=self._submitMode, timeout=(timeout + 5) * 1000 |
|
1239 ) |
|
1240 if err: |
|
1241 return {}, err |
|
1242 |
|
1243 scanResults = {} |
|
1244 for line in out.decode("utf-8").splitlines(): |
|
1245 res = ast.literal_eval(line) |
|
1246 address = res["address"] |
|
1247 if address not in scanResults: |
|
1248 scanResults[address] = BluetoothAdvertisement(address) |
|
1249 scanResults[address].update( |
|
1250 res["adv_type"], res["rssi"], res["advertisement"] |
|
1251 ) |
|
1252 |
|
1253 return scanResults, "" |
|
1254 |
1186 |
1255 |
1187 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): |
1256 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): |
1188 """ |
1257 """ |
1189 Function to instantiate a MicroPython device object. |
1258 Function to instantiate a MicroPython device object. |
1190 |
1259 |