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 Naf18, 

    could you post the entire .c file where you're adding the characteristics? I think that you're missing the pointer to the ble_gatts_attr_md_t cccd_md struct in the charachteristic metadata( char_md).

    Best regards

    Bjørn

  • Hi ,

    thanks for your answer.

    Below you can check the functions I'm running to configure this service:

    retval_t initQWRSService()
    {
        uint32_t          err_code = 0;
        nrf_ble_qwr_init_t  qwr_init;
        nrf_ble_qwrs_init_t qwrs_init;
    
        // Initialize Queued Write Module
        qwr_init.mem_buffer.len   = MEM_BUFF_SIZE;
        qwr_init.mem_buffer.p_mem = umdc_qwrs_service.m_buffer;
        qwr_init.error_handler    = NULL;
        qwr_init.callback         = queuedWriteHandlerWrapper;
    
        err_code += nrf_ble_qwr_init(&umdc_qwrs_service.m_qwr, &qwr_init);
    
        //initialize the Queued Writes Example Service
        memset(&qwrs_init, 0, sizeof(qwrs_init));
    
        qwrs_init.evt_handler   = queuedWriteServiceEvtHandlerWrapper;
        qwrs_init.error_handler = NULL;
        qwrs_init.p_qwr_ctx     = &umdc_qwrs_service.m_qwr;
    
        err_code += nrfBleQWRSInit(&qwrs_init, &umdc_qwrs_service.m_qwrs);
    
        if (0 == err_code) {
            return SUCCESS;
        }
    
        return ERROR;
    }
    
    
    retval_t nrfBleQWRSInit(nrf_ble_qwrs_init_t *p_qwrs_init, nrf_ble_qwrs_t *p_qwrs)
    {
        ret_code_t    err_code = 0;
        ble_uuid_t    ble_uuid;
    
        // Initialize service structure.
        p_qwrs->evt_handler   = p_qwrs_init->evt_handler;
        p_qwrs->error_handler = p_qwrs_init->error_handler;
        p_qwrs->conn_handle   = BLE_CONN_HANDLE_INVALID;
    
        // Add service.
        ble_uuid128_t base_uuid = QWRS_BASE_UUID;
        err_code += sd_ble_uuid_vs_add(&base_uuid, &p_qwrs->uuid_type);
    
        ble_uuid.type = p_qwrs->uuid_type;
        ble_uuid.uuid = BLE_UUID_QWRS_SERVICE;
    
        err_code += sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
                                            &ble_uuid,
                                            &p_qwrs->service_handle);
    
        //Add Long characteristic
        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);
    
        if (p_qwrs_init->p_qwr_ctx != NULL)
        {
            err_code += nrf_ble_qwr_attr_register(p_qwrs_init->p_qwr_ctx,
                                                 p_qwrs->long_charact_handles.value_handle);
        }
    
        if (0 == err_code) {
            return SUCCESS;
        }
    
        return ERROR;
    }

    The next step I need is to add a characteristic with read and notifications properties active. Can you tell what I need to do?

    Thanks 

    Nuno Ferreira 

  • Hi Nuno, 

    I apologize for the late reply.

    I suggest that you take a look at custom_value_char_add() in ble_cus.c in the GitHub repo link below. 

    https://github.com/bjornspockeli/custom_ble_service_example/blob/master/ble_cus.c

    It adds a characteristic that has write/read/notification properties. 

    Bjørn 

     

  • Hi Bjørn,

    Thanks for your answer. I was trying that example and I was able to create a characteristic with reading and notifications permissions. It appeared at nordic nrf connect application, but when I tried to read something it failed and I was again unable to use that characteristic.

    Thanks

    Nuno Ferreira 

  • Could you be a bit more specific than "when I tried to read something it failed and I was again unable to use that characteristic"? Did you get en error message with an error code when you tried to write to it in nRF connect?

Related