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

How to read characteristic value

Hi, all

I'm using nRF52832 pc10040, s132.

This is ble_app_uart_c example, and I'm trying to read my own beacon device's characteristic value.

ble_uuid_t p_uuid = {.uuid = 0x1601, .type = BLE_UUID_TYPE_BLE};
ble_gattc_handle_range_t p_handle_range = {.start_handle = 0x0001, .end_handle = 0xFFFF};

err_code = sd_ble_gattc_char_value_by_uuid_read(p_ble_nus_evt->conn_handle, &p_uuid, &p_handle_range);
            
if(err_code != NRF_SUCCESS)
{
    NRF_LOG_INFO("Failed to send read request to peer error = %d", err_code);
    printf("\n\rFailed to send read request to peer error = %d\n\r", err_code);
}

I successfully retrieved device name using line 4: 'sd_ble_gattc_char_value_by_uuid_read'.

I also read the value of '0' before my device sent data.


But if I try to read the moment the device sends data, I can't get the value right.

I have to read this type of value.

case BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP :
            for(uint32_t j = 0; j <=  p_read_rsp->len; j++) {
                for (uint32_t i = 0; i < p_read_rsp->offset; i++)
                {
                    while(app_uart_put( p_read_rsp->data[i]) != NRF_SUCCESS);
                }
                printf("\r\n");       
            }
            break;

this is my output part code.

How can I get this value?

Parents
  • You may read characteristic from your gatt evt handle like this.

    void ble_gatt_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
    {
    if ((p_context == NULL) || (p_ble_evt == NULL))
    {
    return;
    }

    ble_gatt_c_t * p_ble_gatt_c = (ble_gatt_c_t *)p_context;

    switch (p_ble_evt->header.evt_id)
    {
    case BLE_GATTC_EVT_HVX:
    on_hvx(p_ble_gatt_c, p_ble_evt);
    break;

    case BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP:

    on_read_rsp(p_ble_gatt_c, p_ble_evt);
    break;

    case   BLE_GATTC_EVT_WRITE_RSP:

    on_write_rsp(p_ble_gatt_c, p_ble_evt);
    break;

    case BLE_GAP_EVT_DISCONNECTED:
    on_disconnected(p_ble_gatt_c, p_ble_evt);
    break;

    default:
    break;
    }
    }

    In on_read_rsp code

    evt->gattc_evt.params.char_val_by_uuid_read_rsp.handle_value[?]

    to get your data.

    By the way, your GAP service uuid=0x1601.

    should be setup in your handle initial.

Reply
  • You may read characteristic from your gatt evt handle like this.

    void ble_gatt_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
    {
    if ((p_context == NULL) || (p_ble_evt == NULL))
    {
    return;
    }

    ble_gatt_c_t * p_ble_gatt_c = (ble_gatt_c_t *)p_context;

    switch (p_ble_evt->header.evt_id)
    {
    case BLE_GATTC_EVT_HVX:
    on_hvx(p_ble_gatt_c, p_ble_evt);
    break;

    case BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP:

    on_read_rsp(p_ble_gatt_c, p_ble_evt);
    break;

    case   BLE_GATTC_EVT_WRITE_RSP:

    on_write_rsp(p_ble_gatt_c, p_ble_evt);
    break;

    case BLE_GAP_EVT_DISCONNECTED:
    on_disconnected(p_ble_gatt_c, p_ble_evt);
    break;

    default:
    break;
    }
    }

    In on_read_rsp code

    evt->gattc_evt.params.char_val_by_uuid_read_rsp.handle_value[?]

    to get your data.

    By the way, your GAP service uuid=0x1601.

    should be setup in your handle initial.

Children
Related