Bluetooth: Peripheral HIDS mouse
The Peripheral HIDS mouse sample demonstrates how to use the GATT Human Interface Device (HID) Service to implement a mouse input device that you can connect to your computer. This sample also shows how to perform directed advertising.
Requirements
The sample supports the following development kits:
Hardware platforms |
PCA |
Board name |
Board target |
---|---|---|---|
PCA10156 |
|
||
PCA10175 |
|
||
PCA10095 |
|
||
PCA10040 |
|
||
PCA10056 |
|
When built for a board target with the */ns
variant, 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.
Note
If you use nRF5340 DK, the additional configuration of the network core will be taken from the child_image
directory.
For more details, see Image-specific variables.
Overview
The sample uses the buttons on a development kit to simulate the movement of a mouse. The four buttons simulate movement to the left, up, right, and down, respectively. Mouse clicks are not simulated.
This sample exposes the HID GATT Service. It uses a report map for a generic mouse.
You can also disable the directed advertising feature by clearing the BT_DIRECTED_ADVERTISING
flag in the application configuration.
This feature is enabled by default and it changes the way how advertising works in comparison to the other Bluetooth® Low Energy samples.
When the device wants to advertise, it starts with high duty cycle directed advertising provided that it has bonding information.
If the timeout occurs, the device starts directed advertising to the next bonded peer.
If all bonding information is used and there is still no connection, the regular advertising starts.
User interface
- Button 1:
Simulate moving the mouse pointer five pixels to the left.
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:
Simulate moving the mouse pointer five pixels up.
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:
Simulate moving the mouse pointer five pixels to the right.
- Button 4:
Simulate moving the mouse pointer five pixels down.
- Button 0:
Simulate moving the mouse pointer five pixels to the left.
When pairing, press this button to confirm the passkey value that is printed on the COM listener to pair with the other device.
- Button 1:
Simulate moving the mouse pointer five pixels up.
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 2:
Simulate moving the mouse pointer five pixels to the right.
- Button 3:
Simulate moving the mouse pointer five pixels down.
Configuration
See Configuring and building an application for information about how to permanently or temporarily change the configuration.
Setup
The HID service specification does not require encryption (CONFIG_BT_HIDS_DEFAULT_PERM_RW_ENCRYPT
), but some systems disconnect from the HID devices that do not support security.
Note
If you want to pair the device with a computer running MacOS, set the CONFIG_BT_HIDS_DEFAULT_PERM_RW_AUTHEN
Kconfig option to y
.
Building and running
To build this sample with the nRF RPC IPC Service transport library on the nRF5340 DK, set the EXTRA_CONF_FILE option to the overlay-nrf_rpc.conf
file.
west build -b nrf5340dk/nrf5340/cpuapp -- -DEXTRA_CONF_FILE=overlay-nrf_rpc.conf
This sample can be found under samples/bluetooth/peripheral_hids_mouse
in the nRF Connect SDK folder structure.
When built as firmware image for a board target with the */ns
variant, 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, follow the instructions in Configuring and building an application for your preferred building environment. See also Programming an application for programming steps and Testing and optimization for general information about testing and debugging in the nRF Connect SDK.
Note
When building repository applications in the SDK repositories, building with sysbuild is enabled by default.
If you work with out-of-tree freestanding applications, you need to manually pass the --sysbuild
parameter to every build command or configure west to always use it.
Note
Programming the nRF54H20 SoC can sometimes fail due to conflicts in the resource configuration. This can happen if, for example, an application programmed to the nRF54H20 SoC configured the UICRs for one or more cores in a way that is incompatible with the configuration required by the application you are trying to program on the SoC.
To fix this error and erase the UICR for the application core, run the following command:
nrfutil device recover --core Application
If your sample also uses the radio core, you must also erase the UICRs for the radio core. To erase the UICR for the radio core, run the following command:
nrfutil device recover --core Network
For more information on the command, run:
nrfutil device recover --help
You can then run west flash
to program your application.
Testing
After programming the sample to your development kit, you can test it either by connecting the development kit as a mouse device to a Microsoft Windows computer or by connecting to it with the Bluetooth Low Energy app of the nRF Connect for Desktop.
Testing with a Microsoft Windows computer
To test with a Microsoft Windows computer that has a Bluetooth radio, complete the following steps:
Power on your development kit.
On your Windows computer, search for Bluetooth devices and connect to the device named “NCS HIDS mouse”.
Push Button 1 on the kit. Observe that the mouse pointer on the computer moves to the left.
Push Button 2. Observe that the mouse pointer on the computer moves upward.
Push Button 3. Observe that the mouse pointer on the computer moves to the right.
Push Button 4. Observe that the mouse pointer on the computer moves downward.
Disconnect the computer from the device by removing the device from the computer’s devices list.
Power on your development kit.
On your Windows computer, search for Bluetooth devices and connect to the device named “NCS HIDS mouse”.
Push Button 0 on the kit. Observe that the mouse pointer on the computer moves to the left.
Push Button 1. Observe that the mouse pointer on the computer moves upward.
Push Button 2. Observe that the mouse pointer on the computer moves to the right.
Push Button 3. Observe that the mouse pointer on the computer moves downward.
Disconnect the computer from the device by removing the device from the computer’s devices list.
Testing with nRF Connect for Desktop
To test with nRF Connect for Desktop, complete the following steps:
Power on your development kit.
Start nRF Connect for Desktop.
Open the Bluetooth Low Energy app.
Connect to the device from the app. The device is advertising as “NCS HIDS mouse”
Optionally, bond to the device. Click the Settings button for the device in the app, select Pair, check Perform Bonding, and click Pair. Optionally check Enable MITM protection to pair with MITM protection and use a button on the device to confirm or reject passkey value.
Click Match in the app. Wait until the bond is established before you continue.
Observe that the services of the connected device are shown.
Click Play for all HID Report characteristics.
Push Button 1 on the kit. Observe that a notification is received on one of the HID Report characteristics, containing the value
FB0F00
.Mouse motion reports contain data with an X-translation and a Y-translation. These are transmitted as 12-bit signed integers. The format used for mouse reports is the following byte array, where LSB/MSB is the least/most significant bit:
[8 LSB (X), 4 LSB (Y) | 4 MSB(X), 8 MSB(Y)]
.Therefore,
FB0F00
denotes an X-translation of FFB = -5 (two’s complement format), which means a movement of five pixels to the left, and a Y-translation of 000 = 0.Push Button 2. Observe that the value
00B0FF
is received on the same HID Report characteristic.Push Button 3. Observe that the value
050000
is received on the same HID Report characteristic.Push Button 4. Observe that the value
005000
is received on the same HID Report characteristic.Disconnect the device in the Bluetooth Low Energy app of nRF Connect for Desktop. Observe that no new notifications are received and the device is advertising.
As bond information is preserved by the Bluetooth Low Energy app, you can immediately reconnect to the device by clicking the Connect button again.
Power on your development kit.
Start nRF Connect for Desktop.
Open the Bluetooth Low Energy app.
Connect to the device from the app. The device is advertising as “NCS HIDS mouse”
Optionally, bond to the device. Click the Settings button for the device in the app, select Pair, check Perform Bonding, and click Pair. Optionally check Enable MITM protection to pair with MITM protection and use a button on the device to confirm or reject passkey value.
Click Match in the app. Wait until the bond is established before you continue.
Observe that the services of the connected device are shown.
Click Play for all HID Report characteristics.
Push Button 0 on the kit. Observe that a notification is received on one of the HID Report characteristics, containing the value
FB0F00
.Mouse motion reports contain data with an X-translation and a Y-translation. These are transmitted as 12-bit signed integers. The format used for mouse reports is the following byte array, where LSB/MSB is the least/most significant bit:
[8 LSB (X), 4 LSB (Y) | 4 MSB(X), 8 MSB(Y)]
.Therefore,
FB0F00
denotes an X-translation of FFB = -5 (two’s complement format), which means a movement of five pixels to the left, and a Y-translation of 000 = 0.Push Button 1. Observe that the value
00B0FF
is received on the same HID Report characteristic.Push Button 2. Observe that the value
050000
is received on the same HID Report characteristic.Push Button 3. Observe that the value
005000
is received on the same HID Report characteristic.Disconnect the device in the Bluetooth Low Energy app of nRF Connect for Desktop. Observe that no new notifications are received and the device is advertising.
As bond information is preserved by the Bluetooth Low Energy app, you can immediately reconnect to the device by clicking the Connect button again.
Dependencies
This sample uses the following nRF Connect SDK libraries:
In addition, it uses the following Zephyr libraries:
include/zephyr/types.h
lib/libc/minimal/include/assert.h
lib/libc/minimal/include/errno.h
include/sys/printk.h
include/sys/byteorder.h
API:
include/bluetooth/bluetooth.h
include/bluetooth/hci.h
include/bluetooth/conn.h
include/bluetooth/uuid.h
include/bluetooth/gatt.h
samples/bluetooth/gatt/bas.h
The sample also uses the following secure firmware component: