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

How to send 20 bytes using sd_ble_gatts_hvx

I've read in several other posts that it's possible to send up to 20 bytes per notification packet using sd_ble_gatts_hvx(). I've also read that we should not edit the file ble_gatts.h or any other header files that have a "ble_" prefix.

In ble_gatts.h, the definition for the ble_gatts_hvx_params_t struct has p_data set to uint8_t, which is equal to 1 byte. Rather than sending 1 byte though, I'm interested in sending 20 bytes, yet I'm not supposed to edit this value. So how do I send more data per packet without editing that value?

From some reading that i've done on other posts, I also see that the max_len attribute can be increased to a value that indicates 20 bytes. That seems like what needs to happen, and I've experimented with changing it. However I'm still limited to a single byte (uint8_t) for the p_data value, so setting max_len to 20 seems to have no impact.

Is there any sample code for calling sd_ble_gatts_hvx() with more than a single byte of data?

  • If you look carefully at the struct again, p_data is actually an uint8_t pointer!

    typedef struct
    {
      uint16_t          handle;             /**< Characteristic Value Handle. */
      uint8_t           type;               /**< Indication or Notification, see @ref BLE_GATT_HVX_TYPES. */
      uint16_t          offset;             /**< Offset within the attribute value. */
      uint16_t*         p_len;              /**< Length in bytes to be written, length in bytes written after successful return. */
      uint8_t*          p_data;             /**< Actual data content, use NULL to use the current attribute value. */
    } ble_gatts_hvx_params_t;
    

    Sending 20 bytes can easily be done, like this:

    ble_gatts_hvx_params_t hvx_params;
    memset(&hvx_params, 0, sizeof(hvx_params));
    uint16_t len = 20;
    uint8_t data[20] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; // 20 bytes of data
    
    hvx_params.handle = value_handle;
    hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
    hvx_params.offset = 0;
    hvx_params.p_len  = &len; // remember that this is also a pointer!
    hvx_params.p_data = data;
    
    err_code = sd_ble_gatts_hvx(conn_handle, &hvx_params);
    
  • Thanks Vebjorn, that resolved the question.

  • This did not work for me. I receive a gatt connection error as soon as I increase len to anything more than 1

Related