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

How to add an event handler for service?

Hi,

I'm studying the example code of "ble_app_blinky_pca10040_s132 ". 

What confused me is I couldn't find how the "led_write_handler" is added to the service. There is a line 

// Initialize LBS.
init.led_write_handler = led_write_handler;

in main.c (line 302), and it's passed to ble_lbs_init() (in ble_lbs.c), then in ble_lbs_init(), the handler pointer is set to p_lbs:

// Initialize service structure.
p_lbs->led_write_handler = p_lbs_init->led_write_handler;

(line 198),  but then I couldn't find this pointer is passed to anywhere, how does it work?

More generally, if I want to add a custom service to GATT, to my understanding I need to call several SoftDevice APIs:

1. sd_ble_uuid_vs_add() to add the UUID to the stack's UUID table.

2. sd_ble_gatts_service_add() to add service

3. sd_ble_gatts_characteristic_add() to add characteristics for this service

There is no parameter of these functions that needs a event handler pointer, then if I need a function to handle the write event to the characteristic value, what should I do? Did I missed something?

Thanks.

Parents
  • Hi!

     

    // Initialize service structure.
    p_lbs->led_write_handler = p_lbs_init->led_write_handler;

    Here the member called "led_write_handler" from the struct that p_lbs points to is set to the "led_write_handler" of our service init structure, and so it is added to the service. The pointer p_lbs points to m_lbs in main.c since we called:

        err_code = ble_lbs_init(&m_lbs, &init);

    in line 302.

    Do this answer your question?

    If you want to add custom services and characteristics I recommend to first take a look at our Bluetooth Low Energy tutorials for how to add Services and Characteristics.

    Best Regards,

    Marjeris

Reply
  • Hi!

     

    // Initialize service structure.
    p_lbs->led_write_handler = p_lbs_init->led_write_handler;

    Here the member called "led_write_handler" from the struct that p_lbs points to is set to the "led_write_handler" of our service init structure, and so it is added to the service. The pointer p_lbs points to m_lbs in main.c since we called:

        err_code = ble_lbs_init(&m_lbs, &init);

    in line 302.

    Do this answer your question?

    If you want to add custom services and characteristics I recommend to first take a look at our Bluetooth Low Energy tutorials for how to add Services and Characteristics.

    Best Regards,

    Marjeris

Children
Related