43

I'm trying to communicate with a BLE module through a Linux machine (the module is running a heart rate profile). So far, I've been able to do everything I need except listening for Notifications and indications (e.g. listening for the Heart Rate Measurement Notification). I'm using kernel version 3.5 and bluez-5.3.

Succcessful commands used so far:

hcitool lescan
hcitool lecc
gatttool -b <Mac Address> --primary
gatttool -b <MAC Address> --characteristics
gatttool -b <MAC Address> --char-read
gatttool -b <MAC Address> --char-desc
gatttool -b <MAC Address> --interactive

Failed commands:

gatttool -b <MAC Address> --listen

Any help is greatly appreciated.

Youssif Saeed
  • 8,246
  • 4
  • 39
  • 62

4 Answers4

50

Try this...

Run gatttool -b <MAC Address> --interactive like you did before. You'll get a prompt and then you type connect. You should see a CON in the prompt indicating that you've connected to the device. Then type char-read-uuid 2902. You should get a list of all CCC (Client Characteristic Configuration) attributes on the device. You can try setting them all to 0100 to get notifications, 0200 for indications, 0300 for both, or 0000 for everything off. Type help to see all the commands and their arguments.

EDIT:

The use of the --listen argument requires you to couple it with other commands to turn on the notifications and/or indications. So here's an example that works in Bluez 4.101:

gatttool -b <MAC Address> --char-write-req --handle=0x0031 --value=0100 --listen

Obviously you need to change the handle to the handle of the CCC that you want to turn on notifications for. However, I still find it way easier to just use the interactive mode.

Tim Tisdall
  • 8,670
  • 3
  • 41
  • 70
  • BTW, the standard says that the CCC values should be retained between connections, but I've used some bluetooth dongles that end up resetting everything between connections. – Tim Tisdall Apr 01 '13 at 13:03
  • Yes currently everything is resetting for me between connections. However, it's not a problem for me as I can always reset the values to get the notifications. Thanks. – Youssif Saeed Apr 02 '13 at 14:30
  • @TimTisdall, Thanks. I am trying something similar with a proximity fob. I have been able to write the CCC and read it to confirm, but I am still not seeing notifications using gatttool -b -t random -I --listen. Do you have any other suggestions? Or am I misunderstanding how it should work? – AgDude Jun 16 '13 at 22:59
  • @AgDude some devices require you to set a few variables to get a notification to work. For example, you may have to turn the notifications on in the CCC and then turn on the particular sensor you want to "listen" to. I'm not sure how your's works, but maybe you have to set the proximity alarm level? – Tim Tisdall Jun 17 '13 at 13:25
  • 5
    @AgDude, For me I had to set the CCC values and listen at the same time. The exact command that I used was gatttool -b --char-write-req --handle=0xXXXX --value=XXXX --listen – Youssif Saeed Jun 21 '13 at 09:39
  • @yousifsaeed Either should work. The `-I` should give you an interactive prompt to send HCI commands. Doing everything on the command line does the same thing, but won't let you do further commands once you run the command. (actually, you may not need `--listen` when adding the `-I`) – Tim Tisdall Jun 24 '13 at 14:14
  • Is it possible to change CCC with gatttool? I do a `char-read-uuid 2902` and it is all zeros, but when I try to use the handles to set the bit it says "Characteristic Write Request failed: Internal application error: I/O". Is it normal, or I need to use another blutooth adaptor dongle? thanks – dashesy Aug 27 '13 at 18:26
  • You should be able to change it with `gatttool`. However, it may be possible to restrict access to it so you may need to pair with the device or something. – Tim Tisdall Aug 27 '13 at 18:38
  • I used `bluetoothctl` to pair (it is BlueZ5), not the values are this: "handle: 0x000f value: 02 00" "handle: 0x002a value: 00 00" but still cannot change CCC – dashesy Aug 27 '13 at 20:40
  • Try changing the security level when you're trying to modify the CCC value. In other words, this is the command that you'll need: #gatttool -l high -b --char-write-req --handle=0x002a --value=02 --listen – Youssif Saeed Sep 10 '13 at 08:14
  • The link returns 403 Forbidden. :( – Fritz Feb 05 '20 at 13:14
  • @Fritz updated the link. https://www.bluetooth.com/specifications/gatt/descriptors/ has all the links to the XML files specifying each. – Tim Tisdall Feb 06 '20 at 20:02
11

Looks like the older version of Bluez (hcitool & gatttool) don't allow you to write to Bluetooth Low Energy devices. I ended up installing a newer version (5.17 as of this writing) in order to enabled notifications, etc.

To get a list of all your handles you can run the following:

char-desc

You can then read from a handle:

char-read-hnd 0x000e

(the above handle is for my nrf51822 battery level)

Where the handle is one from the list you got from char-desc.

Just like Tim said above, you can write to the notification related handle to get indications or notifications. (in my case my device only had notifications)

char-write-req 0x000f 0100

(the above handle is for my nrf51822 battery level notification)

In my case the battery notification shouldn't send anything until the battery level has changed.

I wrote a pretty lengthy blog post on getting setup with Bluez. You can find it here: Get Started with Bluetooth Low Energy Feel free to check it out!

jaredwolff
  • 697
  • 8
  • 12
  • 1
    The older version works fine for writing to BLE devices. However, it's a little finicky/buggy with the formats. Sometimes it'll except "0x01" but other times it only accepts "01". – Tim Tisdall Jun 04 '15 at 14:17
6

Final answer for reading heart rate on Mio Alpha :

gatttool -b xx:xx:xx:xx:xx:xx -t random --char-write-req -a 0x0025 -n 0100 --listen

Characteristic value was written successfully
Notification handle = 0x0024 value: 10 4b 33 03 
Notification handle = 0x0024 value: 10 4b 33 03 
Notification handle = 0x0024 value: 10 4b 33 03 
Notification handle = 0x0024 value: 10 4a 3e 03 
Notification handle = 0x0024 value: 10 4c 28 03 28 03 
Notification handle = 0x0024 value: 10 4c 28 03 
Notification handle = 0x0024 value: 10 4b 33 03 
Notification handle = 0x0024 value: 10 4a 3e 03 3e 03
Fred.Taiwan
  • 61
  • 1
  • 3
2

to retain the CCC value you need to pair the two device. once they are paired you do not need to set the CCC again. on next reconnection it will find the setting , which will be saved in thec FLASH of Key fob. try to configure after pairing via SMP.

RobinSingh
  • 136
  • 1
  • 9
  • 1
    How exactly do you do that? – Tim Tisdall Jun 03 '13 at 21:02
  • 4
    @Tim Tisdall It seems that this can be achieved by increasing the security level of the connection. I tried the following line: #gatttool -l high -b 00:11:22:33:44:55 --primary ,and a notification window poped up asking me to acccept pairing request. – Youssif Saeed Nov 27 '13 at 13:27