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 STM32CubeWBA MCU Package[4] is the subject of this article.
Bluetooth® Low Energy Heart Rate Profile & STM32WBA |
---|
The table below describes the structure of Heart Rate Sensor services:
Bluetooth® Low Energy Heart Rate Service specification | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
The Heart 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 |
---|
The GATT - generic attribute profile defines and manages in/out data exchange.
Collector GATT client device and Sensor GATT service device |
---|
Example of flow diagram between STM32WBA & STBLEToolbox |
---|
1.1. Advertising Data
At startup, the Heart Rate Sensor application starts a fast advertising(80ms/100ms).
Data advertised are composed as follow:
Heart Rate Advertising packet | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
The manufacturer data are encoded following STMicroelectronics BlueST-SDK v2 as described below:
STMicroelectronics Manufacturer Advertising data | |||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
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 Nucleo-WBA55CG boards | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
2. Requirements
2.1. Software and Hardware requirements
For software and hardware requirements refer to STM32WBA Build BLE Project wiki page.
2.2. Collector applications compatible
The STM32CubeWBA Heart Rate Sensor project is compatible with the following collectors:
3. STM32WB Heart Rate Sensor example description
3.1. Project directory
The "BLE_HeartRate" application is available by downloading the STM32CubeWBA MCU Package[4].
Refer to 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 |
---|
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 |
---|
3.2.3. GAP and GATT initialization and interaction
Heart Rate Sensor software module interaction |
---|
and 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 SVCCTL_Init() - svc_ctl.c.
- Manage the GAP event - SVCCTL_App_Notification().
- HCI_LE_CONNECTION_COMPLETE - provides information of the connection interval, slave latency, supervision timeout.
- HCI_LE_CONNECTION_UPDATE_COMPLETE- provides the new information of the connection.
- HCI_DISCONNECTION_COMPLETE - informs the application about the link disconnection and the reason.
The Services management is done by the service controller, svc_ctl.c
- Initialize the number of registered handlers - SVCCTL_Init().
- Manage events - SVCCTL_UserEvtRx()- from the Bluetooth® Low Energy Host Stack and redirect them to the gap event handler - SVCCTL_App_Notification.
The application level of the Heart Rate Sensor is done with hrs_app.c:
- Initialization of the services:
- Heart Rate Service - HRS_Init() - 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_Measurement) and increase the energy expended to transfer to the remote device (collector) - HRS_UpdateValue().
The Heart Rate Service hrs.c manages the specification of the service:
- Service Init - HRS_Init()
- Registers Heart Rate Event Handle to Service Controller - SVCCTL_RegisterSvcHandler(HRS_EventHandler).
- Initializes Service UUID – add Heart Rate service as Primary services.
- Initializes Heart rate measurement characteristic.
- Initializes Body Sensor location characteristic.
- Manages the GATT event from the Bluetooth® Low Energy Stack - HRS_EventHandler().
- ACI_GATT_WRITE_PERMIT_REQ_VSEVT_CODE
- Reception of a Write Command: HR Control Point Characteristic Value
- Sending of an aci_gatt_write_response() with an OK or KO status
- Notification to the application to Reset Energy Expended - HRS_Notification(HRS_RESET_ENERGY_EXPENDED_EVT)
- Reception of a Write Command: HR Control Point Characteristic Value
- ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE
- Reception of an attribute modification - HR Measurement Characteristics Description Value : ENABLE or DISABLE Notification
- Notify application of the Measurement Notification - HRS_Notification(HRS_NOTIFICATION_ENABLED/DISABLED)
- Reception of an attribute modification - HR Measurement Characteristics Description Value : ENABLE or DISABLE Notification
- ACI_GATT_WRITE_PERMIT_REQ_VSEVT_CODE
3.3. How to use the Bluetooth® Low Energy Heart Rate application
Once the Bluetooth® Low Energy Heart Rate application is installed on the STM32WBA 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.
With the Nucleo-WBA55CG board, it is also possible to change the RF PHY modulation.
- 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 (1)
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 (0)
A debugger can also be supported by enabling it within app_conf.h as described below:
/**
* User interaction
* When CFG_LED_SUPPORTED is set, LEDS are activated if requested
* When CFG_BUTTON_SUPPORTED is set, the push button are activated if requested
* When CFG_DBG_SUPPORTED is set, the debugger is activated
*/
[...]
#define CFG_DBG_SUPPORTED (1)
Heart Rate Sensor - Initialization phase | Connected Phase |
---|---|
==>> Start Ble_Hci_Gap_Gatt_Init function Success: hci_reset command Success: aci_hal_write_config_data command - CONFIG_DATA_PUBADDR_OFFSET Public Bluetooth Address: 00:80:e1:2a:7c:ea Success: aci_hal_write_config_data command - CONFIG_DATA_IR_OFFSET Success: aci_hal_write_config_data command - CONFIG_DATA_ER_OFFSET Success: aci_hal_set_tx_power_level command Success: aci_gatt_init command Success: aci_gap_init command Success: aci_gatt_update_char_value - Device Name Success: aci_gatt_update_char_value - Appearance Success: hci_le_set_default_phy command Success: aci_gap_set_io_capability command Success: aci_gap_set_authentication_requirement command Success: aci_gap_configure_whitelist command ==>> End Ble_Hci_Gap_Gatt_Init function Services and Characteristics creation Success: aci_gatt_add_service command: HRS Success: aci_gatt_add_char command : HRME Success: aci_gatt_add_char command : BSL Success: aci_gatt_add_char command : HRCP Success: aci_gatt_update_char_value BSL command Success: aci_gatt_add_service command: DIS Success: aci_gatt_add_char command : MANS Success: aci_gatt_add_char command : MNBS Success: aci_gatt_add_char command : SNS Success: aci_gatt_add_char command : HRS Success: aci_gatt_add_char command : FRS Success: aci_gatt_update_char_value MANS command Success: aci_gatt_update_char_value MNBS command Success: aci_gatt_update_char_value SNS command Success: aci_gatt_update_char_value HRS command Success: aci_gatt_update_char_value FRS command End of Services and Characteristics creation ==>> aci_gap_set_discoverable - Success ==>> Success: Start Advertising |
>>== HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE - Connection handle: 0x0001 - Connection established with @:62:f1:e7:ab:a6:18 - Connection Interval: 48.75 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: 48.75 ms - Connection latency: 0 - Supervision Timeout: 5000 ms 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 the STM32WBA 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 | ||
---|---|---|
5. References