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

How to prevent a client from writing an invalid value into a GATT characteristic?

Hello,

What is the best way to prevent a client from writing wrong values into a characteristic? For example, I created a writable uint8 characteristic on a nRF52832 that affects the duty cycle of a PWM in %. The Client that writes to the characteristic and therefore controls the duty cycle on the nRF52832 is at least a Bluetooth 4.2 smartphone. The valid values of the uint8 characteristic are between 5 to 100. I know I can specify the limits in the Smartphone App, too. But I would prefer to prevent clients from writing wrong values (e.g. 2, 123, ...) into the characteristic. How can I limit the valid range? I know there is a Valid Range Descriptor but as far as I understand this is only to inform the client about the valid range.

I found the following possibility to solve my problem but I'm wondering if this is still the recommended solution. If authorization is still the best solution which error code in response to a write request is recommended if a client tries to write an invalid characteristic value?

https://devzone.nordicsemi.com/f/nordic-q-a/4053/ble_gatts_evt_write---value-limit-control

Used SoC: nRF52832 

Used SD: S132 V6.1

Thanks.

Best regards,

Christian

Parents Reply
  • Hi Andy,

    Thank you very much for your fast response.

    Do you know which error code I have to choose in response to a write request? In the core spec 5.0 Vol 3, Part G, 4.9.3 I found:

    "If the Characteristic Value that is written is the wrong size, or has an invalid value as defined by the profile, then the value shall not be written and an Error Response shall be sent with the Error Code set to Application Error by the server."

    Does this mean I have to use an error code between BLE_GATT_STATUS_ATTERR_APP_BEGIN and BLE_GATT_STATUS_ATTERR_APP_END if a client tries to write an invalid value?

Children
Related