Bluetooth aioble threading

Hi,

I’m running into a threading issue when switching from the old ble_temperature.py with bluetooth.BLE() to the new temp_sensor_aioble.py with aioble.

When I used to run self._ble.gap_advertise(interval_us, adv_data=self._payload), the thread would continue while waiting for a connection. In the latest firmware, it errors out on self._ble.active(True).

So I switched over to aioble, but when asyncio.run(main()) is called, the thread hangs until a connection is made via async with await aioble.advertise( and no other code runs while waiting for a connection.

In the temp_sensor_aioble.py example, it only prints 2 after a connection is made:

print("1")
asyncio.run(main())
print("2")

Is there a way to keep the cam processing while waiting for a BLE connection and waiting for a charicteristic to be written to?

You just need to run your code in another asyncio task, and it should occasionally yield with await asyncio.sleep().

Note these examples are based on ones from MicroPython, I’m not a BLE/aioble expert, if you need more help with this you could try MicroPython’s discussions or their discord.

Ah, so its asyncio tasks all the way down. Thanks!