I am trying to run a example code given on github to discover BLE devices and then get connected to my device. My sample code is as below. Performance of the code is very unstable. Out of about 10 tries, I am able to connect to my device only once for remaining 9 times I get timeout error. Timeout error is also given below, Can someone please suggest if any change is required in the program below. Is there any already known issue for this function. To be specific, discover function is working correctly, issue is mainly observed with connection establishment "BleakClient()" or "client.connect()"
import asyncio
import platform
import sys
from bleak import BleakClient, BleakScanner
from bleak.exc import BleakError
ADDRESS = (
"6E:01:8F:08:37:71"
if platform.system() != "Darwin"
else "B9EA5233-37EF-4DD6-87A8-2A875E821C46"
)
async def main(ble_address: str):
device = await BleakScanner.find_device_by_address(ble_address, timeout=5.0)
if not device:
raise BleakError(f"A device with address {ble_address} could not be found.")
async with BleakClient(device) as client:
svcs = await client.get_services()
print("Services:")
for service in svcs:
print(service)
if __name__ == "__main__":
asyncio.run(main(sys.argv[1] if len(sys.argv) == 2 else ADDRESS))
I am getting below error
async with BleakClient(device) as client:
File "C:\Python39\lib\site-packages\bleak\backends\client.py", line 61,
in __aenter__
await self.connect()
File "C:\Python39\lib\site-packages\bleak\backends\winrt\client.py",
line 249, in connect
await asyncio.wait_for(event.wait(), timeout=timeout)
File "C:\Python39\lib\asyncio\tasks.py", line 494, in wait_for
raise exceptions.TimeoutError() from exc
asyncio.exceptions.TimeoutError
```</div>