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

Setting Custom Gatt characteristic data.

Hi,

I want to set a custom value to my Gatt characteristic value. I can do this by writing to the characteristic in my app or the NRF_connect application. 

What I want to do is based on an event or the data I receive to change the GATT characteristic data for my app to receive. I tried using this code which, did not change the data.

//err_code = ble_cus_custom_value_update(p_cus, p_ble_evt,m_custom_value);
//APP_ERROR_CHECK(err_code);
//I pass in a fixed value for testing into m_custom_value

uint32_t ble_cus_custom_value_update(ble_cus_t * p_cus, ble_evt_t const * p_ble_evt, uint8_t custom_value)
{
    NRF_LOG_INFO("In ble_cus_custom_value_update. \r\n"); 
    if (p_cus == NULL)
    {
        return NRF_ERROR_NULL;
    }

    uint32_t err_code = NRF_SUCCESS;
    ble_gatts_value_t gatts_value;

    // Initialize value struct.
    memset(&gatts_value, 0, sizeof(gatts_value));

    gatts_value.len     = sizeof(uint8_t);
    gatts_value.offset  = 0;
    gatts_value.p_value = &custom_value;

    // Update database.
    err_code = sd_ble_gatts_value_set(p_cus->conn_handle,
                                      p_cus->custom_value_handles.value_handle,
                                      &gatts_value);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }

    // Send value if connected and notifying.
    if ((p_cus->conn_handle != BLE_CONN_HANDLE_INVALID)) 
    {
        ble_gatts_hvx_params_t hvx_params;

        memset(&hvx_params, 0, sizeof(hvx_params));

        hvx_params.handle = p_cus->custom_value_handles.value_handle;
        hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
        hvx_params.offset = gatts_value.offset;
        hvx_params.p_len  = &gatts_value.len;
        hvx_params.p_data = gatts_value.p_value;

        err_code = sd_ble_gatts_hvx(p_cus->conn_handle, &hvx_params);
    }
    else
    {
        err_code = NRF_ERROR_INVALID_STATE;
    }


    return err_code;
}

What I need to know is how to update this data value so I can transfer useful data between the two devices (mobile application and nordic board).

Note I am trying to do this within my GATT connection itself.

Note using SDK15 , PCA10056 NRF52840-dk Segger,

Thanks

  • Hi,

    What I want to do is based on an event or the data I receive to change the GATT characteristic data for my app to receive. I tried using this code which, did not change the data.

     So you are calling ble_cus_custom_value_update() to update the value? but the value is not updated? Did you get any errors codes returned from ble_cus_custom_value_update() ?

  • Thanks for your response. Sorry I forgot to add on calling the function(from within my gatt connection code). It returns an error: 3400. 0r 0x00003400. Just to add some context I am receiving a command from a GATT connection via a mobile application which if passed(Correct command) i then output some text via uart and call the ble_cus_custom_value_update();

    I am not however, sure why this crash occurs. Thanks

  • Also another question(Completely non related). I found a post you commented on about timing and calender's. I have downloaded the example and it works. But, can I add this functionality to my code as in the comments it says to add soft device support. If my GATT connection is maintained e.g. I'm transferring data between two devices; will the date still increment each second?

    Thanks,

    Post is here: https://devzone.nordicsemi.com/f/nordic-q-a/25086/local-time-utc-and-timezones. With a link to the github.

  • Did you add the service / characteristic like this ?

    https://github.com/bjornspockeli/custom_ble_service_example/blob/SDK_v15.0.0/ble_cus.c#L198

    Thomas said:
    It returns an error: 3400. 0r 0x00003400

     This is BLE_ERROR_GATTS_INVALID_ATTR_TYPE .  

    If you call sd_ble_gatts_hvx() and the return error code is:

    * @retval ::BLE_ERROR_GATTS_INVALID_ATTR_TYPE Invalid attribute type(s) supplied, only characteristic values may be notified and indicated.

    Then I think either hvx_params.handle is wrong (maybe it's not the correct handle to the characteristic that can notify values), or the handle you are using is pointing to a characteristic that don't support notifications (char_props.notify).

  • I believe I added the service and characteristic properly. I did follow that tutorial but here is my init code non the less. 

    The data for p_cus and the event is passed in from the on_write statement inside of the gatt connection itself. 

    uint32_t ble_cus_init(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init)
    {
        //Init statemachine for reading the commands
    
        CMD_BLE_STATE_VALUE = CMD_BLE_STATE_START;//Move to start state
        CMD_STARTED = true;
    
        if (p_cus == NULL || p_cus_init == NULL)
        {
            return NRF_ERROR_NULL;
        }
    
        uint32_t   err_code;
        ble_uuid_t ble_uuid;
    
        // Initialize service structure
        //p_cus->evt_handler               = p_cus_init->evt_handler;
        p_cus->conn_handle               = BLE_CONN_HANDLE_INVALID;
        //p_cus->conn_handle               = BLE_CONN_HANDLE_INVALID;
    
        // Add Custom Service UUID
        ble_uuid128_t base_uuid = {CUSTOM_SERVICE_UUID_BASE};
        err_code =  sd_ble_uuid_vs_add(&base_uuid, &p_cus->uuid_type);
        VERIFY_SUCCESS(err_code);
        
        ble_uuid.type = p_cus->uuid_type;
        ble_uuid.uuid = CUSTOM_SERVICE_UUID;
    
        // Add the Custom Service
        err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_cus->service_handle);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
        // Add Custom Value characteristic
        return custom_value_char_add(p_cus, p_cus_init);
    }

    You are correct though it is sd_ble_gatt_hvx that is the issue. So how can I write to it? Do I change it from having notifications enabled to something that is not?

Related