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?

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

  • 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

Related