writing to a ble characteristic

Hello everyone,

I have two devices: a Thingy53 acting as the peripheral and an nRF5340 acting as the central. I have a characteristic in a service that I want to modify using the central device. After discovering the characteristic and obtaining its handle, I attempted to write to this characteristic. However, I encountered the following error:

[00:00:06.088,745] <err> os: ***** USAGE FAULT *****
[00:00:06.088,775] <err> os:   Illegal use of the EPSR
[00:00:06.088,775] <err> os: r0/a1:  0x20001420  r1/a2:  0x00000003  r2/a3:  0x20002a48
[00:00:06.088,806] <err> os: r3/a4:  0x00000000 r12/ip:  0x00000001 r14/lr:  0x0001ca51
[00:00:06.088,806] <err> os:  xpsr:  0x60000000
[00:00:06.088,836] <err> os: Faulting instruction address (r15/pc): 0x00000000
[00:00:06.088,867] <err> os: >>> ZEPHYR FATAL ERROR 35: Unknown error on CPU 0
[00:00:06.088,897] <err> os: Current thread: 0x20001210 (BT RX)

the funtion i am using to write to the characteristic is :

void write_characteristic(struct bt_conn *conn) {

static uint8_t value_to_write[] = { 0x01, 0x02, 0x03 }; 

struct bt_gatt_write_params write_params = {

.handle = characteristic_handle,

.data = value_to_write,

.length = sizeof(value_to_write),

.func = write_func,

.offset = 0, };

int err = bt_gatt_write(conn, &write_params);

if (err) {

printk("Write failed (err %d)\n", err); }

else{ printk("Write successful\n");

}

}

Parents
  • Hello,

    Please add the 'static' keyword to your 'write_params' variable definition and see if it helps. From the API documentation:

    Best regards,

    Vidar

  • when i add static keyword to 'write_params' the handle member doesn't accept non constant value

  • Yes i think that i'm writing to the correcte handler because the characteristic that i want to modify is BT_UUID_LBS_DATE and i store it's handler value here :

    else if (!bt_uuid_cmp(discover_params.uuid,
                    BT_UUID_LBS_DATE)) {

            //write_characteristic(default_conn)
            characteristic_handle_date = attr->handle;
            printk("MYSENSOR characteristic discovered\n");
            memcpy(&discover_uuid, BT_UUID_LBS_MYSENSOR, sizeof(discover_uuid));
            discover_params.uuid = &discover_uuid.uuid;
            discover_params.start_handle = attr->handle - 3;
            discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
            err = bt_gatt_discover(conn, &discover_params);
            if (err) {
                printk("Discover failed (err %d)\n", err);
            }
            //return BT_GATT_ITER_STOP;
        }
  • and this is the declaration of my service characteristic :

    BT_GATT_CHARACTERISTIC(BT_UUID_LBS_DATE, BT_GATT_CHRC_WRITE, BT_GATT_PERM_WRITE, NULL,
                       write_date, NULL),
  • The code snippets do not show where the 'characteristic_handle' is assigned. Do you see the same if you increment 'characteristic_handle' by 1? Maybe the handle is pointing to the characteristic containing the UUID, and not the value handle for the characteristic.

  • characteristic_handle is same as characteristic_handle_date because i have changed its name, now i have incremented its value by 1 and it works, thank you so much for your help.

    But can you please explain to me why when the value is incremented the write operation works ?

  • Hi Youssef, do you know how to obtain the handle value of the attribute or the RX or TX characteristics. Is it initialized with how you defined your sevice or it is related to the UUID? For right now I am kind of stucking in obtaining the handle value of the RX characteristic which I want to write into.

Reply
  • Hi Youssef, do you know how to obtain the handle value of the attribute or the RX or TX characteristics. Is it initialized with how you defined your sevice or it is related to the UUID? For right now I am kind of stucking in obtaining the handle value of the RX characteristic which I want to write into.

Children
No Data