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 Reply Children
  • I am able to discover all services.

    I want to discover its characteristics and properties.

    As you said i have added sd_ble_gattc_primary_services_discover (), and after that calling characteristics discover()  function but it fails. I think conn_hdle is same for both functions so this issue is coming. 
    I am attaching code here.

    1.Can you help to discover services along with its characteristics?

    2. How to discover and print 128-bit UUID of unknown service.

    Code as below:

    void on_primary_srv_discovery_rsp(ble_db_discovery_t       * p_db_discovery,
                                             ble_gattc_evt_t    const * p_ble_gattc_evt)
    {
        ble_gatt_db_srv_t * p_srv_being_discovered;
        uint16_t count = 0;
    
        uint16_t        bytes_to_copy;
        static uint16_t offset = 0;
        int conn_hdle;
    	conn_hdle = p_ble_gattc_evt->conn_handle;
    
    
        ble_gattc_service_t * const p_service =
                		mp_device_srv[conn_hdle]->services;
    
        p_srv_being_discovered = &(p_db_discovery->services[p_db_discovery->curr_srv_ind]);
    
        if (p_ble_gattc_evt->conn_handle != p_db_discovery->conn_handle)
        {
       // 	return;
        }
    
        if (p_ble_gattc_evt->gatt_status == BLE_GATT_STATUS_SUCCESS)
        {
    
        	//int i = 0;
        	ble_gattc_evt_prim_srvc_disc_rsp_t const * p_prim_srvc_disc_rsp_evt;
            uint32_t err_code;
    
            p_prim_srvc_disc_rsp_evt = &(p_ble_gattc_evt->params.prim_srvc_disc_rsp);
            count = p_prim_srvc_disc_rsp_evt->count;
            NRF_LOG_INFO("service count %d",count);
    
    		// If no more services are found.
    		if ((count != 0) &&
    		(p_ble_gattc_evt->gatt_status == BLE_GATT_STATUS_SUCCESS))
    		{
    			if ((count + offset) > MAX_SERVICE_COUNT)
    			{
    				bytes_to_copy = MAX_SERVICE_COUNT - offset;
    			}
    			else
    			{
    				bytes_to_copy = count;
    			}
    
    			// Save services data.
    			memcpy((p_service + offset),p_prim_srvc_disc_rsp_evt->services,
    			bytes_to_copy * sizeof(ble_gattc_service_t));
    
    			offset += count;
    			for(int i= 0;i <= count; i++ )
    			{
    			p_srv_being_discovered->srv_uuid     = p_prim_srvc_disc_rsp_evt->services[i].uuid;
    			p_srv_being_discovered->handle_range = p_prim_srvc_disc_rsp_evt->services[i].handle_range;
    
    			NRF_LOG_INFO("Found service UUID %x and its start handle:%d , End handle: %d", p_srv_being_discovered->srv_uuid.uuid,
    			p_srv_being_discovered->handle_range.start_handle, p_srv_being_discovered->handle_range.end_handle);
    			}
    			// If the last service has not been reached, this function must be called again with a new start handle.
    			err_code = sd_ble_gattc_primary_services_discover(conn_hdle,
    			p_prim_srvc_disc_rsp_evt->services[count - 1].handle_range.end_handle + 1,
    			NULL);
    			if(err_code == NRF_SUCCESS)
    			{
    			NRF_LOG_INFO("Discovery done successfully..");
    			}
    			else
    			{
    			NRF_LOG_INFO("Discovery Fail..");
    
    			}
    		//  APP_ERROR_CHECK(err_code);
    		}
    
            err_code = characteristics_discover(p_db_discovery, p_ble_gattc_evt->conn_handle);
    
            if (err_code != NRF_SUCCESS)
            {
                p_db_discovery->discovery_in_progress = false;
    
                NRF_LOG_INFO("Error with discovering the characteristics");
                // Indicate the error to the registered user application.
                discovery_error_evt_trigger(p_db_discovery, err_code, p_ble_gattc_evt->conn_handle);
    
                m_pending_user_evts[0].evt.evt_type    = BLE_DB_DISCOVERY_AVAILABLE;
                m_pending_user_evts[0].evt.conn_handle = p_ble_gattc_evt->conn_handle;
            }
        }
        else
        {
            NRF_LOG_INFO("Service UUID 0x%x not found.", p_srv_being_discovered->srv_uuid.uuid);
            // Trigger Service Not Found event to the application.
            discovery_complete_evt_trigger(p_db_discovery, false, p_ble_gattc_evt->conn_handle);
            on_srv_disc_completion(p_db_discovery, p_ble_gattc_evt->conn_handle);
        }
        NRF_LOG_INFO("Services count at end: %d", offset);
         offset = 0;
    }
    

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

Related