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

Adding a second characteristic to a custom service

nrf52DK - SDK12.2

This is probably a simple thing I am missing but I can't seem to find the answer.

I have been following the tutorial for BLE Characteristics (here)

I was able to follow through the whole thing and make changes needed - it was very well written and informative.

I am now at the point where I want to add another characteristic to the service for a photocell reading (already sending the SD temp on the other characteristic). I am using the same timer to send the photocell reading. I added a new characteristic uuid - no problem. I can see the new characteristic in nrfConnect app (android). The notification values it sends out are always on only one of the characteristics. I am pretty sure it has something to do with this quote from the tutorial:

handle: The SoftDevice needs to know what characteristic value we are working on. In applications with two or more characteristics naturally we will need to reference the handle of the specific characteristic value we want to use. Our example only has one characteristic and we will use the handle stored in p_our_service->char_handles.value_handle.

Below is my code to update the sd temp characteristic value (I made it an uint8_t to show the temp in degrees C) :

// OUR_JOB: Step 3.E, Update characteristic value
if (p_our_service->conn_handle != BLE_CONN_HANDLE_INVALID)
{
    uint16_t               len = 1;
    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*)temperature_value;
    sd_ble_gatts_hvx(p_our_service->conn_handle, &hvx_params);
}

And my code for updating my photocell reading characteristic value:

{
if (p_our_service->conn_handle != BLE_CONN_HANDLE_INVALID)
{
    uint16_t               len = 2;
    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*)photoReading_value;
    sd_ble_gatts_hvx(p_our_service->conn_handle, &hvx_params);
}   

Can someone explain how I can reference the handle of each specific characteristic?

Thanks,

Bryan

  • Hi Bryan,

    You need to add a new variable holding the handles for the second characteristics. In step 2.D of the tutorial, you are asked to add the member ble_gatts_char_handles_t char_handles; to the struct ble_os_t. You need to add a second member of type ble_gatts_char_handles_t to this struct, holding the handles of your second charactersitics. Your struct can look something like this:

    typedef struct
    {
        uint16_t                    conn_handle; 
        uint16_t                    service_handle;        
        ble_gatts_char_handles_t    char_a_handles;
        ble_gatts_char_handles_t    char_b_handles;
    } ble_os_t;
    

    In the next step, you will use the new handle holder when adding the new characteristics to the GATT table:

    err_code = sd_ble_gatts_characteristic_add(p_our_service->service_handle,
                                               &char_md,
                                               &attr_char_value,
                                               &p_our_service->char_b_handles);
    APP_ERROR_CHECK(err_code);
    

    Best regards,

    Jørgen

  • Jørgen, Thank you so much! I feel really stupid for not figuring that out myself. I also had to change my code for updating the photocell reading as below:

    if (p_our_service->conn_handle != BLE_CONN_HANDLE_INVALID)
    {
        uint16_t               len = 2;
        ble_gatts_hvx_params_t hvx_params;
        memset(&hvx_params, 0, sizeof(hvx_params));
        hvx_params.handle = p_our_service->char_b_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*)photoReading_value;
        sd_ble_gatts_hvx(p_our_service->conn_handle, &hvx_params);
    }
    

    Thank you again - big help! Bryan

Related