Not able to discover services after BLE connection.

Hello All,

I am referring to the ble_app_interactive example and it works fine. I wrote similar functions in my project to scan nearby BLE and connect BLE if it's RSSI goes > -30 then it will connect to that BLE and try to discover the services. There is no authentication in BLE it is simple iBeacon. I can also connect it using nRF Connect app.

But I notice there is a difference in the working / output even though I tried to make it all similar. Here is below function common in both my project as well as in ble_app_interactive project. I have added some NRF_LOG to display to get clear idea about the flow and value.

static void on_primary_srv_discovery_rsp(ble_gattc_evt_t const * p_ble_gattc_evt)
{
    uint16_t        count;
    uint16_t        bytes_to_copy;
    uint16_t        conn_handle;
    static uint16_t offset = 0;
    ret_code_t      err_code;
NRF_LOG_INFO("Inside on_primary_srv_discovery_rsp");
    // For readability.
    ble_gattc_evt_prim_srvc_disc_rsp_t const * p_prim_serv =&(p_ble_gattc_evt->params.prim_srvc_disc_rsp);
    conn_handle = p_ble_gattc_evt->conn_handle;
    ble_gattc_service_t * const p_service =mp_device_srv[conn_handle]->services;

    // Number of discovered services.
    count = p_prim_serv->count;
NRF_LOG_INFO("Service count : %d conn_handle: %d",count,conn_handle);
    // If no more services are found.
    if ((count != 0) && 
        (p_ble_gattc_evt->gatt_status == BLE_GATT_STATUS_SUCCESS))
    {
    NRF_LOG_INFO("inside if");
        if ((count + offset) > 6)
        {
            bytes_to_copy = 6 - offset;
        }
        else
        {
            bytes_to_copy = count;
        }

        // Save services data.
        memcpy((p_service),
               p_prim_serv->services,
               bytes_to_copy * sizeof(ble_gattc_service_t));
NRF_LOG_INFO(" %X ",p_prim_serv->services[0].uuid.uuid);
        offset += count;

        // 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_handle,
            p_prim_serv->services[count - 1].handle_range.end_handle + 1,
            NULL);

        APP_ERROR_CHECK(err_code);
    }
    else
    {
    NRF_LOG_INFO("inside else conn_handle : %d offset : %d ", conn_handle,offset);
        mp_device_srv[conn_handle]->count = offset;

        // If service UUID type is unknown, then look for a 128-bit UUID.
        // Only the first one is searched for here, the rest is searched for in the @ref on_read_rsp.
        for (uint8_t i = 0; i < offset; i++)
        {

        NRF_LOG_INFO("Inside for counter : %d start_handle : %d",i,p_service[i].handle_range.start_handle);
            if (p_service[i].uuid.type == BLE_UUID_TYPE_UNKNOWN)
            {
            NRF_LOG_INFO("Inside BLE_UUID_TYPE_UNKNOWN");
                m_vendor_uuid_read = true;
                // Read service 128-bit UUID.
                err_code = sd_ble_gattc_read(conn_handle, p_service[i].handle_range.start_handle, 0);
                APP_ERROR_CHECK(err_code);
                offset = 0;

                return;
            }
        }

        NRF_LOG_INFO("Services count: %d", offset);
        uuid_print(p_ble_gattc_evt->conn_handle, p_service);

        offset = 0;
    }
}

In app_ble_interactive project I get below output

now here is the output from the project

If you notice there is start_handle : 2689 due that I guess there is error.

After checking line by line.  I guess in my project There is problem below code

// Save services data.
        memcpy((p_service),
               p_prim_serv->services,
               bytes_to_copy * sizeof(ble_gattc_service_t));

Because of above It is not able to save and hence not possible to fetch data displayed like in ble_app_interactive project.

Please help me, What I am doing wrong?

Thanks in advance.

Regards,

Neeraj Dhekale

Parents Reply Children
No Data
Related