1180 Public method to get Bluetooth status data of the connected board. |
1181 Public method to get Bluetooth status data of the connected board. |
1181 |
1182 |
1182 @return list of tuples containing the translated status data label and |
1183 @return list of tuples containing the translated status data label and |
1183 the associated value |
1184 the associated value |
1184 @rtype list of tuples of (str, str) |
1185 @rtype list of tuples of (str, str) |
|
1186 @exception OSError raised to indicate an issue with the device |
1185 """ |
1187 """ |
1186 command = """ |
1188 command = """ |
1187 def ble_status(): |
1189 def ble_status(): |
1188 import _bleio |
1190 import _bleio |
1189 import binascii |
1191 import binascii |
1276 out, err = self._interface.execute(command, mode=self._submitMode) |
1280 out, err = self._interface.execute(command, mode=self._submitMode) |
1277 if err: |
1281 if err: |
1278 raise OSError(self._shortError(err)) |
1282 raise OSError(self._shortError(err)) |
1279 |
1283 |
1280 return out.strip() == b"True" |
1284 return out.strip() == b"True" |
|
1285 |
|
1286 def getDeviceScan(self, timeout=10): |
|
1287 """ |
|
1288 Public method to perform a Bluetooth device scan. |
|
1289 |
|
1290 @param timeout duration of the device scan in seconds (defaults |
|
1291 to 10) |
|
1292 @type int (optional) |
|
1293 @return tuple containing a dictionary with the scan results and |
|
1294 an error string |
|
1295 @rtype tuple of (dict, str) |
|
1296 """ |
|
1297 from ..BluetoothDialogs.BluetoothAdvertisement import ( |
|
1298 ADV_IND, |
|
1299 ADV_SCAN_IND, |
|
1300 SCAN_RSP, |
|
1301 BluetoothAdvertisement, |
|
1302 ) |
|
1303 |
|
1304 command = """ |
|
1305 def ble_scan(): |
|
1306 import _bleio |
|
1307 import binascii |
|
1308 import time |
|
1309 |
|
1310 a = _bleio.adapter |
|
1311 |
|
1312 ble_enabled = a.enabled |
|
1313 if not ble_enabled: |
|
1314 a.enabled = True |
|
1315 |
|
1316 scanResults = a.start_scan( |
|
1317 buffer_size=1024, extended=True, timeout={0}, minimum_rssi=-100, active=True |
|
1318 ) |
|
1319 time.sleep(10) |
|
1320 a.stop_scan() |
|
1321 |
|
1322 for res in scanResults: |
|
1323 print({{ |
|
1324 'address': binascii.hexlify( |
|
1325 bytes(reversed(res.address.address_bytes)), ':' |
|
1326 ).decode(), |
|
1327 'advertisement': res.advertisement_bytes, |
|
1328 'connectable': res.connectable, |
|
1329 'rssi': res.rssi, |
|
1330 'scan_response': res.scan_response, |
|
1331 }}) |
|
1332 |
|
1333 if not ble_enabled: |
|
1334 a.enabled = False |
|
1335 |
|
1336 ble_scan() |
|
1337 del ble_scan |
|
1338 """.format( |
|
1339 timeout |
|
1340 ) |
|
1341 out, err = self._interface.execute( |
|
1342 command, mode=self._submitMode, timeout=(timeout + 5) * 1000 |
|
1343 ) |
|
1344 if err: |
|
1345 return {}, err |
|
1346 |
|
1347 scanResults = {} |
|
1348 for line in out.decode("utf-8").splitlines(): |
|
1349 res = ast.literal_eval(line) |
|
1350 address = res["address"] |
|
1351 if address not in scanResults: |
|
1352 scanResults[address] = BluetoothAdvertisement(address) |
|
1353 if res["scan_response"]: |
|
1354 advType = SCAN_RSP |
|
1355 elif res["connectable"]: |
|
1356 advType = ADV_IND |
|
1357 else: |
|
1358 advType = ADV_SCAN_IND |
|
1359 scanResults[address].update(advType, res["rssi"], res["advertisement"]) |
|
1360 |
|
1361 return scanResults, "" |
1281 |
1362 |
1282 |
1363 |
1283 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): |
1364 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): |
1284 """ |
1365 """ |
1285 Function to instantiate a MicroPython device object. |
1366 Function to instantiate a MicroPython device object. |