This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to add custom service to the Ble_app_template example

Hello i am using nrf52810 with SDK version 17.0.2 , i want to add custom service to the ble_app_template example and i followed this tutorial link https://github.com/edvinand/custom_ble_service_example so at first debug i got this error as expected 

next i have change the RAM_START AND RAM_SIZE as per the memory size mentioned in the error then i DEBUG it again so the i got 

so i don't know what's happening please help me to resolve this . 

thank you.

Parents
  • Hello,

    Please make sure to have DEBUG defined in your preprocessor defines, like shown in the included image:

    This will make your logger output a detailed error message whenever a non-NRF_SUCCESS error code is passed to an APP_ERROR_CHECK.
    Please do this, and let me know what the error message reads, along with which function that returned the error code pointed to in the error message.

    Best regards,
    Karl

  • Hello,

    mr Karl i am very happy to communicate with you and thank you so much for your reply, i tried as you said  and i got this error,

    thank you,

  • Have you implemented and provided an event handler for your custom service during its initialization?

    Yes, in service init() i have declare like this,

    .

    static void services_init(void)
    {
        ret_code_t         err_code;
        
        nrf_ble_qwr_init_t qwr_init = {0};
        ble_cus_init_t                     cus_init;
        
        
    
        // Initialize Queued Write Module.
        qwr_init.error_handler = nrf_qwr_error_handler;
    
    
         cus_init.evt_handler               = on_cus_evt;
       
    
        err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init);
        APP_ERROR_CHECK(err_code);
    
        /* YOUR_JOB: Add code to initialize the services used by the application.
           ble_xxs_init_t                     xxs_init;
           ble_yys_init_t                     yys_init;
    
           // Initialize XXX Service.
           memset(&xxs_init, 0, sizeof(xxs_init));
    
           xxs_init.evt_handler                = NULL;
           xxs_init.is_xxx_notify_supported    = true;
           xxs_init.ble_xx_initial_value.level = 100;
    
           err_code = ble_bas_init(&m_xxs, &xxs_init);
           APP_ERROR_CHECK(err_code);
    
           // Initialize YYY Service.
           memset(&yys_init, 0, sizeof(yys_init));
           yys_init.evt_handler                  = on_yys_evt;
           yys_init.ble_yy_initial_value.counter = 0;
    
           err_code = ble_yy_service_init(&yys_init, &yy_init);
           APP_ERROR_CHECK(err_code);
         */
    
        
        
        // Initialize CUS Service.
        memset(&cus_init, 0, sizeof(cus_init));
        
        err_code = ble_cus_init(&m_cus, &cus_init);
        printf("err_code:%d",err_code);
        APP_ERROR_CHECK(err_code);   
     
        
        
    }
    

    and handler is,

     

    static void on_cus_evt(ble_cus_t     * p_cus_service,
                           ble_cus_evt_t * p_evt)
    {
     
        switch(p_evt->evt_type)
        {
            case BLE_CUS_EVT_CONNECTED:
                break;
    
            case BLE_CUS_EVT_DISCONNECTED:
                break;
    
            default:
                // No implementation needed.
                break;
        }
    
    }
    

    in tutorial i got that the handler should not be NULL so how can achieve this.??

  • It looks to me like you do not set the event handler as part of the ble_cus_init function in the function you shared previously. It seems you have omitted the line where the provided handler is registered to the custom service.
    Please rectify this, and let me know if this resolves your issue.

    Best regards,
    Karl

  • Hello, 

    No, i added but from that only problem created right..?? so removed. the updated ble_cus_init() function is 

    ret_code_t ble_cus_init(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init)
    {
        NRF_LOG_INFO("i am cu_init\r\n");
        if (p_cus == NULL || p_cus_init == NULL || p_cus_init->evt_handler == NULL)
        {
            return NRF_ERROR_NULL;
        }
    
        ret_code_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;
    
        // 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;    // This seems redundant, but we'll add more later
        }
        
      // Add the Custom Value Characteristic
        return custom_value_char_add(p_cus, p_cus_init);
        
    }
    

    but still same result and in service _init() if i comment memset of cus_init() then  advertising happened and i wrote two lines to receive the data which i have sent is it correct because whatever  the data if i sent same number was received.

    But this does not solve the underlying issue, it is just treating the symptoms. If you just remove error checks that fail the errors will still be there, you just will not know about it.
    To fix it, and make it function as expected you will need to resolve the underlying issue.

    so i have correct this that is my bad i added extra return this issue is resolved 

    please let me if there is any mistake.

    thank you.

  • this is two lines i have added to receive the data from mobile app 

    static void on_write(ble_cus_t * p_cus, ble_evt_t const * p_ble_evt)
    {
        ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
        p_evt_write->data[0]=p_evt_write;
        printf("%d", p_evt_write->data[0]);
        // Check if the handle passed with the event matches the Custom Value Characteristic handle.
        if (p_evt_write->handle == p_cus->custom_value_handles.value_handle)
        {
            nrf_gpio_pin_toggle(LED_4); 
        }
    }
    

  • Hello again,

    sagarnayakm said:
    No, i added but from that only problem created right..?? so removed

    I am not sure what you mean by this, but in general you should not just remove different lines here and there if they are causing you trouble. Instead, you should investigate what they do, why they are there in the first place, and why they are causing you trouble. If you just remove it you might be omitting important parts of your program which will be hard to debug later.

    sagarnayakm said:
    but still same result and in service _init() if i comment memset of cus_init() then  advertising happened and i wrote two lines to receive the data which i have sent is it correct because whatever  the data if i sent same number was received.

    You should not just comment out different lines in the hopes of the application being able to proceed past the step.
    Every step of the tutorial, and every line of code in it, is added for a reason, and needs to be present for the tutorial to function as expected.
    At this point I think the best way forward would be for your to go through the tutorial from the beginning, and make sure to follow all the steps as described, without commenting out or removing certain parts of the code unless you are completely certain of what that part of the code is doing.

    sagarnayakm said:
    so i have correct this that is my bad i added extra return this issue is resolved

    I am glad to hear that the issue is resolved for now but this will keep on happening as long as you comment out or remove lines without considering why they are there in the first place.
    I also see that you have some warnings in your code, please clean these up as well.

    Best regards,
    Karl

Reply
  • Hello again,

    sagarnayakm said:
    No, i added but from that only problem created right..?? so removed

    I am not sure what you mean by this, but in general you should not just remove different lines here and there if they are causing you trouble. Instead, you should investigate what they do, why they are there in the first place, and why they are causing you trouble. If you just remove it you might be omitting important parts of your program which will be hard to debug later.

    sagarnayakm said:
    but still same result and in service _init() if i comment memset of cus_init() then  advertising happened and i wrote two lines to receive the data which i have sent is it correct because whatever  the data if i sent same number was received.

    You should not just comment out different lines in the hopes of the application being able to proceed past the step.
    Every step of the tutorial, and every line of code in it, is added for a reason, and needs to be present for the tutorial to function as expected.
    At this point I think the best way forward would be for your to go through the tutorial from the beginning, and make sure to follow all the steps as described, without commenting out or removing certain parts of the code unless you are completely certain of what that part of the code is doing.

    sagarnayakm said:
    so i have correct this that is my bad i added extra return this issue is resolved

    I am glad to hear that the issue is resolved for now but this will keep on happening as long as you comment out or remove lines without considering why they are there in the first place.
    I also see that you have some warnings in your code, please clean these up as well.

    Best regards,
    Karl

Children
No Data
Related