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

Writing a characteristic value from within the peripheral.

This is probably a simple question for an experienced nRF developer.

I have a demo going where I can read or write any of four characteristics I have set up on my peripheral (in my case an nRF58232 dev board) from the central. IE: I can easily use nRF Connect to R/W any of these characteristics. This demonstrates that I can send data from the central to the peripheral.

Now, I want to send data from the peripheral to the central. I am aware I need to assign a CCCD to the characteristics and set the NOTIFY bit. All good so far. I can see the notify field in nRF Connect for my four characteristics and am able to enable or disable this value.

Next, I want to modify the characteristic value based on an action taken AT THE PERIPHERAL. In this case, the dev board has four buttons. If I press one of the buttons, I want to do two things:

1) Increment the respective characteristic value by one.

2) Notify the central which characteristic has changed and what the updated value is.

QUESTION 1) What is the preferred call to update the characteristic value from within the peripheral itself?

QUESTION 2) What is the recommended code for sending this update back to the central?

From one of the online tutorials, I believe a partial answer to Question 2 is code that looks something like:

        uint16_t len = 1;     // characteristic value is one uint8_t 

        ble_gatts_hvx_params_t hvx_params;
        memset(&hvx_params, 0, sizeof(hvx_params));
        hvx_params.handle = p_our_service->char_handles.value_handle;
        hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
        hvx_params.offset = 0;
        hvx_params.p_len  = &len;
        hvx_params.p_data = (uint8_t*)characteristic_value; 
        sd_ble_gatts_hvx(p_our_service->conn_handle, &hvx_params);
What I don't quite understand is how to determine and communicate exactly which characteristic got changed.
  • So I'll partially answer my own question after a bit of exploration. By explicitly setting the handle of the characteristic I want to send to the central, I can easily communicate which characteristic got modified. The handle was retrieved and stored at the time I set up the characteristic using the call to: sd_ble_gatts_characteristic_add();

            uint16_t  len = 1;
            ble_gatts_hvx_params_t hvx_params;
            memset(&hvx_params, 0, sizeof(hvx_params));
            hvx_params.handle = char_handle;
            hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
            hvx_params.offset = 0;
            hvx_params.p_len  = &len;
            hvx_params.p_data = (uint8_t*) char_value; 
            sd_ble_gatts_hvx(p_our_service->conn_handle, &hvx_params);
    I now pass in the "char_handle" and "char_value" that I want to NOTIFY the central about.
    My question #1 still stands though on what is the preferred way to modify the characteristic at the peripheral. Currently, I am just directly modifying the location in memory (which is just a uint8_t variable) that I originally assigned to the characteristic value prior to initiation. However, I am still wondering if this might not be the best or proper way to inform the BLE stack that a value was changed.
    Please advise. Thanks!
  • Hi,

    Sorry for the late reply.

    For Question 1) What is the preferred call to update the characteristic value from within the peripheral itself?
     
    If you only want to update the value, and not the send the update to a central, use sd_ble_gatts_value_set().
    For Question 2) What is the recommended code for sending this update back to the central?
     
    Use sd_ble_gatts_hvx(), this will both update the internal characteristic value in the SD, and send the update to the central. See this Message Sequence chart on the infocenter:
    https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.s132.api.v7.0.1%2Fgroup___b_l_e___g_a_t_t_s___h_v_n___m_s_c.html
    Best regards,
    Marjeris
Related