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?

  • 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.

  • Have you tried to debug and see what the data in the p_read_rsp->data looks like? What is the p_read_rsp->offset when you hit this for-loop?

  • Hi, Edvin

    What is the p_read_rsp->offset when you hit this for-loop?

    I just don't understand what offset means, so I tried 'for-loop'.

    Have you tried to debug and see what the data in the p_read_rsp->data looks like?

    When I tried to see my device name which is 'MyService',

    I can see my device name through 'p_read_rsp->data'.

      The picture is the result below.

    case BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP :
        printf("Read length: %d, Read offset: %d\r\n", p_read_rsp->len, p_read_rsp->offset);
        printf("Read %d\r\n", p_read_rsp->data);
               
        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;

    (I don't understand why offset fits the length of the data...)

    I tried to show the characteristic value, just like the name but it's not coming out properly.

  • Hi, Henry

    The 'ble_app_uart_c' example does not have 'on_read_rsp', so I compiled the code.

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

    ble_gattc_evt_read_rsp_t const * p_read_rsp = &p_ble_evt->evt.gattc_evt.params.read_rsp;

    I tried to access value with 'p_read_rsp' by defining like this.

  • Hey!Lyrics:

    the ble_app_uart.c dosen't have the code exactly. The code is build in my project case.

    However, you may take the reference code from SDK (Central or Peripheral) ble_app_gatts  example. The gatt_cache_manager.c

    void gcm_ble_evt_handler(ble_evt_t const * p_ble_evt)

    function. case BLE_GATTC_EVT_READ_RSP:

    You may get the answer which you need. However nus(Nordic Uart service) is also a kind of gatt. So the solution is the same.

    My SDK version is 17.0.2

Related