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

Why can't I decode my received data?

Hello,

I am sending a float in my BLE payload, using nRF Connect I can sniff and see that the value is being sending, it changes every time.

However, in my central when collecting the data, this value seems always to be zero, so I am doing something wrong with the encoding but I cannot point out what. I see on the logs that the connection, discovery, binding goes fine.

Any hint on what I am doing wrong?

The called function to parse the hvx incoming data (central)

static void on_hvx(ble_c_t * p_ble_c, const ble_evt_t * p_ble_evt)
{
    // Check if the event is on the link for this instance
    if (p_ble_c->conn_handle != p_ble_evt->evt.gattc_evt.conn_handle)
    {
        NRF_LOG_DEBUG("received HVX on link 0x%x, not associated to this instance, ignore\r\n",
            p_ble_evt->evt.gattc_evt.conn_handle);
        return;
    }
    NRF_LOG_DEBUG("received HVX on handle 0x%x, m_handle 0x%x\r\n",
        p_ble_evt->evt.gattc_evt.paras.hvx.handle,
        p_ble_c->peer_db.m_handle);
    // Check if this is a notification.
    if (p_ble_evt->evt.gattc_evt.paras.hvx.handle == p_ble_c->peer_db.m_handle)
    {
        ble_c_evt_t ble_c_evt;
        uint32_t        index = 0;

        ble_c_evt.evt_type                    = BLE_C_EVT_m_NOTIFICATION;
        ble_c_evt.conn_handle                 = p_ble_c->conn_handle;

        
         printf("received length %d \r\n",(p_ble_evt->evt.gattc_evt.paras.hvx.len));

        printf("p_ble_evt->evt.gattc_evt.paras.hvx.data 0 %f \r\n",p_ble_evt->evt.gattc_evt.paras.hvx.data[0]);
        printf("p_ble_evt->evt.gattc_evt.paras.hvx.data 1 %f \r\n",p_ble_evt->evt.gattc_evt.paras.hvx.data[1]);
        printf("p_ble_evt->evt.gattc_evt.paras.hvx.data 2 %f \r\n",p_ble_evt->evt.gattc_evt.paras.hvx.data[2]);
        printf("p_ble_evt->evt.gattc_evt.paras.hvx.data 3 %f \r\n",p_ble_evt->evt.gattc_evt.paras.hvx.data[3]);
        printf("p_ble_evt->evt.gattc_evt.paras.hvx.data 4 %f \r\n",p_ble_evt->evt.gattc_evt.paras.hvx.data[4]);
        ble_c_evt.paras.hr.m_value = p_ble_evt->evt.gattc_evt.paras.hvx.data[1]; 
        printf("m_value %f \r\n",ble_c_evt.paras.hr.m_value);
        NRF_LOG_RAW_HEXDUP_INFO("the Hvx.data",*p_ble_evt->evt.gattc_evt.paras.hvx.data)
        p_ble_c->evt_handler(p_ble_c, &ble_c_evt);
    }
}

The function that sends dat from the pheripherial

uint32_t ble_msr_send(ble_msr_t * p_ms, float measurement)
{
    uint32_t err_code;

    // Send value if connected and notifying
    if (p_ms->conn_handle != BLE_CONN_HANDLE_INVALID)
    {
        uint16_t               len;
        uint16_t               hvx_len;
        ble_gatts_hvx_params_t hvx_params;

        len     =  sizeof(measurement);
        hvx_len = len;

        memset(&hvx_params, 0, sizeof(hvx_params));

        hvx_params.handle = p_ms->hrm_handles.value_handle;
        hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
        hvx_params.offset = 0;
        hvx_params.p_len  = &hvx_len;
        hvx_params.p_data = (uint8_t *)& measurement;

        err_code = sd_ble_gatts_hvx(p_ms->conn_handle, &hvx_params);
        if ((err_code == NRF_SUCCESS) && (hvx_len != len))
        {
            err_code = NRF_ERROR_DATA_SIZE;
        }
    }
    else
    {
        err_code = NRF_ERROR_INVALID_STATE;
    }

    return err_code;
}
  • Which SDK and SoftDevice version are you using?

    Should it be

    p_ble_evt->evt.gattc_evt.params.hvx.data[]
    

    not

    p_ble_evt->evt.gattc_evt.paras.hvx.data[0]
    
  • @Petter Myhre, I am using SDK12.2. If I use p_ble_evt->evt.gattc_evt.params.hvx.data[] I get an error sayingexpected expression before ']' token

  • You don't have params in the ble_gattc_evt_t struct in ble_gattc.h?

    /**@brief GATTC event structure. */
    typedef struct
    {
      uint16_t            conn_handle;                /**< Connection Handle on which event occured. */
      uint16_t            gatt_status;                /**< GATT status code for the operation, see @ref BLE_GATT_STATUS_CODES. */
      uint16_t            error_handle;               /**< In case of error: The handle causing the error. In all other cases @ref BLE_GATT_HANDLE_INVALID. */
      union
      {
        ble_gattc_evt_prim_srvc_disc_rsp_t          prim_srvc_disc_rsp;         /**< Primary Service Discovery Response Event Parameters. */
        ble_gattc_evt_rel_disc_rsp_t                rel_disc_rsp;               /**< Relationship Discovery Response Event Parameters. */
        ble_gattc_evt_char_disc_rsp_t               char_disc_rsp;              /**< Characteristic Discovery Response Event Parameters. */
        ble_gattc_evt_desc_disc_rsp_t               desc_disc_rsp;              /**< Descriptor Discovery Response Event Parameters. */
        ble_gattc_evt_char_val_by_uuid_read_rsp_t   char_val_by_uuid_read_rsp;  /**< Characteristic Value Read by UUID Response Event Parameters. */
        ble_gattc_evt_read_rsp_t                    read_rsp;                   /**< Read Response Event Parameters. */
        ble_gattc_evt_char_vals_read_rsp_t          char_vals_read_rsp;         /**< Characteristic Values Read Response Event Parameters. */
        ble_gattc_evt_write_rsp_t                   write_rsp;                  /**< Write Response Event Parameters. */
        ble_gattc_evt_hvx_t                         hvx;                        /**< Handle Value Notification/Indication Event Parameters. */
        ble_gattc_evt_exchange_mtu_rsp_t            exchange_mtu_rsp;           /**< Exchange MTU Response Event Parameters. */
        ble_gattc_evt_timeout_t                     timeout;                    /**< Timeout Event Parameters. */
        ble_gattc_evt_attr_info_disc_rsp_t          attr_info_disc_rsp;         /**< Attribute Information Discovery Event Parameters. */
      } params;                                                                 /**< Event Parameters. @note Only valid if @ref gatt_status == @ref BLE_GATT_STATUS_SUCCESS. */
    } ble_gattc_evt_t;
    /** @} */
    
Related