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 Reply Children
  • Hi MapleBay,

    My apology, I misunderstood the API when I wrote my previous answer. It was wrong.

    You need to supply a buffer to readout the value in the .p_value field when you call sd_ble_gatts_value_get(). Something like this:

    uint32_t err_code;
    ble_gatts_value_t t_gatts_value;
    
    memset(&t_gatts_value, 0, sizeof(t_gatts_value));
    
    uint8_t t_buffer[__YOUR_MAXIMUM_POSSIBLE_SIZE__] = {0};
    
    t_gatts_value.len     = __YOUR_MAXIMUM_POSSIBLE_SIZE__;
    t_gatts_value.offset  = 0;
    t_gatts_value.p_value = t_buffer;
    
    err_code = sd_ble_gatts_value_get(BLE_CONN_HANDLE_INVALID,
    							  __YOUR_HANDLE__,
    							  &t_gatts_value);

    Hieu

  • Hi Hieu,

    Thanks for your help. Now I can get service value.

    MapleBay

Related