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.

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

  • On both the client and on the server you would need to define the number of vendor specific base UUID you planning to have so that the softdevice can reserve the memory for that.  You define that value in sdk_config.h file. Please look for NRF_SDH_BLE_VS_UUID_COUNT. 

  • ok.
     I am facing issue here.in on_primary_srv_discovery_rsp() function. It causes many issues ahead.
    if (p_ble_gattc_evt->conn_handle != p_db_discovery->conn_handle)
    {

    NRF_LOG_INFO("p_ble_gattc_evt->conn_handle: %d, p_db_discovery->conn_handle: %d",p_ble_gattc_evt->conn_handle,p_db_discovery->conn_handle);
     return;
    }

    Code return from here.

    terminal on output as below:

    <info> ble_db_disc: p_ble_gattc_evt->conn_handle: 0, p_db_discovery->conn_handle: 65535

    Can you please help me with this?

Reply
  • ok.
     I am facing issue here.in on_primary_srv_discovery_rsp() function. It causes many issues ahead.
    if (p_ble_gattc_evt->conn_handle != p_db_discovery->conn_handle)
    {

    NRF_LOG_INFO("p_ble_gattc_evt->conn_handle: %d, p_db_discovery->conn_handle: %d",p_ble_gattc_evt->conn_handle,p_db_discovery->conn_handle);
     return;
    }

    Code return from here.

    terminal on output as below:

    <info> ble_db_disc: p_ble_gattc_evt->conn_handle: 0, p_db_discovery->conn_handle: 65535

    Can you please help me with this?

Children
Related