hbldh / bleak

A cross platform Bluetooth Low Energy Client for Python using asyncio
MIT License
1.56k stars 275 forks source link

Multiple BLE devices packet loss. #1502

Open 13Pixel opened 3 months ago

13Pixel commented 3 months ago

Description

Maybe It's more of BLE issue and not this library...

I have BLE devices(ESP32) which sends data(306bytes) with notify every 40ms. MTU size on device is set to 512. When I try to send data with one device - it works as expected - I receive all packets without data loss.

The problem appears when I try to receive data from 2 or more devices.

I run two instances of same python(with bleak) code(code is very simple - when computer gets notification that there is data - he reads it and adds data to queue, after some time - notifications stops and program prints all data in queue).

Issue is that after I start reading data from second device, I start to miss packets from first device. Data loss quite high - 40%.

What I Did

I tried to send less data (100bytes every 40ms) and I get same issue.

Tried send more data from one device (450bytes every 16ms) And data loss was 0 when I reading from only one device, so I confirmed that problem isn't with throughput.

Maybe someone had same issues and know how to fix this or have ideas what I could try, how to identify where is the problem?

Thanks you.

dlech commented 3 months ago

I would suggest logging Bluetooth traffic using a sniffer to see what is going on over the airwaves. This will let you see empty packets that you can't see by logging HCI packets on the host computer.

My experience (with writing to peripherals in addition to getting notifications) is that when multiple peripherals are connected to a single adapter on the central that throughput can take a big hit because of the connection interval. BlueZ has a default connection interval of 50ms. It seems like some adapters are better than others of turning off the radio early during a connection interval. But even the "good" adapters sometimes have issues.