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

Create BLE characteristic with notifications active on queued write service

I'm developing an application to communicate with a smartphone. I have the need of writing characteristics with the size bigger than the negotiated MTU, so I'm exploring Queued write example. So far, I'm able to write to the characteristic, however, I need to have two characteristics, one used to write and another to read with the possibility of push notifications.

To do that I tried to change the source code to add a characteristic with "read" and "notify" permissions, as you can see below:

    ble_add_char_params_t add_char_params;

    memset(&add_char_params, 0, sizeof(add_char_params));
    add_char_params.uuid               = BLE_UUID_QWRS_LONG_CHARACTERISTIC;
    add_char_params.max_len            = BLE_QWRS_MAX_LONG_CHAR_LEN;
    add_char_params.init_len           = 0;
    add_char_params.char_props.write   = true;
    add_char_params.write_access       = SEC_OPEN;
    add_char_params.is_defered_write   = true;

    err_code = characteristic_add(p_qwrs->service_handle,
                                  &add_char_params,
                                  &p_qwrs->long_charact_handles);

to

    ble_add_char_params_t add_char_params;

    memset(&add_char_params, 0, sizeof(add_char_params));
    add_char_params.uuid               = BLE_UUID_QWRS_LONG_CHARACTERISTIC;
    add_char_params.max_len            = BLE_QWRS_MAX_LONG_CHAR_LEN;
    add_char_params.init_len           = 0;
    add_char_params.char_props.read    = true;
    add_char_params.char_props.notify  = true;
    add_char_params.read_access       = SEC_OPEN;

    err_code = characteristic_add(p_qwrs->service_handle,
                                  &add_char_params,
                                  &p_qwrs->long_charact_handles);

After that change I received an error code "NRF_INVALID_PARAM" and the characteristic is not added. I also tried to add the read permission only and that works fine.

Can somebody help me to understand how can I add notify permissions?

Note: I am using nordic SDK v13.0.0.

  • Hi Bjørn,

    I will repeat the tests and send you a log of the nRF connect.

    However, I don't understand yet why I can't add a characteristic with those permissions using my code. in your first comment you told "I think that you're missing the pointer to the ble_gatts_attr_md_t cccd_md struct in the charachteristic metadata( char_md).". Where did I forgot the pointer? What do I need to change to put it working?

    Thanks

  • Hi Nuno.

    I took a look at the characteristic_add() function that is used in the nrf_ble_qwrs_init() function. The characteristic_add() function is a utility function that sets a lot of the parameters automatically, as opposed to setting them manually as I do in my tutorial. If you want the QWRS characteristic to have Read and Notify properties in addition to Write, then I think you simply need to add 

    add_char_params.char_props.notify= true;
    add_char_params.char_props.read = true;

    when the ble_add_char_params_t struct is populated. The characteristic_add() function will examine the properties and then set the appropriate permissions for the notification property, i.e.

    memset(&char_md, 0, sizeof(ble_gatts_char_md_t));
    if ((p_char_props->char_props.notify == 1)||(p_char_props->char_props.indicate == 1))
    {
    
        memset(&cccd_md, 0, sizeof(cccd_md));
        set_security_req(p_char_props->cccd_write_access, &cccd_md.write_perm);
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
    
        cccd_md.vloc       = BLE_GATTS_VLOC_STACK;
    
        char_md.p_cccd_md  = &cccd_md;
    }

  • Hi Bjørn,

    If you take a look at my code at the beginning of this thread, that's exactly what I have done, however, after that change the function characteristic_add() returned NRF_INVALID_PARAM. Could I be forgetting something?

    Thanks 

    Nuno Ferreira

  • The only property I see in the attached code from August 2nd is 

        add_char_params.char_props.write   = true;

    However; I do got the same result and you need to add an additional line setting the cccd write access, i.e. 

    add_char_params.char_props.notify= true;
    add_char_params.char_props.read = true;
    add_char_params.cccd_write_access= true;

    Best regards

    Bjørn 

Related