Two services on a single device

Here's what we're trying to do. We have a gesture recognition wearable that is intended to be used as a remote control for a smartphone. But some smartphone functions are easier to implement using the HID service and some using a custom service. We have the wearable and the phone connected and both services are shown in nRF Connect but if we pair with the HID service first we cannot connect to the custom service using a custom app on the smartphone. Similarly, if we pair to the custom service first, we cannot use the HID service. How do we get both services to work simultaneously from one device?

Parents
  • Hello,

    We have the wearable and the phone connected and both services are shown in nRF Connect but if we pair with the HID service first we cannot connect to the custom service using a custom app on the smartphone. Similarly, if we pair to the custom service first, we cannot use the HID service. How do we get both services to work simultaneously from one device?

    When you say 'pair with the HID service first', do you here mean that you have 2 different advertising payloads that you are using, and that you are connecting to one which contains the HID service UUID, or the other which contains the custom service UUID?
    If so, please note that the contents of the advertising payload does not matter for which services are exposed during a connection - once the connection is established the central will perform a discovery which then finds all services offered by the peripheral device.

    When you connect to your device, are you able to see both the HID service and the customer service?

    If so, I think the issue you are seeing stems from the 'control' of the BLE link in the smartphone side. I.e the OS could have control over the BLE link and use it as a HID device, or the custom app could take control over the BLE link (within the smartphone) to use the custom service. The OS will usually only allow a specific subset of known services, such as HID devices, but not custom ones, so the challenge here will perhaps be to find a way to be able to access the BLE connection both from the OS side (for use as HID) and from your custom smartphone app (to access the custom service).

    Best regards,
    Karl

  • Would it be possible to establish the HID connection first using the OS settings dialog and then advertise for the custom service? This is obviously not ideal, but would be a short-term fix.

    No, not using 2 different advertising payloads, Here is the UUID array:

    static ble_uuid_t m_adv_uuids[] = {{BLE_UUID_HUMAN_INTERFACE_DEVICE_SERVICE, BLE_UUID_TYPE_BLE},
                                       {CUSTOM_SERVICE_UUID, BLE_UUID_TYPE_VENDOR_BEGIN}}; 

    I use this for the m_advertising structure. I can see both services with nRF Connect app.

  • When I set the peripheral and total link counts to 3 the code compiles but then immediately hardfaults when run, screenshot attached. i have DEBUG in the preprocessor defines but the debug terminal has nothing and the call stack only extends as far as the gcc error handler (app_error_handler_gcc.c) which tells me nothing useful.

    With the link count set to 2, and advertising initialized to include both uuid's, if I pair the app first the advertising stops and so there is no way to connect to the HID service. It's only when I separate the advertising into two that I can attempt connecting to both services, but then the other service disconnects. See the code snippets.

    static ble_uuid_t m_adv_uuids_cushid[] =  {{BLE_UUID_HUMAN_INTERFACE_DEVICE_SERVICE, BLE_UUID_TYPE_BLE},
                                            {CUSTOM_SERVICE_UUID, BLE_UUID_TYPE_VENDOR_BEGIN}}; 
    static ble_uuid_t m_adv_uuids_cus[] = {CUSTOM_SERVICE_UUID, BLE_UUID_TYPE_VENDOR_BEGIN};
    
    static ble_uuid_t m_adv_uuids_hid[] = {BLE_UUID_HUMAN_INTERFACE_DEVICE_SERVICE, BLE_UUID_TYPE_BLE};
    
    This shows the formulation of the ble_uuid_t struct for both, just custom and just hid. 

    static void advertising_init_cus(void)
    {
        uint32_t               err_code;
        uint8_t                adv_flags;
        ble_advertising_init_t init;
        //apdobaj move the uuid's to the scan response packet to advertise the whole name
        ble_advdata_t sr_data;
        memset(&sr_data, 0, sizeof(sr_data));
    
        memset(&init, 0, sizeof(init));
    
        adv_flags                            = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
        init.advdata.name_type               = BLE_ADVDATA_FULL_NAME;
        init.advdata.include_appearance      = false;
        //apdobaj change flags per HRS example
        init.advdata.flags                   = adv_flags;
        //init.advdata.flags                   = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
        init.srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids_cushid) / sizeof(m_adv_uuids_cushid[0]);
        init.srdata.uuids_complete.p_uuids  = m_adv_uuids_cushid;
    
        init.config.ble_adv_whitelist_enabled          = false; //apdobaj debug note setting
        init.config.ble_adv_directed_high_duty_enabled = false;
        // try connecting to the most recent connection
        init.config.ble_adv_directed_enabled           = false;
        init.config.ble_adv_directed_interval          = APP_ADV_DIRECTED_INTERVAL;
        init.config.ble_adv_directed_timeout           = APP_ADV_DIRECTED_DURATION;
        init.config.ble_adv_fast_enabled               = true;
        init.config.ble_adv_fast_interval              = APP_ADV_FAST_INTERVAL;
        init.config.ble_adv_fast_timeout               = APP_ADV_FAST_DURATION;
        init.config.ble_adv_slow_enabled               = true;
        init.config.ble_adv_slow_interval              = APP_ADV_SLOW_INTERVAL;
        init.config.ble_adv_slow_timeout               = APP_ADV_SLOW_DURATION;
    
        init.evt_handler   = on_adv_evt;
        init.error_handler = ble_advertising_error_handler;
    
        err_code = ble_advertising_init(&m_advertising, &init);
        APP_ERROR_CHECK(err_code);
    
    
        ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
    }

    And here is the init routine that uses those uuid structs. the only thing I'm changing in this is the two lines involving the uuid, in this code snippet lines 18 and 19.

  • Hello,

    apdobaj said:
    i have DEBUG in the preprocessor defines but the debug terminal has nothing and the call stack only extends as far as the gcc error handler (app_error_handler_gcc.c) which tells me nothing useful.

    Which logger backend have you enabled? The debug terminal will only show RTT loggings, and so if you are using the UART backend for the logger you will need to open a serial monitor to view the logs.

    apdobaj said:
    I separate the advertising into two that I can attempt connecting to both services, but then the other service disconnects. See the code snippets.

    Which disconnection reason is given on the first connection when it disconnects?

    Best regards,
    Karl

  • OK, I got the phone to accept both services (HID and custom) by making sure that both get bonded. But now, only the custom service works, not HID. I traced sending a keycode over the HID interface all the way back to the BLE_GATTS_EVT_HVN_TX_COMPLETE event in the ble_evt_handler method with no errors. This event handler calls buffer_dequeue and this does throw an error:

        if (BUFFER_LIST_EMPTY())
        {
            err_code = NRF_ERROR_NOT_FOUND;
        }
    

    This appears to be where execution ends. What does this mean?

  • However, the same thing happens when the HID interface is working (ie, the custom is not advertised).

  • The custom service is still being disconnected when pairing to the HID service, reason is 19. 

Reply Children
  • I'm trying to increase the link count to 3 but I get an NRF_ERROR_NOMEM when enabling the BLE stack. I've tried increasing the stack and heap but that doesn't help, and ram_start is set to it's lowest value given the soft device.

  • Hello,

    apdobaj said:
    I'm trying to increase the link count to 3 but I get an NRF_ERROR_NOMEM when enabling the BLE stack. I've tried increasing the stack and heap but that doesn't help, and ram_start is set to it's lowest value given the soft device.

    Could you share your full logger output when this happens?
    Were you able to see the logs when you changed the logger backend as I mentioned in my previous comment?

    apdobaj said:
    The custom service is still being disconnected when pairing to the HID service, reason is 19. 

    Could you copy the full error message?

    Best regards,
    Karl

  • Logger output and offending code for no mem hardfault:

    <info> app_timer: RTC: initialized.
    <error> app: ERROR 4 [NRF_ERROR_NO_MEM] at C:\Nordic\gestr_GRM_Rev2.0\main.c:3565
    PC at: 0x0002C263
    <error> app: End of error report

        // Enable BLE stack.
        err_code = nrf_sdh_ble_enable(&ram_start);
        APP_ERROR_CHECK(err_code);
    

    Logger output when trying to exercise both HID and custom (link count set to 2 to avoid the nomem crash):

    Haptic driver interface go!
    LIS3MDL Sensor interface go!
    <info> app_timer: RTC: initialized.
    <debug> nrf_sdh: State request: 0x00000000
    <debug> nrf_sdh: Notify observer 0x00044604 => ready
    <debug> nrf_sdh: State change: 0x00000000
    <debug> nrf_sdh: State change: 0x00000001
    <debug> nrf_sdh_ble: RAM starts at 0x20002BE0
    <debug> nrf_sdh_ble: RAM start location can be adjusted to 0x20002990.
    <debug> nrf_sdh_ble: RAM size for application can be adjusted to 0x3D670.
    <info> ble_bms: Init complete.
    <info> app: HID Keyboard example started.
    <info> app: Fast advertising.
    <debug> nrf_sdh_ble: BLE event: 0x10.
    <info> app: Connected
    <debug> nrf_sdh_ble: BLE event: 0x14.
    <debug> nrf_sdh_ble: BLE event: 0x1A.
    <debug> nrf_sdh_ble: BLE event: 0x50.
    <info> app: on_write: len 2, p_evt_write->data[0]: 1, p_evt_write->data[1]: 0.
    <debug> nrf_sdh_ble: BLE event: 0x50.
    <debug> nrf_sdh_ble: BLE event: 0x12.
    gesture: 3
    <debug> nrf_sdh_ble: BLE event: 0x57.
    gesture: 3
    <debug> nrf_sdh_ble: BLE event: 0x57.
    gesture: 7
    <debug> nrf_sdh_ble: BLE event: 0x57.
    gesture: 2

    gesture: 2

    Note that when exercising the custom interface I get a BLE event 0x57, but no event when exercising the HID interface. Here is the code that goes with that log:

            switch (finalGesture) {
              case L2R:
                //ble_cus_custom_value_update(&m_cus, gestr_cus_key_array[volUp]);
                keys_send( 1, gestr_hid_key_array+finalGesture );
                break;
              case R2L:
                //ble_cus_custom_value_update(&m_cus, gestr_cus_key_array[volDown]);
                keys_send( 1, gestr_hid_key_array+finalGesture );
                break;
              case U2D:
                ble_cus_custom_value_update(&m_cus, gestr_cus_key_array[mediaPP1]);
                break;
              case D2U:
                 //ble_cus_custom_value_update(&m_cus, gestr_cus_key_array[mediaNext]);
                 keys_send( 1, gestr_hid_key_array+D2U );
                break;
    

    In this case, gestures 3 and 7 run the custom service, 2 runs the HID interface. Here is a screenshot of the nRF Connect view of this connection. I have 4 services running, could it be that only two are enabled because of the link count setting?

  • Hello,

    Thank you for the update.

    apdobaj said:
    <debug> nrf_sdh_ble: RAM start location can be adjusted to 0x20002990.
    <debug> nrf_sdh_ble: RAM size for application can be adjusted to 0x3D670.

    Please make sure to adjust your flash placement macros for the project to match the suggestion from the nrf_sdh_ble module.

    apdobaj said:
    I have 4 services running, could it be that only two are enabled because of the link count setting?

    No, the link could should not affect the number of enabled services.
    Have you tried connecting to your peripheral from 2 different centrals, and seeing that all the services work as intended then?
    I expect that there could be some issue on the smartphone side to 'share' the link between the OS and your custom application still, and so this would be a good test to verify that the peripheral is working as intended.

    Best regards,
    Karl

  • OK, I got both services to run at the same time by bonding the HID connection and then establishing that first (advertise the HID interface only), and then starting the custom service advertising only after the HID interface is connected in ble_evt_handler. However, when I do this the HID interface doesn't work because I get an attribute missing error 3401 when running sd_ble_gatts_hvx in the ble_hids_inp_rep_send subroutine in the file ble_hids.c. The code compiles and runs without error, but the values are never sent to the smartphone. I've tried restoring the sys attributes using sd_ble_gatts_sys_attr_get and sd_ble_gatts_sys_attr_set but that hasn't worked. Here is the code for the "Connected" case: 

        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GAP_EVT_CONNECTED:
                NRF_LOG_INFO("Connected");
                err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
                APP_ERROR_CHECK(err_code);
                m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
                err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
                APP_ERROR_CHECK(err_code);
                //apdobaj 27Jun2023 set tx power
                err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_CONN, m_conn_handle, +8);
                APP_ERROR_CHECK(err_code);
                attrs = sd_ble_gatts_sys_attr_get(m_conn_handle, p_attr_data, p_len, BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS);
                err_code = sd_ble_gatts_sys_attr_get(m_conn_handle, p_attr_data, *p_len, BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS);
                //apdobaj connect HID first then custom
                nrf_delay_ms(2);
    #if defined(USE_CUSTOM_SERVICE)
                advertising_init_cus();
                advertising_start_cus();
    #endif
    
                break;

Related