BlueZ 5.30: D-Bus GATT API - Simply Discover and Connect to a BLE device in C
Asked Answered
P

2

6

With the last release of BlueZ (5.30) the highlight was the completion of the GATT D-Bus apis. My goal is to programmatically (in C), as a BLE client:

  1. scan for ble devices (which I can do with the hci layer)
  2. Connect to an advertising BLE device
  3. Get the UUIDs
  4. Execute Read and Write to handles

The BlueZ community is strongly suggesting to use the GATT-Dbus api to accomplish this. After multiple searches and head scratching I was not successful to find a proper way or example that would perform this through GATT-DBUs api. It seems more complicate than just use directly the GATT layer. Unfortunately BlueZ removed direct access to make calls to gatt.

I'm very close to just pull the GATT source files out from Bluez, compile it as it's own independent library and directly use the GATT layer and calls to connect and execute reads/writes to ble device server. I know it is not the suggested way compared to the DBUS-GATT api but I'm out of options.

If anybody has any input on this or suggestion (with some sample code) please advise.

thank you in advance!

Petromilli answered 12/6, 2015 at 16:56 Comment(0)
P
7

The best way to start with DBUS GATT API is to hace a look at the source code here: http://git.kernel.org/cgit/bluetooth/bluez.git/tree/

Under client folder you can find a full sample of how to use the DBUS GATT API. Actually that is the source of bluetoothctl tool.

Note that DBUS GATT API is still experimental but you can enable it running bluetoothd service with -E flag.

Piracy answered 15/6, 2015 at 15:49 Comment(4)
Experimental agreed that's why I'm debating the route of extracting the GATT source code and calling that layer directly instead of going through DBUS route.Petromilli
In that case all you need is here: git.kernel.org/cgit/bluetooth/bluez.git/tree/attribPiracy
Which version of BlueZ did you extract it from?Petromilli
We asked on the BlueZ Mailinglist about bugs in the /attrib Code and they pointed us to using btgatt-client which is utilizing code from src/shared. (If you don't want to/can't use the dbus API). The posting I'm referring to: marc.info/?l=linux-bluetooth&m=143798563718675Selectman
R
3

I had a similar issue which is to interact with a BLE device with a GATT C/C++ API.

In my case I had to use Bluez v4.101 - the latest Bluez version available in Ubuntu 14.04 LTS (read my story). In Bluez v4.x, there is no DBus API.
And actually, I have recently worked with Bluez v5 DBus API and I discovered if you do not use at least Bluez v5.39 (from April 2016) it is likely you will have issue with the DBus API. And as said before the DBus API is still experimental.

So to solve my Bluez v4.x dependency I had to pull the GATT source files out from Bluez and create my own library 'gattlib'. The project is Open-Source to encourage feedback and contribution. I am using this library in my own GATT client project. I wrote few examples to help people to start using it see the folder /examples of the project.

EDIT March 2017: I have just added DBus support to gattlib (https://github.com/labapart/gattlib/). From Bluez v5.42, gattlib automatically uses DBus API (DBus gattlib backend can also be forced when building the library to use it before v5.42).

Robet answered 28/4, 2016 at 15:55 Comment(8)
Could you describe the issue you had with pre-5.39 release of BlueZ 5? Ubuntu 16.04 LTS happens to ship with 5.37...Geometrid
Are you able to connect to multiple devices simultaneouslySideway
"I have just added DBus support to gattlib" That's great.Sideway
@ArtoBendiken: Actually, the issue pre-5.39 is only related to Bluez DBus GATT Server support. It should not affect DBus GATT client.Robet
@OlivierM: Are you able to do everything using DBUS?Sideway
Can you answer this question: #43476251Sideway
@Sideway I replied your other post.Robet
any help how can I use 'indicate' service?, I could make notify work , how can i get notification when value received by client?Janeanjaneczka

© 2022 - 2024 — McMap. All rights reserved.