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:
    There is no second device, only a second service from the same device.

    Ah, but then you will still need to bond with the second link, if the links are two separate links.

    apdobaj said:
    Here is the code for that:

    Please make sure to check the error codes returned by your calls to sd_be_gatts_sys_attr_get as well.

    apdobaj said:
    If this is the correct resolution, why is sd_ble_gatts_sys_attr_get  returning error 5? Please advise.

    NRF_ERROR_NOT_FOUND means that the variable you are trying to read is not found.

    apdobaj said:
    So I'm trying to fix the 3401 error (attribute missing) when calling sd_ble_gatts_hvx by getting the attributes and then setting them. Here is the code for that:

    I am only seeing get calls here, and no set - if the issue is that these variables has not been set yet you will not be able to get them.
    You will need to add the sd_ble_gatts_sys_attr_set call to your handlng of the BLE_GAP_SYS_ATTR_MISSING event handling.
    Please see this comment, for instance.

    Best regards,
    Karl

  • Ah, but then you will still need to bond with the second link, if the links are two separate links.

    The device is bonded with the smartphone, and the app that handles the custom service connects without intervention, so my conclusion is that it's bonded.

    I am only seeing get calls here, and no set - if the issue is that these variables has not been set yet you will not be able to get them.
    You will need to add the sd_ble_gatts_sys_attr_set call to your handlng of the BLE_GAP_SYS_ATTR_MISSING event handling.

    There are two get commands and one set command. The first get is to obtain the size of the attribute data as I mentioned earlier. And there is no BLE_GAP_SYS_ATTR_MISSING event in SDK 17.1. Can you confirm that the 3401 error would exhibit that behavior (calling send_key_scan_press_release in the HID application runs but the code is never received by the smartphone)? 

  • This line of inquiry might be a red herring. It seems the custom service handler is reaching the BLE_GAP_EVT_CONNECTED event before it's connected. Here's the call stack when breaking on line 278 of ble_cus.c. 

    Could this be interfering with the HID service?

  • I've discovered that with both services running , if the smartphone goes out of BT range, neither of the services reconnect automatically. But if I run the HID service and not the custom one, the reconnect happens as expected and all is good. What might be preventing the HID service to reconnect when the custom service is active? What is supposed to happen during a reconnect with a bonded peripheral following a  reason 8 disconnect?

  • I'm kind of stumped here. I keep getting an NRF_ERROR_NO_MEM when initializing the adv object for one of the services, but from the map file it seems there's plenty of room in RAM. Here's the error message that hardfaults the instant I try to run advertising_init_cus. Debugger output, map file and code snippet attached. 

    Haptic driver interface go! 
    LIS3MDL Sensor interface go! 
    <info> app_timer: RTC: initialized.
    <debug> nrf_sdh: State request: 0x00000000
    <debug> nrf_sdh: Notify observer 0x00044078 => ready
    <debug> nrf_sdh: State change: 0x00000000
    <debug> nrf_sdh: State change: 0x00000001
    <debug> nrf_sdh_ble: RAM starts at 0x20002800
    <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
    <error> app: ERROR 4 [NRF_ERROR_NO_MEM] at C:\Nordic\gestr_GRM_Rev2.0\main.c:3809
    PC at: 0x0002BFB5
    <error> app: End of error report
    
    0624.gestr_GRM_Rev2.0.map
    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_cus) / sizeof(m_adv_uuids_cus[0]);
        init.srdata.uuids_complete.p_uuids  = m_adv_uuids_cus;
    
        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_cus;
        init.error_handler = ble_advertising_error_handler;
    
        err_code = ble_advertising_init(&m_advertising_cus, &init);
        APP_ERROR_CHECK(err_code);
    
    
        ble_advertising_conn_cfg_tag_set(&m_advertising_cus, APP_BLE_CONN_CFG_TAG);
    }

Reply
  • I'm kind of stumped here. I keep getting an NRF_ERROR_NO_MEM when initializing the adv object for one of the services, but from the map file it seems there's plenty of room in RAM. Here's the error message that hardfaults the instant I try to run advertising_init_cus. Debugger output, map file and code snippet attached. 

    Haptic driver interface go! 
    LIS3MDL Sensor interface go! 
    <info> app_timer: RTC: initialized.
    <debug> nrf_sdh: State request: 0x00000000
    <debug> nrf_sdh: Notify observer 0x00044078 => ready
    <debug> nrf_sdh: State change: 0x00000000
    <debug> nrf_sdh: State change: 0x00000001
    <debug> nrf_sdh_ble: RAM starts at 0x20002800
    <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
    <error> app: ERROR 4 [NRF_ERROR_NO_MEM] at C:\Nordic\gestr_GRM_Rev2.0\main.c:3809
    PC at: 0x0002BFB5
    <error> app: End of error report
    
    0624.gestr_GRM_Rev2.0.map
    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_cus) / sizeof(m_adv_uuids_cus[0]);
        init.srdata.uuids_complete.p_uuids  = m_adv_uuids_cus;
    
        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_cus;
        init.error_handler = ble_advertising_error_handler;
    
        err_code = ble_advertising_init(&m_advertising_cus, &init);
        APP_ERROR_CHECK(err_code);
    
    
        ble_advertising_conn_cfg_tag_set(&m_advertising_cus, APP_BLE_CONN_CFG_TAG);
    }

Children
  • I really need to get this figured out, your attention would be most appreciated.

  • Hello,

    Thank you for your patience with this.

    apdobaj said:
    The device is bonded with the smartphone, and the app that handles the custom service connects without intervention, so my conclusion is that it's bonded.

    Could you share the full log from your device when both connections are live?

    apdobaj said:
    I've discovered that with both services running , if the smartphone goes out of BT range, neither of the services reconnect automatically. But if I run the HID service and not the custom one, the reconnect happens as expected and all is good. What might be preventing the HID service to reconnect when the custom service is active? What is supposed to happen during a reconnect with a bonded peripheral following a  reason 8 disconnect?

    Reason 8 is connection time out, which is expected when the peer stops replying (such as if it goes out of range).
    What happens after the disconnection is up to the peripheral device - you could for instance attempt to reconnect quickly by doing high duty cycle directed advertising targeting the peer that was just lost, to attempt to reconnect, or you could start to advertise normally again and see if the central device shows up again.
    You will then need to make sure that your central device is also immediately attempting to reconnect to the lost peer, if you wish for that to happen.

    apdobaj said:
    I'm kind of stumped here. I keep getting an NRF_ERROR_NO_MEM when initializing the adv object for one of the services, but from the map file it seems there's plenty of room in RAM. Here's the error message that hardfaults the instant I try to run advertising_init_cus. Debugger output, map file and code snippet attached. 

    Assuming the NRF_ERROR_NO_MEM is returned by the call to ble_advertising_init, then it likely stems from sd_ble_gap_adv_set_configure which will be returned when there is not enough memory to configure a new advertising set.
    Could you possibly share the full main.c source file (or the file where you set up your advertising) here instead, so that I may see what might be happening here? It would also be great if you could share your sdk_config.h file, so that I may take a look there as well.
    Please let me know if you would like me to make the ticket private before sharing, in case there is any sensitive code.
    A private ticket is only viewable by yourself and the support staff here at Nordic.

    As a side-note you should also change to using the BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE flag if your advertising will timeout and stop (as opposed to using an infinite advertising duration).

    Best regards,
    Karl

  • The no mem issue appears to have been a bad sensor with a stuck interrupt bit. This is very difficult to reproduce in a reliable way, but the two services seem to be coexisting. I did this by getting the HID service to connect and bond first and not starting the custom service advertising until after. But now I can't read the characteristic value in nRF Connect for android, or any other similar app for that matter. I can connect to the server with the app but when I tap refresh services the whirliegig just keeps spinning forever and none of the arrow buttons do anything. Another app told me that notifications were disabled so I added the following to the init structure for the custom service:

        cus_init.custom_value_char_attr_md.cccd_write_perm.sm = 1;
        cus_init.custom_value_char_attr_md.cccd_write_perm.lv = 1;
        cus_init.custom_value_char_attr_md.write_perm.sm = 1;
        cus_init.custom_value_char_attr_md.write_perm.lv = 1;
        cus_init.custom_value_char_attr_md.read_perm.sm = 1;
        cus_init.custom_value_char_attr_md.read_perm.lv = 1;
    	

    The app now reports that notifications are enabled but the services still don't refresh and I still cannot read the characteristic values in nRF connect on Android. And you suggest adding the BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE  flag, how and why would I want to do that? Please advise.

Related