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.

Overview

The sample demonstrates both 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.

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

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 Bluetooth: Peripheral HR 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 Bluetooth: Central / Heart-rate Monitor sample.

User interface

LED 1:

Blinks when the main loop is running (that is, the device is advertising) with a period of two seconds, duty cycle 50%.

LED 2:

On when the development kit is connected as central.

LED 3:

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

See Building and programming an application for information about how to build and program the application.

Testing

After programming the sample to your development kit, test it either by connecting to other development kits that are running the Bluetooth: Peripheral HR sample, or by using the nRF Connect for Desktop Bluetooth Low Energy application that emulates an HRS server.

Testing with other development kits

  1. Connect to the kit that runs this sample with a terminal emulator (for example, PuTTY). See How to connect with PuTTY for the required settings.

  2. Program the other development kit with the Bluetooth: Peripheral HR 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 Bluetooth: Central / Heart-rate Monitor sample and reset it.

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

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

    [NOTIFICATION] data 0x20006779 length 2
    

The sample works as relay now.

Testing with nRF Connect for Desktop

  1. Connect to the kit that runs this sample with a terminal emulator (for example, PuTTY). See How to connect with PuTTY for the required settings.

  2. Reset the development kit.

  3. Start nRF Connect for Desktop and select the connected dongle that is used for communication.

  4. Open the SERVER SETUP tab.

  5. Click the dongle configuration and select Load setup.

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

  7. Click Apply to device.

  8. Open the CONNECTION MAP tab.

  9. Click the dongle configuration(gear ikon) and select Advertising setup.

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

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

  11. Click Apply and Close.

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

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

  14. Explore the Heart Rate Measurement characteristic.

  15. 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
    
  16. The nRF Connect for Desktop also detects the Central and Peripheral HRS sample Heart Rate Service.

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

  18. Write again value 06 80 and click the Play button to send a notification. The same value appears for the sample Heart Rate Measurement characteristic, sample works as relay for Heart Rate Service.

Dependencies

This sample uses the following nRF Connect SDK libraries:

In addition, it uses the following Zephyr libraries:

  • include/zephyr.h

  • Bluetooth APIs:

    • 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