This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Who owns a characteristic's value ?

My system is a BLE peripheral with 2 states. There is a characteristic that represents the state, and it can take 2 values: 0 and 1. You can read the state, write it (force the system to change state), or be notified (be informed when system state changes).

I understood how to set the initial value, and my handler gets called when there is a write. But what happens if I write "0x02" to the characteristic ? my write handler won't do anything as the state is not valid, but the characteristic will take "2" as the value !! How do I prevent that ? In other words, how should I synchronize my internal state and the characteristic's value ?

I tried to use attr_md.vloc = BLE_GATTS_VLOC_USER; and I did attr_char_value.p_value = &my_internal_state but that doesn't seem to help. What should be the correct way ? Thanks

P.S Im using sdk 8.1

Parents
  • Hi

    Maybe you can to a control check of the received value at the write event. You can read the value with e.g. p_ble_evt->evt.gatts_evt.params.write.data. If the value is '0' or '1' everything is good, but if it is something else you can do:

    uint8_t your_state = 0x01;     // Your desired alternate state
    ble_gatts_value_t value;       // Variable to hold desired state value with length and offset information
    value.len = 1;                 // Lenght of your state characteristic
    value.offset = 0;              // Offset of your state characteristic
    value.p_value = &your_state;   // Alternate state
    sd_ble_gatts_value_set("current connection handle", "characteristic handle", &value); // Write new state to characteristic
    

    to overwrite the new value.

  • this doesn't work. I did that in the on_write() method, of my ble service, just after calling my write_handler.

Reply Children
No Data
Related