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

Add service while connection

Dears,

I'd like to add service while connection.

Now I am using UART service and want to add HID service when I put button.

How to add HID service while connection?

Could you provide example code?

SDK : nRF5_SDK_14.2.0_17b948a

Base code : ble_app_uart

App : Android

Thank you.

Best Regards,

Joyce

  • Hi,

    It is possible to add a service in an active connection. The central would need to do the service discovery again. So you should notify the central with the sd_ble_gatts_service_changed() after you have added the service. On a button press, you could then call the hids_init()/service_init() and then call the sd_ble_gatts_service_changed. I haven’t tested how the phone behaves when a HID service is suddenly added, so you should test if the phone “activates” the HID functionality after the service is added.

    Since you are using ble_app_uart, the first step should be to combine one of the HID examples (either ble_app_hids_keyboard or ble_app_hids_mouse) with the ble_app_uart examples. After that you can look into adding the button press functionally. For an example on how to add button press interrupt, take a look at the pin_change_int example in the SDK folder SDK_folder\examples\peripheral\pin_change_int.

  • Hi Sigurd, how to calculate start_handle and end_handle in sd_ble_gatts_service_changed . I want to and bas_service to nus_service via button . Thank

  • Hi Giang,

    You could try something like this:

    ret_code_t send_service_changed(uint16_t conn_handle)
    {
        static uint16_t start_handle;
        const  uint16_t end_handle = 0xFFFF;
        ret_code_t err_code;
    
        err_code = sd_ble_gatts_initial_user_handle_get(&start_handle);
    
        if (err_code != NRF_SUCCESS)
        {
            return NRF_ERROR_INTERNAL;
        }
    
        do
        {
            err_code = sd_ble_gatts_service_changed(conn_handle, start_handle, end_handle);
            if (err_code == BLE_ERROR_INVALID_ATTR_HANDLE)
            {
                start_handle += 1;
            }
        } while (err_code == BLE_ERROR_INVALID_ATTR_HANDLE);
    
        return err_code;
    }

Related