OpenBluetoothToolbox / SimpleBLE

SimpleBLE - the all-in-one Bluetooth library for MacOS, iOS, Windows, Linux and Android.
https://www.simpleble.org
Other
660 stars 111 forks source link

Missed advertisements #218

Closed duichan closed 1 year ago

duichan commented 1 year ago

I am evaluating simpleBLE on a Raspberry Pi 4B (latest Bullseye 32bit OS) with a RuuviTag BLE environment sensor. I know that the Ruuvi is broadcasting a manufacturer-specific data advertisement approximately every 1.25 seconds, but using simpleBLE I am only picking up about every fourth one. The program I am using is your simplebluez/example_scan (except with the scan duration on line 71 increased from 3 s to 100 s). Am I doing something wrong or is this an inherent limitation of simpleBLE? Incidentally if I use hcidump from the command line then I can indeed see all the advertisements. Thank you.

eriklins commented 1 year ago

In general a BLE advertisement is just a broadcast and can be missed by a scanning central for various reasons. There could be collisions with other BLE devices advertising or other devices utilizing the same frequency band (e.g. WiFi). Another limitation comes from the central device not scanning 100% of the time but repeatedly at a certain "scan interval" and over a "scan window" during that interval. The remaining time of the scan interval would be used/reserved for exercising BLE connections. Not sure about default values for scan interval/window on the RasPi. I found that on Windows the scan interval is around 118msec and the window is just 18msec, so just 1/7 of the time and hence quite some advertisements can get missed. Not sure if it would be possible to set these parameters on Linux with something like hcitool or btmgmt before starting the scan example. It's not (yet...) possible to set these from within SimpleBLE.

duichan commented 1 year ago

Wow, thanks for your very quick response. The curious thing is that hcidump on the same system seems to pick up nearly everything, which makes me think that the scan interval/window is somehow different with simpleBLE.

eriklins commented 1 year ago

I think hcidump - as the name implies - would be more like a sniffer and just dump anything coming in over the BT hci interface without taking a scan interval or window into account. Maybe you can try "hcitool lescan" which might operate on a higher application level and hence use scan interval/window. But no idea whether it would adjust scan interval/window to certain values.

duichan commented 1 year ago

OK thanks.

kdewald commented 1 year ago

Hey @duichan, sorry for my late response.

For the missed packets issue that you see, the main cause I've seen is compiling the SimpleBLE in debug mode (which is the default), which severely affects performance. You need to use the -DCMAKE_BUILD_TYPE=Release flag to build with all optimizations on.

duichan commented 1 year ago

Good call! Thank you.