I have a ble device which gives battery status as battery service.I need to read battery information from my ble device using C programming language from linux using bluez. I could not see a proper example anywhere for this purpose even though there are examples of classic bluetooth. I am able to detect my ble device using the example code here. How can I connect to my device ( I have the device address after scanning ) ? How can I request the battery service to my device ( I know the service handle for my battery service ) ? Expecting Example code.
2 Answers
I don't have example code but I can point you to the source code similar to what you did. To read the battery service you'll need the following steps:-
- As you mentioned, scan and detect the address that you are looking for. This can also be found in hcitool.c, at:-
static void cmd_lescan(int dev_id, int argc, char **argv) {...}
- Once your device is found, connect to the device. The source for this can be found at hcitool.c, at:-
static void cmd_lecc(int dev_id, int argc, char **argv) {...}
- You need to then do a characteristic discovery to look for the characteristic handle since you know the UUID. If your device is using the adopted battery service, then the characteristic you are looking for is 2A19. The code for the characteristic discovery can be found at gatttool.c, at:-
static gboolean characteristics(gpointer user_data) {...}
- Knowing the handle, you then proceed to read the characteristic value. The code for this can be found in gatttool.c, at:-
static void char_read_cb(guint8 status, const guint8 *pdu, guint16 plen, gpointer user_data) {...}
Alternatively, you can just read the characteristic data using the characteristic UUID if you already know it. This can be found in gatttool.c, at:-
static void char_read_by_uuid_cb(guint8 status, const guint8 *pdu, guint16 plen, gpointer user_data)
If you're new to BlueZ, I recommend starting from the BlueZ command line tools. This would help you understand more about what is going on under the hood in the source. To achieve what you want through the command line tools you simply need:-
- hcitool lescan
- gatttool -b 00:11:22:33:44:55 --characteristics
- gatttool -b 00:11:22:33:44:55 --char-read --uuid=0x2A19
Where 00:11:22:33:44:55 should be replaced with the device address with the battery service.
I hope this helps.

- 1
- 1

- 11,789
- 4
- 44
- 72
-
Thanks alot @yousif saeed : Could you tell how can I get the argument gpointer user_data of 'characteristics' function you mentioned above ? – Bill Goldberg Mar 23 '17 at 02:50
Most of the examples would suggest you to extract source code from Bluez to read GATT characteristics. gattlib (C-library) does not require you to do so and is built on top of Bluez.
The library has two examples you could use for reading battery level.
You can use the notification
example that subscribe to battery level notification. Or you could use the read_write
example to read the battery level.

- 2,820
- 24
- 41