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

  • Thanks. Agreed. Correct me where I am wrong

    What you described is when something comes FROM central. I see a BLE_GATTS_EVT_WRITE come in and get sent down to ble_nus_on_ble_evt.

    onWrite gets called and a check of two handles occurs. I believe the SD provides the service handle of the event and it's compared to the service handle obtained during nus_init. If they match the event is propogated to the app. 

    In the opposite direction, when I send data from the peripheral, a connection handle and "service handle obtained during nus_init" are passed to sd_ble_gatts_hvx. I suspect a check done there by getting the service handles from the connection handle and comparing them to "service handle obstained during NUS init". If OK, generates a HVN_TX_COMPLETE. Otherwise fails in sd_ble_gatts_hvx. So this is done is the SD. I believe

    If I am correct, the key is having the service handles associated to the connection handle to do the checks. These service handles are there for some events (like a WRITE) but not other (on connect) for example.

    Correct ?

  • marcag said:
    I believe the SD provides the service handle of the event and it's compared to the service handle obtained during nus_init. If they match the event is propogated to the app. 

     Correct.

     

    marcag said:
    In the opposite direction, when I send data from the peripheral, a connection handle and "service handle obtained during nus_init" are passed to sd_ble_gatts_hvx. I suspect a check done there by getting the service handles from the connection handle and comparing them to "service handle obstained during NUS init". If OK, generates a HVN_TX_COMPLETE.

     Also correct. Just be aware that BLE_GATTS_EVT_HVN_TX_COMPLETE will be issued after (!) a message is ACKed by the central. You can queue up several packets using sd_ble_gatts_hvx(), as long as it returns NRF_SUCCESS. If it return NRF_ERROR_RESOURCES, it means that the sending queue is full, and you must wait for a TX_COMPLETE in order to queue the next packet. Please note that the TX_COMPLETE doesn't belong to a specific service. The BLE_GATTS_EVT_HVN_TX_COMPLETE events are general, telling you that a message was ACKed. But then again, all the services share the same queue.

    I believe this is why the BLE_NUS_EVT_TX_RDY isn't really used in main.c in this example. It is probably more common to use the BLE_GATTS_EVT_HVN_TX_COMPLETE event directly in main.c, but it is up to you.

    Best regards,

    Edvin

  • thanks Edvin, your help has been greatly appreciated !!!

Related