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

Adding a Custom service to the HID keyboard service [Advertising Issue]

Hi,

We're trying to add a custom service to the HID keyboard example. the service is initialized we can see the service when connected to the device from the nrfConnect app. but not in the adverting packet. we only see the HID service !

When using our phone app, in where we scan for a specific service UUID (custom service) the app can't find the device, but when scanning for hid service, the app can find it.

static ble_uuid_t m_adv_uuids[] = {

   {BLE_UUID_HUMAN_INTERFACE_DEVICE_SERVICE, BLE_UUID_TYPE_BLE}
  // {SM_SERVICE_UUID, BLE_UUID_TYPE_VENDOR_BEGIN}
              
};

using only the Custom service in the m_adv_uuids[], makes the ble_advertising_init(&m_advertising, &init);  return and error ( INVALID PARAMETER ) .

/**@brief Function for initializing the Advertising functionality.
 */
static void advertising_init(void)
{
    uint32_t               err_code;
    uint8_t                adv_flags;
    ble_advertising_init_t init;

    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      = true;
    init.advdata.flags                   = adv_flags;
    init.advdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
    init.advdata.uuids_complete.p_uuids  = m_adv_uuids;

    //int8_t tx_power                      = -4;// Set Power Level
    //init.advdata.p_tx_power_level        = &tx_power;

    init.config.ble_adv_whitelist_enabled          = false;
    init.config.ble_adv_directed_high_duty_enabled = true;
    init.config.ble_adv_directed_enabled           = false;
    init.config.ble_adv_directed_interval          = 0;
    init.config.ble_adv_directed_timeout           = 0;
    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               = false;
    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);
}

note: we did set the  NRF_SDH_BLE_VS_UUID_COUNT 1  , in the sdk_config file !

What we need to achieve, is to have that Custom service advertised. and not affecting the the HID service, meaning the host will still can see this device as a Hid device !

Thanks in advance,

best regards,

Abdelali

  • Hi

    Have you defined the SM_SERVICE_UUID anywhere? It is not included in the UUID_SERVICES definitions in the ble_srv_commons.h header, where all the other UUID services (like BLE_UUID_HUMAN_INTERFACE_DEVICE_SERVICE, etc.)  are defined. I'm assuming this is what is the invalid parameter you're seeing as an error. You can try defining the service as any of the existing ones to see if that gets rid of this error.

    You can also check out this custom_ble_service_example, originally made by one of my colleagues, but having been migrated to SDK v.15.3.0 by a member of the community. This example should explain in detail how you can include your custom service UUID into the advertisement.

    Best regards,

    Simon

  • Hi,

    Indeed, the service is properly declared, and works as intended. the problem is it is not advertised, when i scan the device with the nrfConnect phone app, and i look at the advertised packet, i son't see that service (i only see the HID service..), but the service is available and functions properly after connecting !

    The service works good as the hid service, but the issue here is we want that custom service to be advertised, so when we scan with our app for this UUID (SM_SERVICE_UUID), the device should appear. not, for when we use a hid UUID to scan for our device, as any other nearby ble hid devices will also appear in the app.

    Best regards,

    Abdelali

  • Hi Abdelali

    I see. What is happening here is that you don't have room for both service UUIDs in your advertising packet. The SM_SERVICE_UUID is 128 bit (16 bytes) long, which means it takes up more than half of the advertising packet, which is 31 byte, where 2 are overhead by default. In order to add this custom service, you should fit it into the scan response data instead of the advertising packet. You can take a look at I.E. the BLE app blinky example to see how a scan response is added to your advertising.

    Best regards,

    Simon

  • Hi,

    Ok, thank you! will give it a try.

    Best Regards,

    Abdelali

Related