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 |
---|
The table below describes the structure of Heart Rate Sensor services:
Bluetooth® Low Energy Heart Rate Service specification | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
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 |
---|
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 STM32WB0 & STBLEToolbox |
---|
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 | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Manufacturer data are encoded following STMicroelectronics BlueST-SDK v2 for STM32WB0 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 STM32WB0 Serie Nucleo boards | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
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 |
---|
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 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.
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.
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().
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(¬ification);
- Reception of a Write Command: HR Control Point Characteristic Value
- ACI_GATT_SRV_WRITE_VSEVT_CODE
- 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(¬ification);
- Notify application of the Measurement 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 | ||
---|---|---|
5. References
Bluetooth® Low Energy stack v4.x Programming Guidelines (PM0274)