I'm working with (everybody's favourite) BlueZ 5.40 compiled and run with experimental features and I need to scan for LE devices, pair and connect to one and read/write a characteristic via the D-Bus API. I have studied sources of hcitool, gatttool and bluetootctl and made a basic application using GDBus. However, there are several problems with it.
- Scanning does not add /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX obj. path to org.bluez bus (checked using d-feet). This is not that surprising considered it's not D-Bus based, but I when I use StartDiscovery it does not detect my device at all. Neither does bluetoothctl.
After that I used gatttool and simple-agent as a workaround to create the object path and then connect using my program but I encountered another problem:
- When I try to read a characteristic I get a "The connection is closed (18)" error. I suspect this has nothing to do with the connection between bluetooth devices and it talks about D-Bus itself because when I try to set scanning filter for LE devices only, using SetDiscoveryFilter, I receive the same error.
Whenever I use only Connect and Disconnect functions everything seems to be working fine, but uses for applications like that are... limited. So my questions are:
How to scan for LE devices using GDBus? If that is not possible, how to add a device manually or persuade bluetoothd to do it for me?
How to read a characteristic properly?
The code is rather lengthy even after shortening, so I put it on pastebin:
http://pastebin.com/YNLMF0qC.
Compile with g++ -std=c++11 $(pkg-config --cflags glib-2.0 gobject-2.0 gio-2.0) ./main.cpp $(pkg-config --libs glib-2.0 gobject-2.0 gio-2.0 bluez)