How can I get my service characteristic value.

How do I get the value of the characteristic of the service I created? I try to get it with the following code, but the content pointer returned is NULL.

uint32_t gatts_char_value_get(uint16_t handle)
{
  uint32_t error_code = NRF_SUCCESS;
  ble_gatts_value_t r_value;
  r_value.p_value = NULL;
  error_code = sd_ble_gatts_value_get(BLE_CONN_HANDLE_INVALID, handle, &r_value);
  if (error_code != NRF_SUCCESS)
  {
    usb_printf_variadic("GATTS Value Get Error Code: 0x%04x", error_code);
    return error_code;
  }
  usb_printf_variadic("GATTS Value Get OK\n");
  NRF_LOG_INFO("P_Value Address: %p",r_value.p_value);
  usb_printf_variadic("Handle: %04X, Value Length: %d, Value: %s\n", handle, r_value.len, r_value.p_value);
  return error_code;
}

Parents
  • Update: This answer is incorrect. Please see my later answer

    Hi MapleBay,

    Refer to the documentation for ble_gatts_value_t:

    uint8_t* ble_gatts_value_t::p_value
    Pointer to where value is stored or will be stored.
    If value is stored in user memory, only the attribute length is updated when p_value == NULL. Set to NULL when reading to obtain the complete length of the attribute value<

    You are likely reading the value of an attribute that was setup to be stored in user memory. Please trace back to the attribute's initialization for where its value is supposed to be set, and read from there.

    Hieu

Reply
  • Update: This answer is incorrect. Please see my later answer

    Hi MapleBay,

    Refer to the documentation for ble_gatts_value_t:

    uint8_t* ble_gatts_value_t::p_value
    Pointer to where value is stored or will be stored.
    If value is stored in user memory, only the attribute length is updated when p_value == NULL. Set to NULL when reading to obtain the complete length of the attribute value<

    You are likely reading the value of an attribute that was setup to be stored in user memory. Please trace back to the attribute's initialization for where its value is supposed to be set, and read from there.

    Hieu

Children
Related