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.

  • Hello,

    apdobaj said:
    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.

    Both services will be exposed to both centrals at each discovery, but yes - from a BLE standpoint you could have two equal connections to the same device (so long as your smartphone OS permits this, which I personally dont have much insight into), which will expose both services to both connections, but where each of the connections could primarily make use of one service each.

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

    Then I did not understand what you meant when you said 'pair with HID first' earlier, could you elaborate on this?

    Best regards,
    Karl

  • Sure. When the wearable is advertising you can pair with it using either the smartphone's "settings" dialog or with the custom app. Either way, the wearable stops advertising after pairing, which is expected behavior. But if you pair with the settings dialog, the HID interface works but the custom interface does not, and vice versa. 

  • Hello,

    Aha, I understand - thank you for clarifying!
    In either case you can give it a try to pair to the device both to your OS as a HID device (through the smartphone 'settings'), and to your custom application concurrently and see if that works.

    apdobaj said:
    Either way, the wearable stops advertising after pairing, which is expected behavior.

    The device will automatically stop advertising once it has hit its maximum for concurrent connections. If you increase the max concurrent connections you can continue to advertise after the first connection is made, and then try to connect from you custom app at the same time. Please note that I do not have any experience developing a smartphone application, and so I do not know if the OS of your smartphone will allow two separate connections to the same device. I dont see any reason why it should not, but I have been surprised of similar limitations earlier, and so I am cautions in making assumptions that it will work right off the bat.

    Best regards,
    Karl

  • OK Karl good stuff, but I need some clarification. First, how do you get more "concurrent connections"? Is it the #define NRF_SDH_BLE_PERIPHERAL_LINK_COUNT, or NRF_SDH_BLE_CENTRAL_LINK_COUNT, or both? Should the number assigned to NRF_SDH_BLE_TOTAL_LINK_COUNT be the first two added together? When I set NRF_SDH_BLE_PERIPHERAL_LINK_COUNT to 3, NRF_SDH_BLE_CENTRAL_LINK_COUNT to zero and NRF_SDH_BLE_TOTAL_LINK_COUNT to 3 I get a hard fault (I now have a custom service, HID over GATT, BMS, and BAS). When set to 2, it runs but it doesn't advertise both services. When they advertise concurrently, which "name" do they use? Please share the details.

Reply
  • OK Karl good stuff, but I need some clarification. First, how do you get more "concurrent connections"? Is it the #define NRF_SDH_BLE_PERIPHERAL_LINK_COUNT, or NRF_SDH_BLE_CENTRAL_LINK_COUNT, or both? Should the number assigned to NRF_SDH_BLE_TOTAL_LINK_COUNT be the first two added together? When I set NRF_SDH_BLE_PERIPHERAL_LINK_COUNT to 3, NRF_SDH_BLE_CENTRAL_LINK_COUNT to zero and NRF_SDH_BLE_TOTAL_LINK_COUNT to 3 I get a hard fault (I now have a custom service, HID over GATT, BMS, and BAS). When set to 2, it runs but it doesn't advertise both services. When they advertise concurrently, which "name" do they use? Please share the details.

Children
  • Hey Karl, you there? I've gotten to the point where I advertise for each service separately by first pairing with the custom service (advertising only with that uuid) and then setting a flag when that connection is established in the custom service event handler. When that flag is detected (it is polled in a timer ISR) then the HID advertising is initialized with its uuid and started. Then I can see the device name in the system settings connected devices dialog and I pair to the HID service. Unfortunately, this causes the custom service to disconnect immediately. This method obviously doesn't work, so what will work? The device name is set in the GAP settings so I'm presuming this cannot be changed per service. Please advise.

  • Hello,

    Thank you for your patience with this.

    apdobaj said:
    and NRF_SDH_BLE_TOTAL_LINK_COUNT to 3 I get a hard fault (I now have a custom service, HID over GATT, BMS, and BAS). When set to 2, it runs but it doesn't advertise both services. When they advertise concurrently, which "name" do they use?

    You will need to adjust the total- and the peripheral link-count. Since your device will not be working as a central the centra link-count can be 0. The total link count should be the sum of the central and peripheral link-count, correct.

    What hardfault do you get when you change these settings? Does the BLE init return an error?
    Please make sure to have DEBUG defined in your preprocessor defines, like shown in the included image, since this will make the logger output a detailed error message whenever a non-NRF_SUCCESS error code is passed to an APP_ERROR_CHECK.

    apdobaj said:
    When set to 2, it runs but it doesn't advertise both services. When they advertise concurrently, which "name" do they use?

    The advertising payload will not change at all when you change the max link-count. Could you clarify what you mean when you say that it does not advertise both services? The advertising will be the same in both cases - i.e the only difference will be that it keeps advertising after the first connection. The second central will connect to the same advertisement as the first central connected to.

    Best regards,
    Karl

  • 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?

Related