Bluetooth: Human Interface Device Service Keyboard
The Bluetooth LE HIDS keyboard sample demonstrates how to use the Human Interface Device Service (HIDS) to implement a keyboard input device that you can connect to your computer.
Requirements
The sample supports the following development kits:
The following board variants do not have DFU capabilities:
S115:
Hardware platform
PCA
Board target
PCA10156
bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice
nRF54L15 DK (emulating nRF54L10)
PCA10156
bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice
nRF54L15 DK (emulating nRF54L05)
PCA10156
bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice
PCA10184
bm_nrf54lm20dk/nrf54lm20a/cpuapp/s115_softdevice
PCA10214
bm_nrf54ls05dk/nrf54ls05b/cpuapp/s115_softdevice
PCA10188
bm_nrf54lv10dk/nrf54lv10a/cpuapp/s115_softdevice
S145:
Hardware platform
PCA
Board target
PCA10156
bm_nrf54l15dk/nrf54l15/cpuapp/s145_softdevice
nRF54L15 DK (emulating nRF54L10)
PCA10156
bm_nrf54l15dk/nrf54l10/cpuapp/s145_softdevice
nRF54L15 DK (emulating nRF54L05)
PCA10156
bm_nrf54l15dk/nrf54l05/cpuapp/s145_softdevice
PCA10184
bm_nrf54lm20dk/nrf54lm20a/cpuapp/s145_softdevice
PCA10214
bm_nrf54ls05dk/nrf54ls05b/cpuapp/s145_softdevice
PCA10188
bm_nrf54lv10dk/nrf54lv10a/cpuapp/s145_softdevice
The following board variants have DFU capabilities:
S115:
Hardware platform
PCA
Board target
PCA10156
bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice/mcuboot
nRF54L15 DK (emulating nRF54L10)
PCA10156
bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice/mcuboot
nRF54L15 DK (emulating nRF54L05)
PCA10156
bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice/mcuboot
PCA10184
bm_nrf54lm20dk/nrf54lm20a/cpuapp/s115_softdevice/mcuboot
PCA10214
bm_nrf54ls05dk/nrf54ls05b/cpuapp/s115_softdevice/mcuboot
PCA10188
bm_nrf54lv10dk/nrf54lv10a/cpuapp/s115_softdevice/mcuboot
S145:
Hardware platform
PCA
Board target
PCA10156
bm_nrf54l15dk/nrf54l15/cpuapp/s145_softdevice/mcuboot
nRF54L15 DK (emulating nRF54L10)
PCA10156
bm_nrf54l15dk/nrf54l10/cpuapp/s145_softdevice/mcuboot
nRF54L15 DK (emulating nRF54L05)
PCA10156
bm_nrf54l15dk/nrf54l05/cpuapp/s145_softdevice/mcuboot
PCA10184
bm_nrf54lm20dk/nrf54lm20a/cpuapp/s145_softdevice/mcuboot
PCA10214
bm_nrf54ls05dk/nrf54ls05b/cpuapp/s145_softdevice/mcuboot
PCA10188
bm_nrf54lv10dk/nrf54lv10a/cpuapp/s145_softdevice/mcuboot
Overview
The sample uses the buttons on the development kit to simulate keys on a keyboard.
One button simulates the letter keys by generating letter keystrokes for a predefined string. A second button simulates the Shift button and shows how to modify the letter keystrokes. An LED displays the Caps Lock state, which can be modified by another connected keyboard.
This sample exposes the HID GATT Service. It uses a report map for a generic keyboard.
Allow list
By default, the sample does not use allow-list advertising, which means that any nearby device can connect and bond with the device. Bonded devices are stored in internal non-volatile memory (NVM) and are remembered across power cycles.
You can enable allow-list advertising by setting the CONFIG_BLE_ADV_USE_ALLOW_LIST Kconfig option to y in the base Kconfig fragment (prj.conf).
When enabled, only previously bonded devices are allowed to reconnect, which allows faster reconnection and prevents unknown devices from connecting.
When allow-list advertising is enabled and you want to add a new bonded device, existing bonds must first be deleted. This can be done through user interaction, as described in the user interface section.
User interface
- Button 0:
When pairing with authentication, press this button to confirm the passkey shown in the COM listener and complete pairing with the other device. See Testing.
- Button 1:
Keep the button pressed while resetting the board to delete bonding information for all peers stored on the device.
When pairing with authentication, press this button to reject the passkey shown in the COM listener to prevent pairing with the other device.
- Button 2:
Simulate the Shift key.
- Button 3:
Send one character of the predefined input (“hello\n”) to the computer.
- LED 0:
Lit when the device is initialized.
- LED 1:
Lit when a device is connected.
- LED 2:
Lit when Caps Lock is active on the computer.
Building and running
This sample can be found under samples/bluetooth/ble_hids_keyboard/ in the Bare Metal folder structure.
For details on how to create, configure, and program a sample, see Getting Started with the samples.
Testing
Compile and program the application.
In the Serial Terminal, using the Serial Terminal app or Visual Studio Code, observe that the
BLE HIDS Keyboard sample initializedmessage is printed.Observe that the
Advertising as nRF_BM_HIDS_KBmessage is printed. You can configure the advertising name using theCONFIG_SAMPLE_BLE_DEVICE_NAMEKconfig option. For information on how to do this, see Configuring Kconfig.On your computer or mobile phone, open the Bluetooth settings and scan for advertising devices. If the device is not advertising, reset the board with the Reset Board option in Visual Studio Code or by pressing the reset button on the development kit.
Connect to your device.
The terminal output in Visual Studio Code indicates
Peer connected.After having connected, your computer or mobile phone may attempt to pair or bond with your device in order to encrypt the link.
You may be prompted to compare or enter a passkey as part of the authentication step. If prompted, provide the passkey from the terminal output, or confirm that the passkey is correct by pressing Button 0 on the kit.
Observe that the device is detected as a keyboard.
Repeatedly press Button 3 on the kit. Every button press sends one character of the test message “hello” to your device (the test message includes a carriage return).
Press Button 2 and hold it while pressing Button 3. Observe that the next letter of the “hello” message appears as a capital letter. This is because Button 2 simulates the Shift key.