Reputation: 388
I try to connect to a BTLE device from a Raspberry Pi, but it fails consistently. Can you help me improve this?
The device is about 3-4 meters away in plain sight from the Raspberry Pi Model B Rev 1.2. Using the simple script below:
import asyncio
from bleak import BleakClient
from loguru import logger
async def just_connect(mac):
async with BleakClient(mac) as client:
connected = await client.is_connected()
async def connect_handler(mac,try_):
try:
await asyncio.wait_for(just_connect(mac), timeout)
logger.info(f"try {try_} succeeded")
except Exception as e:
logger.info(f"try {try_} failed due to {type(e).__name__}: {e}")
mac = "C5:5A:42:A4:3C:80"
tries = 10
timeout = 12
for try_ in range(tries):
asyncio.run(connect_handler(mac,try_))
The logging output reads:
2024-06-02 18:57:41.227 | INFO | __main__:connect:15 - try 0 failed due to BleakDeviceNotFoundError: Device with address C5:5A:42:A4:3C:80 was not found.
2024-06-02 18:57:51.322 | INFO | __main__:connect:15 - try 1 failed due to BleakDeviceNotFoundError: Device with address C5:5A:42:A4:3C:80 was not found.
2024-06-02 18:58:01.446 | INFO | __main__:connect:15 - try 2 failed due to BleakDeviceNotFoundError: Device with address C5:5A:42:A4:3C:80 was not found.
2024-06-02 18:58:12.261 | INFO | __main__:connect:15 - try 3 failed due to BleakDeviceNotFoundError: Device with address C5:5A:42:A4:3C:80 was not found.
2024-06-02 18:58:22.400 | INFO | __main__:connect:15 - try 4 failed due to BleakDeviceNotFoundError: Device with address C5:5A:42:A4:3C:80 was not found.
2024-06-02 18:58:33.276 | INFO | __main__:connect:15 - try 5 failed due to BleakDeviceNotFoundError: Device with address C5:5A:42:A4:3C:80 was not found.
2024-06-02 18:58:43.422 | INFO | __main__:connect:15 - try 6 failed due to BleakDeviceNotFoundError: Device with address C5:5A:42:A4:3C:80 was not found.
2024-06-02 18:58:54.288 | INFO | __main__:connect:15 - try 7 failed due to BleakDeviceNotFoundError: Device with address C5:5A:42:A4:3C:80 was not found.
2024-06-02 18:59:04.437 | INFO | __main__:connect:15 - try 8 failed due to BleakDeviceNotFoundError: Device with address C5:5A:42:A4:3C:80 was not found.
2024-06-02 18:59:15.296 | INFO | __main__:connect:15 - try 9 failed due to BleakDeviceNotFoundError: Device with address C5:5A:42:A4:3C:80 was not found.
What I tried before:
Upvotes: 0
Views: 45