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

custom service discovery in BLE_Central

Hi,

I want to develop central code to discover services of 3rd party device and to discover their services and read/write characteristics.

As seen many examples from SDK below is my understanding:

1. Include db_discovery_init() 

2. call ble_db_discovery_start() on BLE_GAP_EVT_CONNECTED event in ble_evt_handler().

3. While initlaizing need to init service which is going to discover as below:

like in BLE_blinky example,includes ble_lbs service as below :


static void db_disc_handler(ble_db_discovery_evt_t * p_evt)
{
    ble_lbs_on_db_disc_evt(&m_ble_lbs_c, p_evt);
}

So my question is:

While db_discovery_init() which service should initialize in db_disc_handler() for custom service discovery? when we don't know which service is advertising by other device.

And

How to discover the services and print this and its characteristics on console. Same as we can see in nrf Connect app. Please suggest or share suitable code except BLE_UART example code.

Please help me on this asap.

Regards,

Prajakta

  • You can start with calling sd_ble_gattc_primary_services_discover() , use connection handle of your current connection, start_handle of 1 and p_srvc_uuid = NULL. It will discover any service starting from handle 1. You call this function after BLE_GAP_EVT_CONNECTED event. 

    Then you wait for BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP event inside ble_evt_handler() , when you receive this event, you can check how many service discovered and which handle range it covers inside the ble_gattc_service_t services[1]; in ble_gattc_evt_prim_srvc_disc_rsp_t struct of the event. 

    Then you can continue to call sd_ble_gattc_primary_services_discover() with the last handle until you receive ATTRIBUTE_NOT_FOUND in the next correspondent event . 

  • After connection its not getting anything.
    logs as below:

    <info> app: Press button 4 to start scanning.
    <info> app: Start scanning.
    <info> app: Start scanning.
    <info> app: Connected.

  • Code changes done in main.c as follows:

    static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
    {
        ret_code_t err_code;
    
        // For readability.
        ble_gap_evt_t const * p_gap_evt = &p_ble_evt->evt.gap_evt;
       // ble_uuid128_t base_uuid = {UUID_BASE};
       //  uint8_t uuidType = 2;
        switch (p_ble_evt->header.evt_id)
        {
            // Upon connection, check which peripheral has connected (HR or RSC), initiate DB
            // discovery, update LEDs status and resume scanning if necessary. */
            case BLE_GAP_EVT_CONNECTED:
                NRF_LOG_INFO("Connected.");
                err_code = sd_ble_gattc_primary_services_discover(p_gap_evt->conn_handle,
                                                                        SRV_DISC_START_HANDLE,0);
        }
        break;
    }
    

  • Please check the return code of err_code , make sure it's = NRF_SUCCESS

    Then please check for BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP event, same way as BLE_GAP_EVT_CONNECTED. 

    May I ask if you have used Nordic chip in other projects before ? Or it's your first time working with Nordic chip ? 

  • Yes it gives NRF_SUCCESS.

    Its first time i am working with nordic chip and also on BLE.

    also i have checked in BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP event.
    it doesnt come in that event. Also i have checked with 2-3 devices which have custom service.

Related