This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problems with function for handling ble events

Hi,

I'm using ble_app_template to implement my custom solution. I can read battery values, but I can't read custom values.

As a reference, I'm using ble_app_cscs, and the function below works fine.

ble_cscs.c

void ble_cscs_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
{
    ble_cscs_t * p_cscs = (ble_cscs_t *)p_context;

    if (p_cscs == NULL || p_ble_evt == NULL)
    {
        return;
    }

    ble_sc_ctrlpt_on_ble_evt(&(p_cscs->ctrl_pt), p_ble_evt);
    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            on_connect(p_cscs, p_ble_evt);
            break;

        case BLE_GAP_EVT_DISCONNECTED:
            on_disconnect(p_cscs, p_ble_evt);
            break;

        case BLE_GATTS_EVT_WRITE:
            on_write(p_cscs, p_ble_evt);
            break;

        default:
            // No implementation needed.
            break;
    }
}

In my code, I made the same function, but the value of machine state is 0 and never enter in the first case to send data do my application.

void ble_cps_on_ble_evt(ble_cps_t * p_cps, ble_evt_t * p_ble_evt)
{
 
    switch (p_ble_evt->header.evt_id)
    {
    case BLE_GAP_EVT_CONNECTED:
        on_connect(p_cps, p_ble_evt);
        break;

    case BLE_GAP_EVT_DISCONNECTED:
        on_disconnect(p_cps, p_ble_evt);
        break;

    case BLE_GATTS_EVT_WRITE:
        on_write(p_cps, p_ble_evt);
        break;

    default:
        // No implementation needed.
        break;
    }
}

I using nrf52 pca10040 dev board and sdk 17.0.2.

Can you help me to solve this?

Regards

Parents
  • Hi there,

    Can you set a breakpoint at the beginning of ble_cps_on_ble_evt() before the case BLE_GAP_EVT_CONNECTED line and confirm that the ble event handler is called. 

    regards

    Jared 

  • Hi Jared,

    I set the breakpoint and modified my code to debug and the event handler was called, but the evt_id return 0.

    void ble_cps_on_ble_evt(ble_cps_t * p_cps, ble_evt_t * p_ble_evt)
    {
        
        NRF_LOG_INFO("ble_cps_on_ble_evt!! valor de p_ble_evt->header.evt_id =%u, ", p_ble_evt->header.evt_id);//todo debug
        
        switch (p_ble_evt->header.evt_id)
        {
        case BLE_GAP_EVT_CONNECTED:
            on_connect(p_cps, p_ble_evt);
            NRF_LOG_INFO("ble_cps_on_ble_evt: BLE_GAP_EVT_CONNECTED ");//todo debug
            break;

        case BLE_GAP_EVT_DISCONNECTED:
            on_disconnect(p_cps, p_ble_evt);
            NRF_LOG_INFO("ble_cps_on_ble_evt: BLE_GAP_EVT_DISCONNECTED ");//todo debug
            break;

        case BLE_GATTS_EVT_WRITE:
            on_write(p_cps, p_ble_evt);
            NRF_LOG_INFO("ble_cps_on_ble_evt: BLE_GATTS_EVT_WRITE ");//todo debug
            break;

        default:
            // No implementation needed.
             NRF_LOG_INFO("ble_cps_on_ble_evt: NENHUM!!! ");//todo debug
            break;
        }
    }

    I cannot idenfitify wich part of the code the this value is changed, because the ble_evt_handler function in main.c works fine to connect and I can see the services in nrf connect app without values

  • Hi,

    Great, I can clearly see from your screenshot that it enters the ble event handler, but neither it or the log shows any CONNECTED event? 

    Laerte Junior said:
    because the ble_evt_handler function in main.c works fine to connect and I can see the services in nrf connect app without values

    It seems that you determine that you are connected because you're able to the see the services provided by the peripheral? Can you verify it by moving the breakpoint inside the BLE_GAP_EVT_CONNECTED switch case and see if it enters it?

    regards

    Jared 

  • Hi Jared,

    I tried it several times finding the point of my code that set this value. I ran the code again to show you with the breakpoint inside the case and never entered.

    Regards

    Laerte

Reply Children
  • Hi,

    Ok, so most likely the device never connects. You''ve probably put the UUID of your custom service inside the advertising packet which explains why you are able to see it. What are you using on the other side to scan the advertising packets? A mobile phone?

    regards

    Jared 

  • Hi Jared

    My custom service connect, because I put the correct UUID.

    I'm using nrf connect app to see the services.

    I can't read the measurement values of my service. Analyzing my code, the function  ble_cps_measurement_send below was never called.

    uint32_t ble_cps_measurement_send(ble_cps_t * p_cps, ble_cps_meas_t * p_measurement)
    {
        uint32_t err_code;
        //NRF_LOG_INFO("Entrou no ble_cps_measurement_send ");//todo debug
        //NRF_LOG_INFO("Valor de p_cps->conn_handle = %u ", p_cps->conn_handle);//todo debug
        // Send value if connected and notifying
        if (p_cps->conn_handle != BLE_CONN_HANDLE_INVALID)
        {
            uint8_t                encoded_cp_meas[MAX_CPM_LEN];
            uint16_t               len;
            uint16_t               hvx_len;
            ble_gatts_hvx_params_t hvx_params;

            len     = cp_measurement_encode(p_cps, p_measurement, encoded_cp_meas);
            hvx_len = len;        // Length = 20 Bytes!!!!

            memset(&hvx_params, 0, sizeof(hvx_params));

            hvx_params.handle   = p_cps->meas_handles.value_handle;
            hvx_params.type     = BLE_GATT_HVX_NOTIFICATION;
            hvx_params.offset   = 0;
            hvx_params.p_len    = &hvx_len;
            hvx_params.p_data   = encoded_cp_meas;

            err_code = sd_ble_gatts_hvx(p_cps->conn_handle, &hvx_params);
            if ((err_code == NRF_SUCCESS) && (hvx_len != len))
            {
                err_code = NRF_ERROR_DATA_SIZE;
            }
            //NRF_LOG_INFO("ble_cps_measurement_send ");//todo debug
            //NRF_LOG_INFO("O que foi enviado no bluetooth = %u ", hvx_params.p_data);//todo debug
            
        }
        else
        {
            err_code = NRF_ERROR_INVALID_STATE;
        }
        //NRF_LOG_FLUSH();
        return err_code;
    }

    I made my code comparing with cscs service in ble_cscs.c and the measures was showed correctly in nrf connect app

    uint32_t ble_cscs_measurement_send(ble_cscs_t * p_cscs, ble_cscs_meas_t * p_measurement)
    {
        if (p_cscs == NULL || p_measurement == NULL)
        {
            return NRF_ERROR_NULL;
        }

        uint32_t err_code;
        NRF_LOG_INFO("Valor de p_cscs->conn_handle = %u ", p_cscs->conn_handle);//todo debug
        //NRF_LOG_INFO("Entrou no ble_cps_measurement_send ");//todo debug
        // Send value if connected and notifying
        if (p_cscs->conn_handle != BLE_CONN_HANDLE_INVALID)
        {
            uint8_t                encoded_csc_meas[MAX_CSCM_LEN];
            uint16_t               len;
            uint16_t               hvx_len;
            ble_gatts_hvx_params_t hvx_params;

            len     = csc_measurement_encode(p_cscs, p_measurement, encoded_csc_meas);
            hvx_len = len;

            memset(&hvx_params, 0, sizeof(hvx_params));

            hvx_params.handle = p_cscs->meas_handles.value_handle;
            hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
            hvx_params.offset = 0;
            hvx_params.p_len  = &hvx_len;
            hvx_params.p_data = encoded_csc_meas;

            err_code = sd_ble_gatts_hvx(p_cscs->conn_handle, &hvx_params);
            if ((err_code == NRF_SUCCESS) && (hvx_len != len))
            {
                err_code = NRF_ERROR_DATA_SIZE;
            }
            NRF_LOG_INFO("O que foi enviado no bluetooth = %u ", &hvx_params.p_data);//todo debug
        }
        else
        {
            err_code = NRF_ERROR_INVALID_STATE;
        }

        return err_code;
    }

    Regards

    Laerte

  • Hi,

    What parameters are you passing to NRF_SDH_BLE_OBSERVER() when you are registering the event handler ?

     
    regards
    Jared 
  • Hi,

    I'm passing the parameters according the function below

    static void ble_stack_init(void)
    {
        ret_code_t err_code;

        err_code = nrf_sdh_enable_request();
        APP_ERROR_CHECK(err_code);

        // Configure the BLE stack using the default settings.
        // Fetch the start address of the application RAM.
        uint32_t ram_start = 0;
        err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
        APP_ERROR_CHECK(err_code);

        // Enable BLE stack.
        err_code = nrf_sdh_ble_enable(&ram_start);
        APP_ERROR_CHECK(err_code);

        // Register a handler for BLE events.
        NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
    }

    Regards

    Laerte

  • Hi,

    Can you share a project that would reproduce this on the nRF52 development kit?

    regards

    Jared 

Related