Bluetooth: Central HIDS

The Central HIDS sample demonstrates how to use the GATT Human Interface Device Service (HIDS) Client to interact with a HIDS server. Basically, the sample simulates a computer that connects to a mouse or a keyboard.

Requirements

The sample supports the following development kits:

Hardware platforms

PCA

Board name

Build target

nRF5340 DK

PCA10095

nrf5340dk_nrf5340

nrf5340dk_nrf5340_cpuapp_ns nrf5340dk_nrf5340_cpuapp

nRF52 DK

PCA10040

nrf52dk_nrf52832

nrf52dk_nrf52832

nRF52840 DK

PCA10056

nrf52840dk_nrf52840

nrf52840dk_nrf52840

When built for an _ns build target, the sample is configured to compile and run as a non-secure application with Cortex-M Security Extensions enabled. Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.

The sample also requires a HIDS device to connect with (for example, another development kit running the Bluetooth: Peripheral HIDS mouse sample or Bluetooth: Peripheral HIDS keyboard sample, or a computer with a Bluetooth® Low Energy dongle and nRF Connect for Desktop).

Overview

The sample scans available devices, searching for a HIDS server. If a HIDS server is found, the sample connects to it and discovers all characteristics.

If any input reports are detected, the sample subscribes to them to receive notifications. If any boot reports are detected, the behavior depends on if they are boot mouse reports or boot keyboard reports:

  • If a boot mouse report is detected, the sample subscribes to it.

  • If a boot keyboard report is detected, the sample subscribes to its input report, and the sample functionality of changing the CAPSLOCK LED is enabled (Button 1 and 3).

User interface

Button 1:

Toggle the CAPSLOCK LED on the connected keyboard using Write without response. This function is available only if the connected keyboard is set to work in Boot Protocol Mode.

When pairing, press this button to confirm the passkey value that is printed on the COM listener to pair with the other device.

Button 2:

Switch between Boot Protocol Mode and Report Protocol Mode. This function is available only if the connected peer supports the Protocol Mode Characteristic.

When pairing, press this button to reject the passkey value that is printed on the COM listener to prevent pairing with the other device.

Button 3:

Toggle the CAPSLOCK LED on the connected keyboard using Write with response. This function is available only if the connected HID has boot keyboard reports. It always writes CAPSLOCK information to the boot report, even if Report Protocol Mode is selected.

Building and Running

This sample can be found under samples/bluetooth/central_hids in the nRF Connect SDK folder structure.

When built as firmware image for the _ns build target, the sample has Cortex-M Security Extensions (CMSE) enabled and separates the firmware between Non-Secure Processing Environment (NSPE) and Secure Processing Environment (SPE). Because of this, it automatically includes the Trusted Firmware-M (TF-M). To read more about CMSE, see Processing environments.

To build the sample with Visual Studio Code, follow the steps listed on the How to build an application page in the nRF Connect for VS Code extension documentation. See Configuring and building an application for other building scenarios, Programming an application for programming steps, and Testing and optimization for general information about testing and debugging in the nRF Connect SDK.

Testing

After programming the sample to your development kit, you can test it either by connecting to another development kit that is running the Bluetooth: Peripheral HIDS keyboard sample, or by using nRF Connect for Desktop that emulates a HIDS server.

Testing with another development kit

  1. Connect to the kit that runs this sample with a terminal emulator (for example, nRF Connect Serial Terminal). See Testing and optimization for the required settings and steps.

  2. Reset the kit.

  3. Program the other kit with the Bluetooth: Peripheral HIDS keyboard sample and reset it.

  4. When connected, press Button 1 on both devices to confirm the passkey value used for bonding, or press Button 2 to reject it.

  5. Wait until the HIDS keyboard is detected by the central. All detected descriptors are listed. In the terminal window, check for information similar to the following:

    HIDS is ready to work
    Subscribe to report id: 1
    Subscribe to boot keyboard report
    
  6. Press Button 1 and Button 2 one after another on the kit that runs the keyboard sample and observe the notification values in the terminal window. See Bluetooth: Peripheral HIDS keyboard for the expected values:

    Notification, id: 0, size: 8, data: 0x0 0x0 0xb 0x0 0x0 0x0 0x0 0x0
    Notification, id: 0, size: 8, data: 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
    
  7. Press Button 2 on the kit that runs the Central HIDS sample and observe that the protocol mode is updated into boot mode:

    Setting protocol mode: BOOT
    
  8. Press Button 1 and Button 2 one after another on the kit that runs the keyboard sample and observe the notification of the boot report values:

    Notification, keyboard boot, size: 8, data: 0x0 0x0 0xf 0x0 0x0 0x0 0x0 0x0
    Notification, keyboard boot, size: 8, data: 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
    
  9. Press Button 1 and Button 3 one after another on the Central HIDS kit and observe that LED 1 on the keyboard kit changes its state. The following information is also displayed in the terminal window.

    If Button 1 was pressed:

    Caps lock send (val: 0x2)
    Caps lock sent
    

    If Button 3 was pressed:

    Caps lock send using write with response (val: 0x2)
    Capslock write result: 0
    Received data (size: 1, data[0]: 0x2)
    

Testing with nRF Connect for Desktop

  1. Connect to the kit that runs this sample with a terminal emulator (for example, nRF Connect Serial Terminal). See Testing and optimization for the required settings and steps.

  2. Reset the kit.

  3. Start nRF Connect for Desktop.

  4. Open the Bluetooth Low Energy app and select the connected dongle that is used for communication.

  5. Open the SERVER SETUP tab. Click the dongle configuration and select Load setup. Load the hids_keyboard.ncs file that is located under samples/bluetooth/central_hids in the nRF Connect SDK folder structure.

  6. Click Apply to device.

  7. Open the CONNECTION MAP tab. Click the dongle configuration and select Advertising setup.

    The current version of nRF Connect cannot store the advertising setup, so it must be configured manually. See the following image for the required target configuration:

    Advertising setup for HIDS keyboard simulator

    Advertising setup for HIDS keyboard simulator

    Complete the following steps to configure the advertising setup:

    1. Delete the default Complete local name from Advertising data.

    2. Add a Custom AD type with AD type value set to 19 and Value set to 03c1. This is the GAP Appearance advertising data.

    3. Add a Custom AD type with AD type value set to 01 and Value set to 06. This is the AD data with “General Discoverable” and “BR/EDR not supported” flags set.

    4. Add a UUID 16 bit complete list with two comma-separated values: 1812 and 180F. These are the values for HIDS and BAS.

    5. Add a Complete local name of your choice to the Scan response data.

    6. Click Apply and Close.

  8. In the Adapter settings, select Start advertising.

  9. Wait until the kit that runs the Central HIDS sample connects. All detected descriptors are listed. Check for information similar to the following:

    HIDS is ready to work
    Subscribe in report id: 1
    Subscribe in boot keyboard report
    
  10. Explore the first report inside Human Interface Device (the one with eight values). Change any of the values and note that the kit logs the change.

  11. Press Button 2 on the kit and observe that the Protocol Mode value changes from 01 to 00.

  12. Press Button 1 and Button 3 one after another and observe that the Boot Keyboard Output Report value toggles between 00 and 02.

Dependencies

This sample uses the following nRF Connect SDK libraries:

In addition, it uses the following Zephyr libraries:

  • include/sys/byteorder.h

  • include/zephyr/types.h

  • lib/libc/minimal/include/errno.h

  • include/sys/printk.h

  • Bluetooth APIs:

    • include/bluetooth/bluetooth.h

    • include/bluetooth/gatt.h

    • include/bluetooth/hci.h

    • include/bluetooth/conn.h

    • include/bluetooth/uuid.h

The sample also uses the following secure firmware component: