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.

  • I am able to discover 4 services. But last service which is custom and having 128-bit UUID, its start handle and end handle = 0.
    So not able to discover characteristics.
    Output as below:

    <info> ble_db_disc: Found service UUID 1800 and its start handle:1 , End handle: 7
    <info> ble_db_disc: Found service UUID 1801 and its start handle:8 , End handle: 11
    <info> ble_db_disc: Found service UUID 180A and its start handle:12 , End handle: 20
    <info> ble_db_disc: Found service UUID 5D4 and its start handle:0 , End handle: 0

  • Hi Prajakta,

    Again please make sure you have the status of the BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP  event asSUCCESS not ATTRIBUTE_NOT_FOUND . 
    One place you can have a look is the ble_app_interactive, in ble_central_and_peripheral. In that example we actually support of discovering unknown UUID service. Please have a look at function on_read_rsp() inside ble_m.c.

    You may want to follow this thread  as the interactive example has some issues as well. 

  • Yes Hung Bui,

    I am following that example only. Many points are cleared from that code.

    But i am finding issue in sd_ble_uuid_vs_add().

    this function return failure.

    static void on_read_rsp(const ble_gattc_evt_t * const p_ble_gattc_evt)
    {
        uint32_t err_code;
       uint16_t                         conn_handle = p_ble_gattc_evt->conn_handle;
       ble_uuid128_t                    uuid = {UUID_BASE};
       const ble_gattc_evt_read_rsp_t * p_read_rsp = &(p_ble_gattc_evt->params.read_rsp);
       NRF_LOG_INFO("Read data = 0x%x,0x%x",p_read_rsp->data[13],p_read_rsp->data[14]);
       NRF_LOG_INFO("Read_rsp_len: %d",p_read_rsp->len);
       if (m_vendor_uuid_read)
       {
           for (uint8_t i = 0; i < mp_device_srv[conn_handle]->count; i++)
           {
              // if (p_read_rsp->len == 16)
    	   	   if (p_read_rsp->handle ==
    	                  mp_device_srv[conn_handle]->services[i].handle_range.start_handle)
               {
                   //lint -save -e420
                   memcpy(uuid.uuid128, p_read_rsp->data, sizeof(uuid.uuid128));
                   //lint -restore
                   err_code = sd_ble_uuid_vs_add(&uuid, &mp_device_srv[conn_handle]->services[i].uuid.type);
                   if(err_code == NRF_SUCCESS)
    				{
    					NRF_LOG_INFO("Adding 128-bit UUID  done successfully..");
    					//NRF_LOG_INFO("128-bit UUID : %x",uuid.uuid128);
    				}
    				else
    				{
    					NRF_LOG_INFO("Adding 128-bit UUID Fail..");
    				}
                   break;
               }
           }

  • Which failure did you receive ? Please check the return code. 
    Most likely you need to declare the NRF_SDH_BLE_VS_UUID_COUNT  to match with the number of UUID bases you have. 

  • Addition of 128-bit fails received.

    NRF_SDH_BLE_VS_UUID_COUNT means number of vendor-specific UUIDs.Right?


    where to declare it?and how to use it?

Reply Children
Related