SDK 14 - How to set up a BLE Observer for custom service

bheestand gravatar image

asked 2017-08-11 22:28:37 +0200

updated 2017-08-14 22:56:42 +0200

NRF52DK (PCA10040) - SDK14 - Arm GCC (using Eclipse) - S132 5.0.0 Soft Device

I had a custom service set up in SDK13 using the tutorial here.

I am migrating my project to SDK14. I have everything else completely working except the BLE service. I can see the custom service and characteristics associated with it fine. I can make changes to the values of the characteristics but they are not getting passed to the "on_write" function of my custom service like it did before with SDK13. Also not working are any changes made on the board that were sent via BLE to the characteristics in my custom service (using the our_xxxxxx_characteristic_update function in my custom service per the tutorial).

I believe it is because the new SoftDevice handler implementation. Before I had the following code under -

static void ble_evt_dispatch(ble_evt_t * p_ble_evt)

nrf_ble_gatt_on_ble_evt(&m_gatt, p_ble_evt);
ble_our_service_on_ble_evt(&m_our_service, p_ble_evt);

The last one was for my custom service.

That code is not in my current program on SDK 14 because I know that it has changed per the SDK migration document. The new way is to use BLE stack event observers. I am confused on how to implement the new BLE stack event observers with my custom service.

I am building off of the peripheral, ble_app_hrs example. There is code in there under ble_stack_init that says:

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

What do I need to do to get my custom service "handled" in this new SDK?

I am sure I am missing something seemingly simple but any help would be greatly appreciated!

edit retag flag offensive close delete report spam

1 answer

Sort by ยป oldest newest most voted
hnhoan gravatar image

answered 2017-08-14 18:45:20 +0200

updated 2017-08-14 23:01:43 +0200

I started migrating too. Have not fully tested but you can try this.

void ble_our_service_on_ble_evt(ble_evt_t *p_ble_evt, void *p_context)
    ble_os_t * our_service = (ble_os_t *)p_context;


// Register a handler for BLE events.
NRF_SDH_BLE_OBSERVER(m_our_service_observer, APP_BLE_OBSERVER_PRIO, ble_our_service_on_ble_evt, (void*)&m_our_service);
edit flag offensive delete publish link more


I can't seem to get it to work with your code above. A ton of errors come up.

This was the code that was in my static void ble_evt_dispatch(ble_evt_t * p_ble_evt) on the previous SDK (that worked):

ble_our_service_on_ble_evt(&m_our_service, p_ble_evt);

This was the working code for void ble_our_service_on_ble_evt(ble_os_t * p_our_service, ble_evt_t * p_ble_evt)

        case BLE_GATTS_EVT_WRITE:
        on_ble_write(p_our_service, p_ble_evt);
        p_our_service->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
        p_our_service->conn_handle = BLE_CONN_HANDLE_INVALID;
        // No implementation needed.

Any help would be appreciated!

Bryan H ( 2017-08-14 22:45:42 +0200 )editconvert to answer

change your

void ble_our_service_on_ble_evt(ble_os_t * p_our_service, ble_evt_t * p_ble_evt)


void ble_our_service_on_ble_evt(ble_evt_t const *p_ble_evt, void *p_context)
    ble_os_t * p_our_service = (ble_os_t *)p_context;

Nguyen Hoan Hoang ( 2017-08-14 23:04:51 +0200 )editconvert to answer

I am now getting an error: ../../../main.c:1733:73: error: initialization from incompatible pointer type [-Werror=incompatible-pointer-types]

NRF_SDH_BLE_OBSERVER(m_our_service_observer, APP_BLE_OBSERVER_PRIO, ble_our_service_on_ble_evt, (void*)&m_our_service);

It is indicating the error is with : ble_our_service_on_ble_evt

Also says right below that error:

c:\sdk14.0\components\softdevice\common\nrf_sdh_ble.h:89:18: note: in definition of macro 'NRF_SDH_BLE_OBSERVER'

 .handler   = _handler,                                                                          \


How can I fix that error?

Bryan H ( 2017-08-15 16:38:23 +0200 )editconvert to answer

I edited previous comment to add const in the declaration

Nguyen Hoan Hoang ( 2017-08-15 17:36:04 +0200 )editconvert to answer

Now I am getting:

../../../our_service.c: In function 'ble_our_service_on_ble_evt':

../../../our_service.c:105:41: error: passing argument 2 of 'on_ble_write' discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]

         on_ble_write(p_our_service, p_ble_evt);

I fixed that by adding const to void on_ble_write...

static void on_ble_write(ble_os_t * p_our_service, ble_evt_t  const * p_ble_evt)

Thank you sooooooo much for your help!

It works perfectly now!!!!

I owe you big time!!!!

Bryan H ( 2017-08-15 17:55:10 +0200 )editconvert to answer

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer. Do not ask a new question or reply to an answer here.

[hide preview]

User menu

    or sign up

Recent questions

Question Tools



Asked: 2017-08-11 22:28:37 +0200

Seen: 231 times

Last updated: aug. 14