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

Parents
  • Hi Prajakta, 

    The db_disc_handler() is where you handle the events from the db discovery library, for example when the discovery is complete, or when the service is not found. 

    However, the db discovery module was not really made for generic service discovery. This means usually you need to provide the service UUID(s) you want to discover to the module before it started. 

    To be able to do generic discovery (where you discovery all UUIDs) you would need to modify it a little bit. There is some discussion here. Please have a look and let me know if you have any question. 

  • I am using BLE_Blinky application, in that after connection done, in ble_evt_handler (),handle_assign function is there, then they called ble_db_discovery_start.

    As per this discussion, need to call sd_ble_gattc_primary_services_discover.
    1.So where I can call this function with parameters?

    And

    2. Can you help me for flow for service discovery from start(init) to end in example code.I am able to do it for ble_lbs_c service(blinky application). I am not getting how to replicate it for primary service discovery.

Reply
  • I am using BLE_Blinky application, in that after connection done, in ble_evt_handler (),handle_assign function is there, then they called ble_db_discovery_start.

    As per this discussion, need to call sd_ble_gattc_primary_services_discover.
    1.So where I can call this function with parameters?

    And

    2. Can you help me for flow for service discovery from start(init) to end in example code.I am able to do it for ble_lbs_c service(blinky application). I am not getting how to replicate it for primary service discovery.

Children
  • Hi Prajakta, 


    What you are planing to do requires you to have good understanding on how service discovery works. Have you studied the db_discovery library ? And understand how it works ? 

    Could you describe what kind of application you are planning to make ? Maybe you don't need to discover generic services. 

    If you find you need to do that, you maybe better not use the db_discovery but better write yourself some code to do so. I would suggest you to follow this sequence chart. After you find the UUID and the handle of the service (I assume you know how the attribute table organized) you can start discovery the characteristic. After that, you can continue to do primary service discovery from the last handle you have of the last primary service discovery response. 

    Basically what you need to do will be very similar to what we have done in the db_discovery, except for that the db discovery has a list of services to look for (p_db_discovery->services[] table). As endnode mentioned in the other thread, you need to call sd_ble_gattc_read() when you receive BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP event and the service UUID is BLE_UUID_TYPE_UNKNOWN. If it's not UNKNOWN, but a 16 bit UUID, you can just continue to do characteristics discovery from the handle of the service. 

    A suggestion to understand how the service discovery works is to capture a sniffer trace  and capture a trace of nRF Connect to do a service discovery. 

  • Application is something as below:

    I want to develop central code whether it will scan for particular device(3rd party device(not nordic)) and connect to it and then discover their services and print it on console.and then read/write their characteristics.

    i am attaching screenshot of that device when it connects from nrf connect.

      

  • I have gone through that sequence chart, i am not able to understand the what handle parameters need to give to sd_ble_gattc_primary_services_discover(...) at which place it should call.

  • 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.

Related