Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Where is the proper place to close a connection when a service is not discovered

Hi, I am developing a central device application using the ble_app_multirole_lesc example as a baseline. I am using v11 of the SDK. This example uses the heart rate service as a demonstration on how to connect, discover services, characteristics, etc. and receive updates.

So far in my application I have removed all peripheral mode functionality and I have added a filter for the advertisements so that I only attempt connections with my peripheral device. At this point the central device will successfully connect to my sensor. It then attempts to discover the HRS and RSCS services which will fail as I have a custom service on my device. The problem is that the central device does not release the connection after failing to find the heart rate services. It will maintain the connection forever if I don't reset one of the two devices.

After some investigation, I have found that the on_srv_disc_completion function in the ble_db_discovery.c code module seems to be the best place to close the connection. Below code snippet shows the three lines of code I added to close the connection:

else
{
    NRF_LOG_PRINTF("Inside the on_srv_disc_completion function - no more discovery needed\r\n");

    // No more service discovery is needed.
    p_db_discovery->discovery_in_progress = false;
    m_pending_user_evts[0].evt.evt_type = BLE_DB_DISCOVERY_AVAILABLE;
    m_pending_user_evts[0].evt.conn_handle = conn_handle;
    //m_evt_handler(&m_pending_user_evts[0].evt);

   err_code = sd_ble_gap_disconnect(conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
   APP_ERROR_CHECK(err_code);
}

I also discovered the connection could be closed in the ble_hrs_on_db_disc_evt of the ble_hrs_c.c code module but it seems the function above will still execute before the connection is closed so I moved it there.

My question is where is the best place to close the connection when a desired service is not discovered? It seems I should not have to change files in the SDK to make this happen. I expected there to be an event I could register for in my application and handle it there but I can't see what that is.

Parents Reply Children
No Data
Related