Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

nrf52-ble-app-uart-c-multilink - second service discovery and connection

Hi,

I have two nRF52840 devices one peripheral and one central. Both devices feature the Nordic Uart Service. They are based on ble_app_uart example and e nrf52-ble-app-uart-c-multilink example respectively (link).

The adapter device can successfully discover, connect and interact with the peripheral without issues.

I have successfully added to the peripheral a second service (custom) along with NUS based on this tutorial (link) and I have tested that it works using application nRF Connect on my smartphone.

Now I want my nRF52840 central device to be able to connect to that custom service along with NUS and receive notifications, data etc. Is there some tutorial or guide on how to do that or what additions/changes to perform to the code?

Thank you for your time

  • Hello,

    Now I want my nRF52840 central device to be able to connect to that custom service along with NUS and receive notifications, data etc. Is there some tutorial or guide on how to do that or what additions/changes to perform to the code?

    Your central device will be able to discover the custom service and characteristic as usual during the discovery, but it will need to know how to interact with the service and characteristic, and as such you must make a file similar to ble_nus_c.c/.h at the nRF5_SDK_17.1.0_ddde560\components\ble\ble_services\ble_nus_c location to implement this.
    Please have a look through these to see how the central side is set up for the NUS service, and use this as a reference for your own customer service central implementation.
    Please do not hesitate to ask if you should encounter any issues or questions while working with this!

    Best regards,
    Karl

  • Hi, yes I have already started doing that so I will let you know if I don't understand something (very likely). So I will leave this ticket open

  • Hello,

    Alright, great! I am glad to hear that you're already progressing towards this - dont hesitate to ask if there's anything! :) 

    Best regards,
    Karl

  • Hi, I faced an issue and I need your help
    First of all I will enumerate the steps I have taken so far

    1. Created the library for the custom service (client side) based on this service library (sdk path: components\ble\ble_services\ble_nus_c).

    2. Added this modified service library into a new project based on ble_app_uart_c example

    3. In main.c of the new project I added everything regarding the new service discovery. Specifically I added the following things

    //main.c modifications for client custom service 
    
    //--------------------------------------
    BLE_CUS_C_ARRAY_DEF(m_ble_cus_c, NRF_SDH_BLE_CENTRAL_LINK_COUNT);       //NOTE NEW FOR CUS
    //--------------------------------------
    /**@brief NUS UUID. */
    static ble_uuid_t const m_nus_uuid =
    {
        .uuid = BLE_UUID_NUS_SERVICE,
        .type = NUS_SERVICE_UUID_TYPE
    };
    
    static ble_uuid_t const m_cus_uuid =   //NOTE NEW FOR CUS
    {
        .uuid = BLE_UUID_CUS_SERVICE,
        .type = BLE_UUID_TYPE_VENDOR_BEGIN+1
    };
    //--------------------------------------
    
    static void db_disc_handler(ble_db_discovery_evt_t * p_evt)
    {
        ble_nus_c_on_db_disc_evt(&m_ble_nus_c[p_evt->conn_handle], p_evt);
        ble_cus_c_on_db_disc_evt(&m_ble_cus_c[p_evt->conn_handle], p_evt);    //NOTE NEW FOR CUS
    }
    //---------------------------------------
    
    //NOTE NEW FOR CUS
    static void ble_cus_c_evt_handler(ble_cus_c_t * p_ble_cus_c, ble_cus_c_evt_t const * p_ble_cus_evt)
    {
       ret_code_t err_code;
    
       switch (p_ble_cus_evt->evt_type)
       {
          case BLE_CUS_C_EVT_DISCOVERY_COMPLETE:
            NRF_LOG_INFO("CUS Discovery complete.");
            err_code = ble_cus_c_handles_assign(p_ble_cus_c, p_ble_cus_evt->conn_handle, &p_ble_cus_evt->handles);
            APP_ERROR_CHECK(err_code);
    
            err_code = ble_cus_c_char_notif_enable(p_ble_cus_c);
            APP_ERROR_CHECK(err_code);
            NRF_LOG_INFO("Connected to device with Custom Service.");
            break;
    
          case BLE_CUS_C_EVT_CUS_TX_EVT:      // NOTE The central received data from a peer
           
           
            //NRF_LOG_INFO("conHandle: 0x%02X", p_ble_nus_c->conn_handle);
            connectionHandle = (uint8_t) p_ble_cus_c->conn_handle;
            connectionLength = (uint8_t) p_ble_cus_evt->data_len;
            memcpy(connectionData, &p_ble_cus_evt->p_data[0], (uint8_t) p_ble_cus_evt->data_len);  
    
            NRF_LOG_INFO("DATA RECEIVED: 0x%.2X", p_ble_cus_evt->p_data[1]);
            break;
    
          case BLE_CUS_C_EVT_DISCONNECTED:
            NRF_LOG_INFO("Disconnected.");
            scan_start();
            break;
       }
    }
    //------------------------------------------------------
    
    static void cus_c_init(void)
    {
        ret_code_t   err_code;
        ble_cus_c_init_t init;
    
        init.evt_handler   = ble_cus_c_evt_handler;
        init.error_handler = cus_error_handler;
        init.p_gatt_queue  = &m_ble_gatt_queue;
        for(int c = 0; c < NRF_SDH_BLE_CENTRAL_LINK_COUNT; c++)
        {
            err_code = ble_cus_c_init(&m_ble_cus_c[c], &init);
            APP_ERROR_CHECK(err_code);
        }
    }
    //--------------------------------------------------------------
        nus_c_init();
        cus_c_init();               //NOTE NEW FOR CUS
    
    

    After both devices are connected I get the following messages from the debugger( central side)

    The CUS is discovered by the client which is good but,

    1. In ble_cus_c_handles_assign(), I don't understand why peer_handles are null

    2. Error 8 is returned from err_code = ble_cus_c_char_notif_enable(p_ble_cus_c); that is called inside the ble_cus_c_evt_handler()

    Any ideas?

  • I fixed it, the problem was in the custom service library where I did not include

    cus_c_evt.handles.cus_char_cccd_handle = p_chars[i].cccd_handle;

    under the case BLE_UUID_CUS_CHARACTERISTIC event

Related