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

  • To complement my question, inside the battery service (ble_bas.c) has the same function and works fine too.

    void ble_bas_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
    {
        if ((p_context == NULL) || (p_ble_evt == NULL))
        {
            return;
        }

        ble_bas_t * p_bas = (ble_bas_t *)p_context;

        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GATTS_EVT_WRITE:
                on_write(p_bas, p_ble_evt);
                break;

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

  • 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

Related