C / C ++ BLE Read / Write Example with Bluez

I am starting to create a simple BLE network with a microcontroller and raspberry pi (tardis BLE dongle). As a starting point, I'm looking for a simple C or C ++ example to read / write a BLE device, similar to what I can do on the command line. The examples I have found so far are quite complex. As a BLE beginner, I need some very simple examples to build before moving forward with a more complex design. I'm fine with hard-coded BLE device name, as in the gatttool example below.

Here's how I use Bluez command line options right now.

From the command line, I can use:

$ sudo hcitool lescan LE Scan ... BB:A0:50:02:18:07 MyDevice 

Next, I can connect to the device on the command line using gatttool:

 $ sudo gatttol -b BB:A0:50:02:18:07 -I [BB:A0:50:02:18:07][LE]> connect Attempting to connect to BB:A0:50:02:18:07 Connection successful 

Finally, I can read and write using appropriate descriptors

 [BB:A0:50:02:18:07][LE]> char-write-req 000f 0100 Characteristic value was written successfully [BB:A0:50:02:18:07][LE]> char-write-cmd 0011 4C467A 

Some sites that I used for initial research and to get started:
http://people.csail.mit.edu/albert/bluez-intro/c404.html
https://github.com/carsonmcdonald/bluez-experiments/blob/master/experiments/scantest.c

+9
c ++ c bluetooth raspberry-pi bluez
source share
2 answers

Something simple? What is the short line? I personally find the gatttool code itself simple enough to follow and retrieve for your purposes (I did this recently). But if this really is not right for you, then another option is libgatt . This is essentially the same code that gatttool uses, but is more convenient in the public library API. Take a look at gatt.h which has a connection / read / write, etc. Hope this should be taken for granted how to use this.

+4
source share

Bluez (Linux's official Bluetooth stack) has switched to DBUS for its API. If in the past it was customary to β€œfork out” bluez code to access BLE support on Linux, now the approach is to use DBUS. The Bluez DBUS API is described here: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc.

Either you speak directly with DBUS for your Bluetooth C / C ++ application, or you use the GATT library as an assistant. The second is probably the best approach for beginners (as well as for non-beginners who would rather leave their Bluez program independent, for example, to support other Bluez pre-DBUS operating systems or APIs or to make their sources more readable).

One of these GATT libraries that supports the modern D-BUS API is gattlib (note: I am the author of this library). Here is a simple example based on this library for reading / writing a BLE device: https://github.com/labapart/gattlib/blob/master/examples/read_write/read_write.c

0
source share

All Articles