labapart / gattlib

Library to access GATT information from BLE (Bluetooth Low Energy) devices
http://labapart.com/
454 stars 160 forks source link

Issues with gattlib, nordic_uart and bluez 5.50 on RPi4 #142

Open krimp opened 4 years ago

krimp commented 4 years ago

I have problem with running the gattlib nordic_uart.c example on an RPi4B+ using Bluez 5.50. The gattlib is cloned and compiled on the RPi according to the gattlib installation instructions.

I am running towards a nrf pheripheral device based on the nordic-uart ble stack, modified to automatically streams its data over ble, once connected.

Using the gatttool everything works as it should:

sudo gatttool -b E7:76:98:F8:7A:70 -I -t random
[E7:76:98:F8:7A:70][LE]> connect
Attempting to connect to E7:76:98:F8:7A:70
Connection successful
[E7:76:98:F8:7A:70][LE]> characteristics
handle: 0x0002, char properties: 0x0a, char value handle: 0x0003, uuid: 00002a00-0000-1000-8000-00805f9b34fb
handle: 0x0004, char properties: 0x02, char value handle: 0x0005, uuid: 00002a01-0000-1000-8000-00805f9b34fb
handle: 0x0006, char properties: 0x02, char value handle: 0x0007, uuid: 00002a04-0000-1000-8000-00805f9b34fb
handle: 0x0008, char properties: 0x02, char value handle: 0x0009, uuid: 00002aa6-0000-1000-8000-00805f9b34fb
handle: 0x000c, char properties: 0x0c, char value handle: 0x000d, uuid: 6e400002-b5a3-f393-e0a9-e50e24dcca9e
handle: 0x000e, char properties: 0x10, char value handle: 0x000f, uuid: 6e400003-b5a3-f393-e0a9-e50e24dcca9e
[E7:76:98:F8:7A:70][LE]> char-desc
handle: 0x0001, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0002, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0003, uuid: 00002a00-0000-1000-8000-00805f9b34fb
handle: 0x0004, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0005, uuid: 00002a01-0000-1000-8000-00805f9b34fb
handle: 0x0006, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0007, uuid: 00002a04-0000-1000-8000-00805f9b34fb
handle: 0x0008, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0009, uuid: 00002aa6-0000-1000-8000-00805f9b34fb
handle: 0x000a, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x000b, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x000c, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x000d, uuid: 6e400002-b5a3-f393-e0a9-e50e24dcca9e
handle: 0x000e, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x000f, uuid: 6e400003-b5a3-f393-e0a9-e50e24dcca9e
handle: 0x0010, uuid: 00002902-0000-1000-8000-00805f9b34fb

Next, using the gatttool, I enables the receive notification by sending the following to my nordic device:

  char-write-req 0x10 0100

and where data is received as expected:

[E7:76:98:F8:7A:70][LE]> char-write-req 0x10 0100
Characteristic value was written successfully
Notification handle = 0x000f value: 54 2c 31 35 2e 39 38 2c 32 31 2e 38 37 2c 32 30 2e 36 35 2c
Notification handle = 0x000f value: 54 2c 31 35 2e 34 38 2c 32 31 2e 38 37 2c 32 30 2e 36 35 2c

To debug, I modified the gattlib nordic_uart.c example to print the discovered characteristics. The gattlib nordic_uart.c example discovers two characteristics:

pi@RPi4Bplus:~/gattlib/build/examples/nordic_uart $ sudo ./nordic_uart E7:76:98:F8:7A:70                              characteristic count: 2
handle = 0x000e, char properties = 0xf0, char value handle = 0x000e, uuid = 6e400003-b5a3-f393-e0a9-e50e24dcca9e
handle = 0x000c, char properties = 0x3c, char value handle = 0x000c, uuid = 6e400002-b5a3-f393-e0a9-e50e24dcca9e

Next, the gattlib nordic_uart example enables notification of rx by


// Enable Status Notification
uint16_t enable_notification = 0x0001;
ret= gattlib_write_char_by_handle(m_connection, rx_handle+1 , &enable_notification, sizeof(enable_notification)); 
if (ret) {
    fprintf(stderr, "Failed to write to handle. ErrorNo: %i \n", ret);
    return 1;
} 

In my case gattlib_write_char_by_handle(...) returns the error:

Failed to write to handle. ErrorNo: 2

ErrorNo 2 is : GATTLIB_NOT_FOUND, which is somehow as expected, since the handle rx_handle+1 probably does not exist. Looking at the output from the gatttools char-desc, I guess rx_handle+1 is ment to be the handle following the rx uuid 6e400003-... (handle 0x0010 to be specific).

Since the gatttool needed an input to handle 0x10 as 0100 and not 0001, I have also tried with

uint16_t enable_notification = 0x0100;

but with the same result.

I am new to the use of the gattlib, so if I have completely missed something, any help pointing me in the right direction of how to use gattlib to enable the rx notification of my datastream from the nordic pheripheral, would be very much appreciated.

mehmoodn commented 3 years ago

I'm a little late in commenting on this. Has this issue been resolved? I am having a lot of challenges receiving any RX notifications. Are there any other workarounds to receive UART data? Many thanks!