14

I am trying to write an application which searches Bluetooth devices nearby and communicates with them. My application is going to be written in C++, and intended to work under Linux.

4 years ago, I used BlueZ. But now, as I see, the API has been changed a lot and now it's using D-Bus. I was not experienced with D-Bus. I looked at some tutorials related to client/server model. Now, I'm OK with D-Bus.

But I couldn't find any example which explains how to use BlueZ with D-Bus. I need some guidance for using BlueZ and D-Bus together.

Are there any tutorial or sample for working with BlueZ via D-Bus in C or C++?

(note: already googled it)

jnbrq -Canberk Sönmez
  • 1,790
  • 1
  • 17
  • 29
  • 4
    FWIW, when I was looking I didn't find any good tutorials or samples either. Ended up just looking at the bluez code, particularly the client/tool code, and working it out from there. [Here](https://bitbucket.org/kaylum/bluez-rssi-example/src) is a simple sample I wrote to discover devices and get the RSSI of a device. Was for a different question. And it's for bluez4 and not bluez5 (as the OP asked for that). But the DBUS aspects are the same and you just need to call the bluez5 APIs instead. Providing in case it is helpful for you. – kaylum Dec 03 '15 at 21:49
  • 2
    @Kaylum Thank you for your code example. You may want to post it as an answer. – jnbrq -Canberk Sönmez Dec 03 '15 at 21:59
  • I would like to know status of your application @jnbrq-CanberkSönmez. I am planning to start bluez. Should i use bluez api or dbus? – abhiarora Jan 06 '17 at 17:15
  • 1
    How to solve a problem is 90% finding accurate timely in-sync with the api version in question documentation. As many software authors and companies are now relying more and more on "crowdsourcing" it is getting harder and harder to get good, hopefully curated, information. – peterk Feb 06 '18 at 08:05

1 Answers1

27

You might want to check out the main.c file in the client folder of the most recent Bluez source code. It's the source code for the bluetoothctl tool. Run it too. The source code shows exactly how they use GDBus, including proxies, agents, calling methods like described in the API documentation (/doc folder) and all that. It's in C and uses the high level API.

I suggest you step through the code because it took me 2 weeks endlessly trying to understand Bluez in C and the fact that there's no documentation, but when I read that main.c file I was ready in a day. Read up on proper DBus API documentation and more importantly the concepts. Some documents that helped me:

The gdbus tool: https://developer.gnome.org/gio/stable/gdbus.html

These contain all the calls to gdbus and objects in the main.c file and explain them very well. https://developer.gnome.org/gio/stable/gdbus-convenience.html

D-Feet, an invaluable tool to inspecting and learning about Dbus on your system. Try checking out the /bluez bus. https://wiki.gnome.org/action/show/Apps/DFeet?action=show&redirect=DFeet

or

sudo apt-get install d-feet

Not much of a tutorial, but worth a read to understand some concepts, as the bluetoothctl tool fits into what they're trying to say here. http://dbus.freedesktop.org/doc/dbus-tutorial.html

The bluetoothctl creates an interactive shell though, so it might not be wise to waste time trying to fit in your code, but just pick what you need from it.

Zimano
  • 1,870
  • 2
  • 23
  • 41
  • 1
    i'm looking into the `bluetoothctl` code and stuck at when characteristics are being retrieved. Guessing it's a watch on the client but i'm lost in code... i'll comment if i find it, and if @Zimano has not replied yet. – nymo Aug 23 '17 at 15:20
  • 3
    it was just a matter of finding the path of the device, then introspecting it for service nodes (e.g. `service001f` for UART), then tacking that on to the path and search the latter on the object manager with `g_dbus_object_manager_get_object (manager, path)`. Of course, the path to the service could also, more easily, be found with Bluez's client: `bluetoothctl`. – nymo Aug 24 '17 at 14:51
  • @nymo Awesome! Glad you solved it. – Zimano Aug 24 '17 at 14:54