What is the correct way to read advertisement packets from a BLE sensor using bluez 5.43 and DBus

3.1k Views Asked by At

I am trying to implement a C++ code (using bluez 5.43 and dbus) to read advertisement packets from a BLE sensor. As per the bluez DBus docs, there is a StartDiscovery API that can be used to scan for nearby devices. However, I am unable to find any APIs to store/parse the advertisement packets from nearby BLE devices. The advertising-api.txt lists registeradvertisement API but as per my understanding, it can be used only for creating advertisement packets and not reading from an external device (or am I wrong?) Can someone please guide me on the correct way to get advertisement packets from nearby BLE devices using bluez and DBus?

2

There are 2 best solutions below

0
On

The behavior you described in one your last comment is the right one (the advertising data is not beeing updated) : if I am correct a BLE device is not supposed to be up all the time, it can sleep or turn to low-power etc.

In this context, it is not weird that the data is in some way "cached". From my experience, when you perform a scan and discover a device (even if you don't Connect to it), the device information will be stored for some time.

In your case, that's problematic because you are passing data through the advertising. However there is a way to force bluez to remove all it's cached data about a device : the adapter-api provides a RemoveDevice(object device) method. It takes the object path (eg "/org/bluez/hci0/dev_AA_BB_AA_BB_AA") as argument.

If you're looking for DBus bindings in C, I suggest GLib GDBus (you will find links at the bottom of this tutorial on freedesktop website : https://dbus.freedesktop.org/doc/dbus-tutorial.html).

If you are familiar with bluetoothctl (a tool to interact with bluez using commands), it was developped by the bluez guys using Glib GDbus and you can find the source code here (look at the bottom to find the command list) : https://git.kernel.org/cgit/bluetooth/bluez.git/tree/client/main.c

There are more straigthforward ways to use GDBus with bluez but bluetoothctl source code is a start and you'll find examples for pretty much anything that is possible to do with bluez =)

0
On

Thanks for the suggestions everyone. I was finally able to get the manufacturer data by using Intel's tinyb library. It has an enable_manufacturer_data_notifications API which enables you to be notified whenever the manufacturer data changes.