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

S130 experimental example - discovery problem

I am trying to modify s130 experimental app example so that my board could connect to any peripheral and discover all of its services. As peripheral I use "ble_app_multilink" because it has custom UUID service.

Devices can connect, but problem occurs during service discovery. After discovery of standard ble services (1800, 1801) central should find custom service. Despite of it it finds "nothing" - UUID is 0, and change "start_handle" to 1. Then it discover all services again, this time finds custom service but also change start_handle to 1.

This is screen from s130 app console: image description

And this is "discovery" code (the part with HEART_RATE is useless, also I will change variable "service_found"), I took part of that code from here :

   /* Service discovery */
handle_range.start_handle = 0x0001;
handle_range.end_handle = BLE_GATTC_HANDLE_END;
do
{
    if ((error_code = sd_ble_gattc_primary_services_discover(gs_peripheral[peripheral_id].conn_handle, handle_range.start_handle, NULL)) != NRF_SUCCESS)
    {
        LOG_DEBUG("(Peripheral %i) Service discovery: error code 0x%4x", peripheral_id, error_code);
        return error_code;
    }
    if ((error_code = event_handle(BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP, 2000, gs_evt_buf, sizeof(gs_evt_buf))) != NRF_SUCCESS)
    {
        LOG_DEBUG("(Peripheral %i) service discovery response: error code 0x%4x", peripheral_id, error_code);
        return error_code;
    }
    if (gsp_ble_evt->evt.gattc_evt.gatt_status != BLE_GATT_STATUS_SUCCESS)
    {
        LOG_DEBUG("(Peripheral %i) Service discovery response: status code 0x%4x", peripheral_id, error_code);
        return NRF_ERROR_NOT_FOUND;
    }
			
			uint8_t services_number = gsp_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.count;
			LOG_DEBUG("Number of services: %d", gsp_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.count);
    for (i = 0; i < services_number; i++)
    {
				
				LOG_DEBUG("UUID type: 0x%x", gsp_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[i].uuid.type);
				LOG_DEBUG("UUID value: 0x%16x", gsp_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[i].uuid.uuid);
				LOG_DEBUG("i= %d", i);
				if (gsp_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[i].uuid.type !=1) {
// Unknown 128-bit UUID => read handle again to get full answer.
					LOG_DEBUG("New unknown UUID found");
error_code = sd_ble_gattc_read(gs_peripheral[peripheral_id].conn_handle, gsp_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[i].handle_range.start_handle, 0);
if (error_code != NRF_SUCCESS) {
		LOG_DEBUG("Error code: 0x%4x", error_code);
    return error_code;
}
error_code = event_handle(BLE_GATTC_EVT_READ_RSP,2000, gs_evt_buf, sizeof(gs_evt_buf));
if (error_code != NRF_SUCCESS) {
    return error_code;
}

// Response should contain full 128-bit UUID.
uint8_t * rsp_data    = (uint8_t *)gsp_ble_evt->evt.gattc_evt.params.read_rsp.data;
uint8_t rsp_data_len  = gsp_ble_evt->evt.gattc_evt.params.read_rsp.len;
if (rsp_data_len == 16) {
    // Mask 16-bit UUID part to zeros.
    rsp_data[12] = 0x00;
    rsp_data[13] = 0x00;
		
		ble_uuid128_t base_uuid;
		uint8_t uuid_type = gsp_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[i].uuid.type;
		
		

    // Copy gathered 128bit UUID as future base.
    memcpy(base_uuid.uuid128, rsp_data, 16);
    error_code = sd_ble_uuid_vs_add((const ble_uuid128_t *)&base_uuid, &uuid_type);
    APP_ERROR_CHECK(error_code);
		
}

}

        if ((gsp_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[i].uuid.uuid == HEART_RATE_SERVICE )
            && (gsp_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[i].uuid.type == BLE_UUID_TYPE_BLE))
        {
            LOG_DEBUG("Service found UUID 0x%04X", gsp_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[i].uuid.uuid);
            handle_range.start_handle = gsp_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[i].handle_range.start_handle;
            if (gsp_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.count > i + 1)
            {
                handle_range.end_handle = gsp_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[i + 1].handle_range.start_handle - 1;
            }
            service_found = 1;
            break;
        }
    }
    if (service_found == 0)
    {		LOG_DEBUG("Start handle = 0x%4x", handle_range.start_handle);
					LOG_DEBUG("End handle = 0x%4x", gsp_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[gsp_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.count-1 ].handle_range.end_handle);
        handle_range.start_handle = gsp_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[gsp_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.count-1 ].handle_range.end_handle+0x1;
					LOG_DEBUG("Start handle = 0x%4x", handle_range.start_handle);
			}
} while ((service_found == 0) && (gsp_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.count > 0));

if (service_found == 0)
{
    LOG_DEBUG("ISSUE (Peripheral %i) Service not found", peripheral_id);
    return NRF_ERROR_NOT_FOUND;
}

Does anyone know what is wrong here? Thank you for answers.

Parents Reply Children
No Data
Related