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

Can I have multiple but distinct BLE event handlers

I have a multi peripheral sample I am trying to make work

I do a

BLE_NUS_DEF(m_nus) and BLE_HRS_DEF(m_hrs)

this will register two event handlers. 

When an event occurs, say a connection to HRS, I do see the events in both handlers. Is is possible to have only the HRS events to the hrs handler and NUS events to NUS handler ?

Thanks

  • Hello,

    Yes. This is possible, but please be aware that all the events propagate from the same bluetooth event handler. 

    Please see how the ble_app_uart example set up the nus event handler. Please note that it is not enough to set the nus_init.data_handler = nus_data_handler; You also need to manually send the events from inside the ble event handler in ble_nus.c.

    If you are interrested, there is also a tutorial here that I have used in a couple of courses. It is rather long, but you can see in the section "Step 8 - Propagating Custom Service events to the application" how to send the events to a custom service event handler.

    Please note that by default the HRS example doesn't have an event handler in main.c. But it is possible to set it up, if you follow the same method as the nus data handler, or the tutorial from the link.

    Best regards,

    Edvin

  • Thanks Edwin

    When you say "all events propagate from the same BT event handler". Understood.

    But my understanding is all event handlers will get even events from other sources. IE if I have a handler for NUS events and another for HRS events, each handler will get the other's events. Correct ?

  • Depends on how you trigger the custom event handlers. If you look in the ble_app_uart_example, you can see that the BLE_NUS_EVT_RX_DATA event is one event in the nus_data_handler(). However, this event is not defined by the softdevice. The event comes from the BLE_GATTS_EVT_WRITE event in the NUS' ble_handler in ble_nus.c:

    ble_nus_on_ble_evt() -> BLE_GATTS_EVT_WRITE -> on_write() ->

    Then it checks wether the event has the NUS service's service_handle. If that is true, then it will generate the event BLE_NUS_EVT_RX_DATA on line 171-175 (line numbers from SDK16) in ble_nus.c:

            evt.type                  = BLE_NUS_EVT_RX_DATA;
            evt.params.rx_data.p_data = p_evt_write->data;
            evt.params.rx_data.length = p_evt_write->len;
    
            p_nus->data_handler(&evt);

    Please read through the Step 8 on the tutorial. It has a more thorough walkthrough of this procedure.

  • thanks

    I think I am getting closer and I am trying to read and understand. This could help me :

    When you say "Then it checks wether the event has the NUS service's service_handle", what specific code does this ? 

    I *think* this check is done in the softdevice ? Can you confirm how it's done.

    thanks

  • Please open ble_nus.c and look at the on_write() function. Look at where it is called (inside ble_nus_on_ble_evt()), and what it does. It is all in the source code. The event comes from the softdevice, but the checks and the forwarding of the event is all in the code (not the softdevice).

Related