This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Discovery getting BLE_DB_DISCOVERY_SRV_NOT_FOUND

Hi everybody,

I am designing a beacon that works with s130 and can act as peripheral or central. This beacon has two custom services, and can connect to two different devices as a central:

  • It can connect to another equal beacon
  • It can connect to another only peripheral device that has its custom service

In order to know which device I am connecting to I check the advertising data. If I find the only peripheral device, I can connect to it and discovery its custom service perfectly. The problem comes when I try to connect to another equal beacon device. I can connect, but when I try to discover services, I always obtain BLE_DB_DISCOVERY_SRV_NOT_FOUND event.

This code is based on the ble_app_hrs_rscs_relay SDK11 example. I use nRF51822. Also, when I try to discover the services using nRF Control Panel, I get all the services correctly of both devices (with correct UUID).

I attach here some of the code, to help to check what is wrong:

This is the part where I connect to one of the two devices. The global val connection_reason tells which is the device.

static void on_ble_central_evt(const ble_evt_t * const p_ble_evt)
{
    const ble_gap_evt_t   * const p_gap_evt = &p_ble_evt->evt.gap_evt;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
        {
            uint32_t err_code;

			if(connection_reason==CONNECTION_REASON_INTERNAL_ALERT || connection_reason==CONNECTION_REASON_EXTERNAL_ALERT)	baliza_state |= 1 << STATE_CONNECTED_TO_PULSERA;
			else if(connection_reason==CONNECTION_REASON_UWB_PACKET_PENDING) baliza_state |= 1 << STATE_CONNECTED_TO_BALIZA_SLAVE;
			
			setBalizaState(baliza_state);	
			
            err_code = ble_db_discovery_start(&m_ble_db_discovery[p_gap_evt->conn_handle], p_gap_evt->conn_handle);
            APP_ERROR_CHECK(err_code);
        } break; // BLE_GAP_EVT_CONNECTED

        case BLE_GAP_EVT_DISCONNECTED:
        {	
			baliza_state &= ~(1 << STATE_CONNECTED_TO_PULSERA);
			setBalizaState(baliza_state);	
			
			// Start scanning
            scan_start();	
        } break; // BLE_GAP_EVT_DISCONNECTED

        case BLE_GAP_EVT_ADV_REPORT:
        {
            uint32_t err_code;
		
			// Advertising packet has enough length and we are not already connected
			if(p_gap_evt->params.adv_report.dlen>=11)
			{
					if(p_gap_evt->params.adv_report.data[PULSERA_ADV_BYTES_CUSTOM1]==0x01 && p_gap_evt->params.adv_report.data[PULSERA_ADV_BYTES_CUSTOM2]==0x01)
					{
						
								// Initiate connection.
								connection_reason=CONNECTION_REASON_EXTERNAL_ALERT;
								err_code = sd_ble_gap_connect(&p_gap_evt->params.adv_report.peer_addr,
														  &m_scan_param,
														  &m_connection_param);
							
						
					}
				
				
					if(p_gap_evt->params.adv_report.data[BALIZA_ADV_BYTES_CUSTOM1]==0x01 && p_gap_evt->params.adv_report.data[BALIZA_ADV_BYTES_CUSTOM2]==0x03)
					{

						{
							// Initiate connection.
							connection_reason=CONNECTION_REASON_UWB_PACKET_PENDING;
							err_code = sd_ble_gap_connect(&p_gap_evt->params.adv_report.peer_addr,
													  &m_scan_param,
													  &m_connection_param);
						}

					}
				
			}
        } break;
(...)		// BLE_GAP_ADV_REPORT
	}
}

Then I have the discovery handler, that is correctly called:

static void db_disc_handler(ble_db_discovery_evt_t * p_evt)
{
	if(connection_reason==CONNECTION_REASON_EXTERNAL_ALERT) ble_pulsera_on_db_disc_evt(&m_ble_pulsera_c, p_evt); //Baliza!! descobrim la caracteristica desitjada
	else if(connection_reason==CONNECTION_REASON_UWB_PACKET_PENDING) ble_balizaslave_on_db_disc_evt(&m_ble_pulsera_c, p_evt);
}

Finally, here they are the two functions that discover the services. The first one, corresponding to the first device, will get inside the "if", but the second one will get always the BLE_DB_DISCOVERY_SRV_NOT_FOUND and therefore not entering the "if".

#define BLE_UUID_PULSERA_SERVICE 0xFF00
#define PULSERA_INTERNAL_ALARM_UUID 0xFF03
#define PULSERA_ACK_UUID 0xFF04
#define PULSERA_EXTERNAL_ALERT_UUID 0xFF05
void ble_pulsera_on_db_disc_evt(ble_pulsera_c_t * p_ble_pulsera_c, const ble_db_discovery_evt_t * p_evt)
{
    // Check if the Heart Rate Service was discovered.
    if (p_evt->evt_type == BLE_DB_DISCOVERY_COMPLETE &&
        p_evt->params.discovered_db.srv_uuid.uuid == BLE_UUID_PULSERA_SERVICE &&
        p_evt->params.discovered_db.srv_uuid.type == BLE_UUID_TYPE_BLE)
    {
        // Find the CCCD Handle of the Heart Rate Measurement characteristic.
        uint32_t i;

        ble_pulsera_c_evt_t evt;

        evt.evt_type    = BLE_PULSERA_C_EVT_DISCOVERY_COMPLETE;
        evt.conn_handle = p_evt->conn_handle;

        for (i = 0; i < p_evt->params.discovered_db.char_count; i++)
        {
            if (p_evt->params.discovered_db.charateristics[i].characteristic.uuid.uuid ==
                PULSERA_INTERNAL_ALARM_UUID)
            {
                // Found Heart Rate characteristic. Store CCCD handle and break.
                evt.params.peer_db.hrm_cccd_handle =
                    p_evt->params.discovered_db.charateristics[i].cccd_handle;
                evt.params.peer_db.hrm_handle =
                    p_evt->params.discovered_db.charateristics[i].characteristic.handle_value;
                //break;
            }
						if (p_evt->params.discovered_db.charateristics[i].characteristic.uuid.uuid ==
                PULSERA_ACK_UUID)
            {
                // Found Heart Rate characteristic. Store CCCD handle and break.

                evt.params.peer_db.ack_handle =
                    p_evt->params.discovered_db.charateristics[i].characteristic.handle_value;
                //break;
            }
			
									if (p_evt->params.discovered_db.charateristics[i].characteristic.uuid.uuid ==
                PULSERA_EXTERNAL_ALERT_UUID)
            {
                // Found Heart Rate characteristic. Store CCCD handle and break.

                evt.params.peer_db.external_alert_handle =
                    p_evt->params.discovered_db.charateristics[i].characteristic.handle_value;
                //break;
            }
        }

        LOG("[PULSERA_C]: Heart Rate Service discovered at peer.\r\n");
        //If the instance has been assigned prior to db_discovery, assign the db_handles
        if(p_ble_pulsera_c->conn_handle != BLE_CONN_HANDLE_INVALID)
        {
            if ((p_ble_pulsera_c->peer_pulsera_db.hrm_cccd_handle == BLE_GATT_HANDLE_INVALID)&&
                (p_ble_pulsera_c->peer_pulsera_db.hrm_handle == BLE_GATT_HANDLE_INVALID))
            {
                p_ble_pulsera_c->peer_pulsera_db = evt.params.peer_db;
            }
        }


        p_ble_pulsera_c->evt_handler(p_ble_pulsera_c, &evt);
    }
}

#define BLE_UUID_BALIZA_SERVICE 0xFE00
#define BALIZA_STATE_UUID 0xFE01
#define BALIZA_ACK_UUID 0xFE05
#define BALIZA_UWB_INFO_UUID 0xFE02
void ble_balizaslave_on_db_disc_evt(ble_pulsera_c_t * p_ble_pulsera_c, const ble_db_discovery_evt_t * p_evt)
{
    // Check if the Heart Rate Service was discovered.
    if (p_evt->evt_type == BLE_DB_DISCOVERY_COMPLETE &&
        p_evt->params.discovered_db.srv_uuid.uuid == BLE_UUID_BALIZA_SERVICE &&
        p_evt->params.discovered_db.srv_uuid.type == BLE_UUID_TYPE_BLE)
    {
        // Find the CCCD Handle of the Heart Rate Measurement characteristic.
        uint32_t i;

        ble_pulsera_c_evt_t evt;

        evt.evt_type    = BLE_PULSERA_C_EVT_DISCOVERY_COMPLETE;
        evt.conn_handle = p_evt->conn_handle;

        for (i = 0; i < p_evt->params.discovered_db.char_count; i++)
        {
            if (p_evt->params.discovered_db.charateristics[i].characteristic.uuid.uuid ==
                BALIZA_STATE_UUID)
            {
                // Found Heart Rate characteristic. Store CCCD handle and break.
                evt.params.peer_db.baliza_state_handle =
                    p_evt->params.discovered_db.charateristics[i].characteristic.handle_value;
                //break;
            }
			if (p_evt->params.discovered_db.charateristics[i].characteristic.uuid.uuid ==
                BALIZA_ACK_UUID)
            {
                // Found Heart Rate characteristic. Store CCCD handle and break.

                evt.params.peer_db.baliza_ack_handle =
                    p_evt->params.discovered_db.charateristics[i].characteristic.handle_value;
                //break;
            }
			
			if (p_evt->params.discovered_db.charateristics[i].characteristic.uuid.uuid ==
                BALIZA_UWB_INFO_UUID)
            {
                // Found Heart Rate characteristic. Store CCCD handle and break.

                evt.params.peer_db.baliza_uwb_info_handle =
                    p_evt->params.discovered_db.charateristics[i].characteristic.handle_value;
                //break;
            }
        }

        LOG("[PULSERA_C]: Heart Rate Service discovered at peer.\r\n");
        //If the instance has been assigned prior to db_discovery, assign the db_handles
        if(p_ble_pulsera_c->conn_handle != BLE_CONN_HANDLE_INVALID)
        {
            if ((p_ble_pulsera_c->peer_pulsera_db.hrm_cccd_handle == BLE_GATT_HANDLE_INVALID)&&
                (p_ble_pulsera_c->peer_pulsera_db.hrm_handle == BLE_GATT_HANDLE_INVALID))
            {
                p_ble_pulsera_c->peer_pulsera_db = evt.params.peer_db;
            }
        }


        p_ble_pulsera_c->evt_handler(p_ble_pulsera_c, &evt);
    }
}

I hope someone can find this out.

Thank you

Related