Bluetooth: Central and Peripheral HRS

The Central and Peripheral HRS sample demonstrates how to use Bluetooth® with Central and Peripheral roles concurrently. It also demonstrates how to use the GATT Heart Rate Service (HRS) Client library. It uses the HRS Client to retrieve heart rate measurement data from a remote device that provides a Heart Rate service. It relays this data to another remote device that provides a Heart Rate Service client implementation.

Requirements

The sample supports the following development kits:

Hardware platforms

PCA

Board name

Board target

nRF54L15 PDK

PCA10156

nrf54l15pdk

nrf54l15pdk/nrf54l15/cpuapp

nRF54L15 DK

PCA10156

nrf54l15dk

nrf54l15dk/nrf54l15/cpuapp

nRF54H20 DK

PCA10175

nrf54h20dk

nrf54h20dk/nrf54h20/cpuapp

nRF5340 DK

PCA10095

nrf5340dk

nrf5340dk/nrf5340/cpuapp/ns nrf5340dk/nrf5340/cpuapp

nRF52 DK

PCA10040

nrf52dk

nrf52dk/nrf52832

nRF52840 DK

PCA10056

nrf52840dk

nrf52840dk/nrf52840

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.

To test just the Bluetooth® LE Central Role operation, you need one of the following setups:

  • A smartphone or a tablet running a compatible application.

  • Another development kit running the Heart-rate Monitor (Peripheral) sample. See the documentation for that sample for detailed instructions.

To test the Relay mode operation, you need one of the following setups:

You can also mix devices when testing this sample. For a simple echo test, you only need one additional device. Alternatively, you can use a smartphone providing the HRS functionality and a development kit running the Heart-rate Monitor (Central) sample.

For testing, you can also use nRF Connect for Desktop.

Overview

The sample demonstrates the following Bluetooth® LE roles:

  • Central role - Scans for a remote device providing Heart Rate Service.

  • Peripheral role - Advertises and exposes a Heart Rate Service.

When a device is connected as central, the sample starts the service discovery procedure to search for the Heart Rate Service. If this succeeds, the sample reads the Body Sensor Location characteristic and subscribes to the Heart Rate Measurement characteristic to receive notifications. When connected also as peripheral to the device acting as a Heart Rate Service client, the sample starts working as relay. It collects data from a remote device with Heart Rate Service that is sending notifications and sends this data to another remote device providing a Heart Rate Service client.

User interface

LED 1:

Blinks, toggling on/off every second, when the main loop is running and the device is advertising.

LED 2:

Lit when the development kit is connected as central.

LED 3:

Lit when the development kit is connected as peripheral.

Building and running

This sample can be found under samples/bluetooth/central_and_peripheral_hr 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 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, test it either by connecting to other development kits that are running the Heart-rate Monitor (Peripheral) sample, or by using the Bluetooth Low Energy app from the nRF Connect for Desktop, which emulates an HRS server.

Testing with other development kits

  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. Program the other development kit with the Heart-rate Monitor (Peripheral) sample and reset it.

  3. Wait until the HRS is detected by the central. Observe that LED 2 is on.

  4. In the terminal window, check for information similar to the following:

    Heart Rate Sensor body location: Chest
    Heart Rate Measurement notification received:
    
          Heart Rate Measurement Value Format: 8 - bit
          Sensor Contact detected: 1
          Sensor Contact supported: 1
          Energy Expended present: 0
          RR-Intervals present: 0
    
          Heart Rate Measurement Value: 134 bpm
    

    Notifications will be displayed periodically with a frequency determined by the HR server.

  5. Program another development kit with the Heart-rate Monitor (Central) sample and reset it.

  6. Wait until central is connected to your development kit. Observe that LED 3 is lit.

  7. In terminal windows connected to device with the Heart-rate Monitor (Central) sample, check for information similar to following:

    [NOTIFICATION] data 0x20006779 length 2
    

The sample works now as relay for the Heart Rate Service.

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 development 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.

  6. Click the dongle configuration and select Load setup.

  7. Load the hr_service.ncs file that is located under samples/bluetooth/central_and_peripheral_hr in the nRF Connect SDK folder structure.

  8. Click Apply to device.

  9. Open the CONNECTION MAP tab.

  10. Click the dongle configuration (gear icon) and select Advertising setup.

    The current version of nRF Connect can store the advertising setup.

  11. Click Load setup. Load the hrs_adv_setup.ncs file that is located under samples/bluetooth/central_and_peripheral_hr in the nRF Connect SDK folder structure.

  12. Click Apply and Close.

  13. Click the gear icon to open the Adapter settings and select Start advertising.

  14. Wait until the development kit running the Central and Peripheral HRS connects. Observe that LED 2 is lit.

  15. To explore the Heart Rate Measurement characteristic, complete the following steps:

    1. Write value 06 80 and click the Play button to send a notification. In the terminal window, check for information similar to the following:

      Heart Rate Sensor body location: Chest
      Heart Rate Measurement notification received:
      
            Heart Rate Measurement Value Format: 8 - bit
            Sensor Contact detected: 1
            Sensor Contact supported: 1
            Energy Expended present: 0
            RR-Intervals present: 0
      
            Heart Rate Measurement Value: 128 bpm
      

      The Bluetooth Low Energy app also detects the Central and Peripheral HRS sample Heart Rate Service.

    2. Enable the notification for the Heart Rate Measurement characteristic.

    3. Write again value 06 80 and click the Play button to send a notification.

      The same value appears for the Heart Rate Measurement characteristic.

The sample works now as relay for the Heart Rate Service.

Dependencies

This sample uses the following nRF Connect SDK libraries:

In addition, it uses the following Zephyr libraries:

  • include/zephyr.h

  • API:

    • include/bluetooth/bluetooth.h

    • include/bluetooth/gatt.h

    • include/bluetooth/conn.h

    • include/bluetooth/uuid.h

    • include/bluetooth/services/hrs.h

    • include/bluetooth/services/bas.h

The sample also uses the following secure firmware component: