IanHarvey / bluepy

Python interface to Bluetooth LE on Linux
Other
1.58k stars 490 forks source link

Code pattern to listen to multiple updates to Manufacturer Advertising BLE packets? #444

Open millercentral opened 3 years ago

millercentral commented 3 years ago

I have a device that broadcasts status using BLE Advertising broadcasts that updates a "Manufacturer" (0xFF) packet type twice over a couple seconds to provide data from two different sensors. The data of the given sensors themselves do not change that frequently. No connection to the device is needed/supported.

I want to build an integration for Home Assistant for this device and leverage bluepy to scan for the BLE broadcast messages, but I'm not sure what the correct coding pattern would be to scan long enough to receive both 0xFF advertisement updates? Would someone with more experience here be able to help with some simple sample code for this scenario?

Here is an example of the BLE broadcasts (as seen by bluetoothctl):

[CHG] Device D0:CF:5E:5C:9B:75 RSSI: -71
[CHG] Device D0:CF:5E:5C:9B:75 ManufacturerData Key: 0xffff
[CHG] Device D0:CF:5E:5C:9B:75 ManufacturerData Value:
  4c 5e 4c 5e 02 e3 51 50 61 63 69 66 69 63 20 43  L^L^..QPacific C
  72 65 73 74 20 50 61 6c 65 20 20                 rest Pale
.
.
.
[CHG] Device D0:CF:5E:5C:9B:75 RSSI: -56
[CHG] Device D0:CF:5E:5C:9B:75 ManufacturerData Key: 0xffff
[CHG] Device D0:CF:5E:5C:9B:75 ManufacturerData Value:
  4c 5e 4c 5e 00 00 41 7b 6e 6f 6e 65 7d 20 20 20  L^L^..A{none}
  20 20 20 20 20 20 20 20 20 20 20
doug-holtsinger commented 2 years ago

I've been using the getValueText(255) method from the ScanEntry class to obtain the manufacturer's data from a passive scan. I have a hacked-up example of what I am doing here:
https://github.com/doug-holtsinger/WirelessSensor/blob/master/Compute/src/AHRS-VISUAL/ahrs-ble.py My application allows for the possibility of dropped packets though.