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

nRF52840 - Writting more than one byte on characteristic

Hi everyone,

I've written a custom BLE service with two characteristics. I want to write more that one bytes to one of the characteristics and then to store the written value to a variable.

By default the data member of ble_gatts_evt_write_t is size 1. I am not sure if I have to modify the size of data member..

typedef struct
{
  uint16_t                    handle;             /**< Attribute Handle. */
  ble_uuid_t                  uuid;               /**< Attribute UUID. */
  uint8_t                     op;                 /**< Type of write operation, see @ref BLE_GATTS_OPS. */
  uint8_t                     auth_required;      /**< Writing operation deferred due to authorization requirement. Application may use @ref sd_ble_gatts_value_set to finalize the writing operation. */
  uint16_t                    offset;             /**< Offset for the write operation. */
  uint16_t                    len;                /**< Length of the received data. */
  uint8_t                     data[1];            /**< Received data. @note This is a variable length array. The size of 1 indicated is only a placeholder for compilation.
                                                       See @ref sd_ble_evt_get for more information on how to use event structures with variable length array members. */
} ble_gatts_evt_write_t;

For example if I write two bytes to the characteristic, is this approach going to work?

static void on_write(ble_cus_t *p_cus, ble_evt_t const *p_ble_evt) {

  ble_gatts_evt_write_t const *p_evt_write = &p_ble_evt->evt.gatts_evt.params.write; 
  static ble_cus_evt_t evt;

if ((p_evt_write->handle == p_cus->sampling_rate_handles.value_handle) && (p_evt_write->len == 2)) {
    if (p_cus->evt_handler != NULL) { 

 uint8_t msb;
 uint8_t lsb;

 msb = p_evt_write->data[0];
 lsb = p_evt_write->data[1];
 unint16_t data =  (msb << 8) | lsb;
 evt.sampling_rate = data;

    p_cus->evt_handler(p_cus, &evt); // na - Call the application event handler.
    
    }

Thanks in advance

Nick

Related