bt_gatt_read

Hello, I have a question about the bt_gatt_read function. I want to read the attribute value through the UUID of the attribute. I am using NCS V1.4.2.Can you please tell me how to use it correctly in the code? 

I can use this function to read values through the handle.

struct bt_gatt_read_params params1 = {
    .func = read_process1,
    .handle_count = 1,
   
.offset = 0;
    .handle
= 44,
};


I see bt_gatt_read_params handle (If equals to 0 by_uuid is used for Read Using Characteristic UUID.)

I know the UUID of the attribute (0xFFF9), but I cannot obtain the attribute value using this method. Why? Did I make a mistake in writing somewhere? How should the correct code be written?

params1.handle_count = 0;
memcpy(&uuid_16, BT_UUID_DECLARE_16(0xFFF9), sizeof(uuid_16));
params1.by_uuid.uuid = &uuid_16.uuid;
params1.by_uuid.start_handle = 0x0001;
params1.by_uuid.end_handle = 0xffff;
bt_gatt_read(conn,&params1);
  • Hi,

    First of all, if you are in development and still use nRF Connect SDK v1.4.2, then I kindly suggest upgrading to a newer version. The latest version to date is nRF Connect SDK v2.6.0.

    Regardless of nRF Connect SDK version, it looks like none of the samples show usage of bt_gatt_read() with handle_count = 0.

    For your implementation, please note that the struct bt_gatt_read_params must remain valid until the callback function is called. In the SDK samples, this is solved through declaring the struct static, e.g.:

    static struct bt_gatt_read_params params1 = {

    The call is expected to most likely not work as intended, if the struct goes out of scope before the callback funciton is called. This is because the struct contents may be garbage values at that point in time. Similarly, uuid_16 (pointed to from within the struct) must also still be valid, so the same applies there (as well as any other structures pointed to within that structure.)

    Regards,
    Terje

Related