Getting started with the Heart Rate Sensor within STM32WB0 Bluetooth® Low Energy

1. Heart Rate Profile

Heart Rate Profile (HRP) [1] is a generic attribute profile (GATT) based low-energy profile defined by the Bluetooth® Special Interest Group[2]
The HRP, widely used in fitness applications, defines the communication process between a GATT-server of a Heart Rate Sensor device, such as a wrist band, and a GATT-client Collector device, such as a smartphone or tablet.

  • The Heart Rate Sensor:
    • Measures the heart rate and exposes it via the Heart Rate Service[3].
    • Contains the Device Information Service that includes information, for example, about the manufacturer of the device.
    • Is the GATT server.
  • The Heart Rate Collector:
    • Accesses the information exposed by the heart rate sensor. It can display it to the end user, or store it on a nonvolatile memory for later analysis.
    • Is the GATT client.

The description of the Heart Rate Sensor project provided within the STM32CubeWB0 MCU Package[4] is the subject of this article.

Bluetooth® Low Energy Heart Rate Profile & STM32WB0
STM32WB0 Heart Rate Profile


The table below describes the structure of Heart Rate Sensor services:

Bluetooth® Low Energy Heart Rate Service specification
Service Characteristic Mode UUID size
Heart Rate Service 0x180D
Heart Rate Measurement Notify 0x2A37 23
Body Sensor Location Read 0x2A38 1
Heart Rate Control Point Write 0x2A39 1
Device Information Service 0x180A
Manufacturer Name String Read 0x2A29 32


TheHeart Rate Profile is a combination of a Heart Rate Collector and a Heart Rate Sensor. The goal is to connect and exchange data in different applications.

The GAP - Generic Access Profile defines and manages advertising and connection.

Collector Central device and Sensor Peripheral device
Connectivity HR GAP Interaction.png


The GATT - generic attribute profile defines and manages in/out data exchange.

Collector GATT client device and Sensor GATT service device
Connectivity HR GATT Interaction.png


Example of flow diagram between STM32WB0 & STBLEToolbox
STM32WB0 HR Flow Diagram


1.1. Advertising Data

At startup, the Heart Rate Sensor application starts a fast advertising (80ms/100ms).
Data advertised are composed as follows:

Heart Rate Advertising packet
Description Length AD Type Value
Device Name 6 0x09 HR_XX (XX: last byte of BD address)
Service UUID 3 0x03 0x180D (HRS)
Manufacturer Data 15 0xFF See table below
Flags 2 0x01 0x06
(GeneralDiscoverable, BrEdrNotSupported)


Manufacturer data are encoded following STMicroelectronics BlueST-SDK v2 for STM32WB0 as described below:

STMicroelectronics Manufacturer Advertising data
Byte Index 0 1 2-3 4 5 6 7 8 9 10-15
Function Length Manufacturer ID Company BlueST-SDK Version Board ID Firmware ID Option 1 Option 2 Option 3 Device Address
Value 0x0F 0xFF 0x0030 STMicroelectronics 0x02 0x8D Nucleo-WB0 0x89 - HR Sensor 0x00 0x00 0x00 0x08E12Axxxx


Advertising is switched to Low Power advertising (1s/2.5s) after 60s.


1.2. On board buttons configuration

Button configuration for Bluetooth® Low Energy HeartRate Application on STM32WB0 Serie Nucleo boards
Application Condition B1 Click B1 Long Press B2 Click B2 Long Press B3 Click B3 Long Press
HR Sensor Idle

Connected

Adv start

Toggle PHY(1M/2M)

/ Clear Security database

Peripheral Security Request

/ -

L2CAP CONN REQ

/


2. Requirements

2.1. Software and Hardware requirements

For software and hardware requirements refer to the STM32WB0 Build BLE Project wiki page.

2.2. Collector applications compatible

The STM32CubeWB0 Heart Rate Sensor project are compatible with the following collectors:

3. STM32WB0 Heart Rate Sensor example description

3.1. Project directory

The "BLE_HeartRate" application is available by downloading the STM32CubeWB0 MCU Package[4].

Refer to the How to Build a Bluetooth® Low Energy project Wiki page for project directory information.

3.2. Project description

3.2.1. Structure

Below, a software project structure with the most important parts:

Heart Rate project structure
Connectivity WB0 HeartRate Archi.png
Connectivity yellow box.png
Main applicative part files
Connectivity dark blue box.png
Services management
Connectivity green box.png
Bluetooth® Low Energy libraries
Connectivity pink box.png
Bluetooth® LE stack modular configuration options and event dispatcher files

WARNING: Do not modify the files in Middleware folder


3.2.2. Application initialization

The different steps of the application initialization are described below:

Heart Rate project initialization
Connectivity WB0 HeartRate Initialization.png
puce1.png
  • Initialize the system (HAL, clocks, peripherals).
puce2.png
  • Initialize the RNG, AES block, PKA
puce3.png
  • Initialize the Bluetooth® Low Energy Host Stack.
puce4.png
  • Initialize the Bluetooth® Low Energy GATT level.
  • Initialize the Bluetooth® Low Energy GAP level.
puce5.png
  • Initialize the Application/Profile context (Services, Characteristics)
  • Manage the Heart Rate Service notification.
puce6.png
  • Register Service Handler.
  • Update the services and characteristics.

3.2.3. GAP and GATT initialization and interaction

Heart Rate Sensor software module interaction
Connectivity WB0 HeartRate module interaction with puces.png
puce3.png

and puce4.png The Bluetooth® Low Energy Heart Rate Sensor application initialization is done within app_ble.c

  • Initialize the Bluetooth® Low Energy stack - initialize the device as peripheral - configure and start advertising: ADV parameters, local name, UUID - APP_BLE_init().
  • Call the services controller initialization BLEEVT_Init() - ble_evt.c.
  • Manage the GAP event - BLEEVT_App_Notification().
    • HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE- provides information of the connection interval, slave latency, supervision timeout.
    • HCI_LE_CONNECTION_UPDATE_COMPLETE_SUBEVT_CODE- provides the new information of the connection.
    • HCI_DISCONNECTION_COMPLETE_EVT_CODE- informs the application about the link disconnection and the reason.
puce5.png

Event management is done by the Bluetoooth® Low Energy Event Dispatcher, ble_evt.c

  • Initialize the number of registered handlers - BLEEVT_Init().
  • Manage events - BLE_STACK_Event()- from the Bluetooth® Low Energy Host Stack and redirect them to the event handler - BLEEVT_App_Notification.
puce6.png

The application level of the Heart Rate Sensor is done with hrs_app.c:

  • Initialization of the services:
    • Heart Rate Service - HRS_APP_Init() -> HRS_Init() in hrs.c
  • Initialization of the context of the application
    • Measurement value flags support
    • Body sensor location - HRS_BODY_SENSOR_LOCATION_HAND
  • Receive notification from the Heart Rate Service - HRS_Notification()
  • When Heart rate measurement characteristics are enabled by the remote, simulate every 1 s the Heart Rate Measure (HRS_APP_Measurements) and increase the energy expended to transfer to the remote device (collector) - HRS_UpdateValue().
puce7.png

The Heart Rate Service hrs.c manages the specification of the service:

  • Service Init - HRS_Init():
    • Registers Heart Rate Event Handle to Service Controller - BLEEVT_RegisterGattEvtHandler(HRS_EventHandler).
    • Initializes Service UUID – add Heart Rate service as Primary services.
      • Initializes Heart rate measurement characteristic.
      • Initializes Body Sensor location characteristic.
      • Initializes Heart rate Control Point characteristic.
  • Manages the GATT event from the Bluetooth® Low Energy Stack - HRS_EventHandler().
    • ACI_GATT_SRV_WRITE_VSEVT_CODE
      • Reception of a Write Command: HR Control Point Characteristic Value
        • Sending of an aci_gatt_srv_resp() with an OK or KO status
        • Notification to the application to Reset The Energy Expended Value:
          notification.EvtOpcode = HRS_RESET_ENERGY_EXPENDED_EVT;
          Notification(&notification);
          
  • ACI_GATT_SRV_ATTRIBUTE_MODIFIED_VSEVT_CODE
  • Reception of an attribute modification - HR Measurement Characteristics Description Value : ENABLE or DISABLE Notification
  • Notify application of the Measurement Notification:
    notification.EvtOpcode = HRS_HRME_NOTIFY_ENABLED_EVT/HRS_HRME_NOTIFY_DISABLED_EVT; 
    HRS_Notification(&notification);
    

3.3. How to use the Bluetooth® Low Energy Heart Rate application

Once the Bluetooth® Low Energy Heart Rate application is installed on the STM32WB0 platform, launch the ST BLE Sensor or ST BLE Toolbox smartphone application. Then, scan and connect the device called HR_XX (where XX is replaced by the last byte of the BD address) to the application.
Once the Bluetooth® Low Energy connection is established and the notification enabled by the smartphone:

  • Heart Rate measurement is provided remotely every 1 second thanks to the timer server and the task manager.

The Bluetooth® Low Energy Heart Rate application supports the pairing procedure.

  • Pressing B2 while not connected allows to clear the security database.
  • Pressing B2 while connected allows to request pairing.

This example also supports switch to 2Mbits PHY:

  • Pressing B1 while connected allows to switch between 2 Mbit/s and 1 Mbit/s PHY.

The Bluetooth® Low Energy Heart Rate application allows other actions on button:

  • Pressing B1 while not connected allows to restart the fast advertising.
  • Pressing B3 while connected allows to update the connection interval.

3.4. Low-power optimization

The project is delivered with the full system low-power enabled:

  • no debug trace
  • no debugger

It is possible to enable/disable the low-power feature within app_conf.h.

/******************************************************************************
 * Low Power
 *
 *  When CFG_FULL_LOW_POWER is set to 1, the system is configured in full
 *  low power mode. It means that all what can have an impact on the consumptions
 *  are powered down.(For instance LED, Access to Debugger, Etc.)
 *
 *  When CFG_FULL_LOW_POWER is set to 0, the low power mode is not activated
 *
 ******************************************************************************/
#define CFG_FULL_LOW_POWER       (0)

3.5. UART debug trace

Thanks to the debug log via UART interface, it is possible to trace the application project.

To enable the traces within the project, enable them within app_conf.h as described below:

/**
 * Enable or Disable traces in application
 */
#define CFG_DEBUG_APP_TRACE         (1)
Heart Rate Sensor - Initialization phase Connected Phase
  Success: aci_hal_set_tx_power_level command
  Success: aci_gatt_srv_profile_init command
  Success: aci_gap_init command
  Static Random Bluetooth Address: e2:5e:29:b8:32:a7
  Success: Gap_profile_set_dev_name - Device Name
  Success: Gap_profile_set_appearance - Appearance
  Success: hci_le_set_default_phy command
  Success: aci_gap_set_io_capability command
  Success: aci_gap_set_security_requirements command
  Success: aci_gap_configure_filter_accept_and_resolving_list command
==>> End BLE_Init function

Services and Characteristics creation
  Success: aci_gatt_srv_add_service command: HRS
  Success: aci_gatt_srv_add_service command: DIS
End of Services and Characteristics creation

==>> Success: aci_gap_set_advertising_configuration
==>> Success: aci_gap_set_advertising_data
==>> Success: aci_gap_set_advertising_enable
>>== hci_le_connection_complete_event - Connection handle: 0x0801
     - Connection established with @:73:9c:28:23:98:13
     - Connection Interval:   45.00 ms
     - Connection latency:    0
     - Supervision Timeout: 5000 ms
>>== HCI_LE_CONNECTION_UPDATE_COMPLETE_SUBEVT_CODE
     - Connection Interval:   7.50 ms
     - Connection latency:    0
     - Supervision Timeout:   5000 ms
>>== HCI_LE_CONNECTION_UPDATE_COMPLETE_SUBEVT_CODE
     - Connection Interval:   45.00 ms
     - Connection latency:    0
     - Supervision Timeout:   5000 ms
HCI_VENDOR_EVT: 0x0C01
HCI_VENDOR_EVT: 0x0C03
ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE 
HRS_NOTIFICATION_ENABLED

4. Heart Rate Collector - smartphone application

4.1. ST BLE Toolbox application

Once the Bluetooth® Low Energy Heart Rate application is installed on STM32WB0 platform, launch the STBLEToolbox smartphone application. The application starts scanning and enables seeing advertisement data, or to connect to the device. Once the device is connected, bpm and kcal measurements are displayed in real time.

Bluetooth® Low Energy Heart Rate application on STBLEToolbox
Connectivity WBA HeartRate Toolbox scan.jpg
Connectivity WBA HeartRate Toolbox adv.jpg
Connectivity WBA HeartRate Toolbox connected.jpg




5. References

Bluetooth® Low Energy stack v4.x Programming Guidelines (PM0274)