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

Send data on an notification event

hello everybody,

I need to send some data on the value attached with the notification:

 /**@brief Function for adding the notify Level characteristic.
   *
   * @param[in]   p_notify        notify Service structure.
   * @param[in]   p_notify_init   Information needed to initialize the service.
   *
   * @return      NRF_SUCCESS on success, otherwise an error code.
   */


   static uint32_t notification_char_add(ble_notify_t * p_notify, const ble_notify_init_t *  p_notify_init)
 {
uint32_t            err_code;
ble_gatts_char_md_t char_md;
ble_gatts_attr_md_t cccd_md;
ble_gatts_attr_t    attr_char_value;
ble_uuid_t          ble_uuid;
ble_gatts_attr_md_t attr_md;
uint8_t             initial_notification;
uint8_t             encoded_report_ref[BLE_SRV_ENCODED_REPORT_REF_LEN];
uint8_t             init_len; 

// Add notify Level characteristic
if (p_notify->is_notification_supported)
{
    memset(&cccd_md, 0, sizeof(cccd_md));

    // According to notify_SPEC_V10, the read operation on cccd should be possible without
    // authentication.
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
    cccd_md.write_perm = p_notify_init->notification_char_attr_md.cccd_write_perm;
    cccd_md.vloc       = BLE_GATTS_VLOC_STACK;
}

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

char_md.char_props.read   = 1;
char_md.char_props.notify = (p_notify->is_notification_supported) ? 1 : 0;
char_md.p_char_user_desc  = NULL;
char_md.p_char_pf         = NULL;
char_md.p_user_desc_md    = NULL;
char_md.p_cccd_md         = (p_notify->is_notification_supported) ? &cccd_md : NULL;
char_md.p_sccd_md         = NULL;

ble_uuid.type = p_notify->uuid_type;
ble_uuid.uuid = UUID_notify_CHAR;
	
memset(&attr_md, 0, sizeof(attr_md));

attr_md.read_perm  = p_notify_init->notification_char_attr_md.read_perm;
attr_md.write_perm = p_notify_init->notification_char_attr_md.write_perm;
attr_md.vloc       = BLE_GATTS_VLOC_STACK;
attr_md.rd_auth    = 0;
attr_md.wr_auth    = 0;
attr_md.vlen       = 0;

initial_notification = p_notify_init->initial_notification;

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

attr_char_value.p_uuid    = &ble_uuid;
attr_char_value.p_attr_md = &attr_md;
attr_char_value.init_len  = sizeof(uint8_t *);
attr_char_value.init_offs = 0 ;
attr_char_value.max_len   = 26 ;
attr_char_value.p_value   = &initial_notification;

err_code = sd_ble_gatts_characteristic_add(p_notify->service_handle, &char_md,
                                           &attr_char_value,
                                           &p_notify->notification_handles);
if (err_code != NRF_SUCCESS)
{
    return err_code;
}

if (p_notify_init->p_report_ref != NULL)
{
    // Add Report Reference descriptor
    BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_REPORT_REF_DESCR);

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

    attr_md.read_perm = p_notify_init->notification_report_read_perm;
    BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm);

    attr_md.vloc    = BLE_GATTS_VLOC_STACK;
    attr_md.rd_auth = 0;
    attr_md.wr_auth = 0;
    attr_md.vlen    = 0;
    
    init_len = ble_srv_report_ref_encode(encoded_report_ref, p_notify_init->p_report_ref);
    
    memset(&attr_char_value, 0, sizeof(attr_char_value));

    attr_char_value.p_uuid    = &ble_uuid;
    attr_char_value.p_attr_md = &attr_md;
    attr_char_value.init_len  = init_len;
    attr_char_value.init_offs = 0;
    attr_char_value.max_len   = attr_char_value.init_len;
    attr_char_value.p_value   = encoded_report_ref;

    err_code = sd_ble_gatts_descriptor_add(p_notify->notification_handles.value_handle,
                                           &attr_char_value,
                                           &p_notify->report_ref_handle);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }
}
else
{
    p_notify->report_ref_handle = BLE_GATT_HANDLE_INVALID;
}

return NRF_SUCCESS;

}

it works with me, but I have a problem the data size is variable, I need to change the maximum value of attribute : attr_char_value.max_len = 26 ;

to be able to send data without the garbage, otherwise I need a way not to send this garbage data :) :)

Parents Reply Children
No Data
Related