Adafruit СircuitPython backend
I have a sample solution to get bleak working on CircuitPython. For now I able to run BleakScanner on my esp32-s3 with no issues.
The general problem is some built-in dependencies is absent in CircuitPython (functools, inspect, uuid, asyncio.timeout). I used to replace it with native approaches for CircuitPython and it works, I used just adafruit _bleio and asyncio modules to make it.
The second problem is adafruit _bleio module is global and synchronous and can be used just on main thread, but there also few sollutions there.
I'm thinking about using a monkeypatching for the absent modules to get a seamless way to use bleak on CircuitPython
CircuitPython should be able to use bleak already. See https://github.com/adafruit/Adafruit_Blinka_bleio , that's on low-level. Adafruit expects BLE users to actually load the higher-level helper https://github.com/adafruit/Adafruit_CircuitPython_BLE, which should be part of Adafruit CircuitPython Bundle.
The goal of https://github.com/adafruit/Adafruit_Blinka_bleio is to be able to use _bleio (and higher level libraries) on CPython. It uses bleak under the covers (though we haven't updated to newer bleak in a long time). See https://learn.adafruit.com/circuitpython-ble-libraries-on-any-computer. It is not meant to be run under CircuitPython.
We did not consider bleak as an API to be implemented by _bleio, because it did not support peripheral mode, only central.
@nomadbyte
It seems you are mistaken, this is a slightly different thing, blinka bleio is for linux and only, they themselves write to use Adafruit_CircuitPython_BLE on microcontrollers, which in turn is based purely on low-level _bleio and does not use bleak in any way. And also it's not provide bleak compatible interface, which I trying to do. Yeap I made a mistake saying I use _bleio directly, I use Adafruit_CircuitPython_BLE
The goal of https://github.com/adafruit/Adafruit_Blinka_bleio is to be able to use
_bleio(and higher level libraries) on CPython. It usesbleakunder the covers (though we haven't updated to newer bleak in a long time). See https://learn.adafruit.com/circuitpython-ble-libraries-on-any-computer. It is not meant to be run under CircuitPython.We did not consider
bleakas an API to be implemented by_bleio, because it did not support peripheral mode, only central.
Yes, that's what it's about. My suggestion is to add CircuitPython support to bleak with minimal loss or change. For now _bleio works quite good
PR #1814
import sys
import os
import asyncio
from bleak import BleakScanner
print(f"{sys.platform=}, {os.uname()=}")
print(f"{sys.version=}")
print(f"{sys.implementation=}")
async def main():
devices = await BleakScanner.discover(timeout=15, return_adv=True)
for dev, adv in devices.items():
print(dev, adv)
if __name__ == "__main__":
asyncio.run(main())
sys.platform=Espressif, os.uname()=(sysname='ESP32S3', nodename='ESP32S3', release='9.2.6', version='9.2.6 on 2025-03-23', machine='LILYGO T-DISPLAY S3 v1.2 with ESP32S3')
sys.version=3.4.0; CircuitPython 9.2.6 on 2025-03-23
sys.implementation=(name='circuitpython', version=(9, 2, 6, ''), _machine='LILYGO T-DISPLAY S3 v1.2 with ESP32S3', _mpy=774)
<Address d2:09:33:f2:c6:a1> (BLEDevice(<Address d2:09:33:f2:c6:a1>, None), AdvertisementData(rssi=-79))
<Address f7:05:a7:40:b7:1b> (BLEDevice(<Address f7:05:a7:40:b7:1b>, Mi Smart Band 6), AdvertisementData(local_name='Mi Smart Band 6', service_uuids=[<StandardUUID object at 0x3c1a08a0>], rssi=-76))
<Address e9:d4:14:dc:af:e5> (BLEDevice(<Address e9:d4:14:dc:af:e5>, EB3A2308014262580), AdvertisementData(local_name='EB3A2308014262580', service_uuids=[<StandardUUID object at 0x3c1a0ed0>], rssi=-78))
<Address f6:ba:4b:28:e8:a3> (BLEDevice(<Address f6:ba:4b:28:e8:a3>, EB3A2309014241321), AdvertisementData(local_name='EB3A2309014241321', service_uuids=[<StandardUUID object at 0x3c1a0730>], rssi=-68))
<Address 20:1f:78:56:54:ae> (BLEDevice(<Address 20:1f:78:56:54:ae>, None), AdvertisementData(rssi=-49))