Workout, a Tizen Sample App: Gathering Heart Rate Data

Patryk Falba

Senior Software Engineer

This is the third blog in a series to introduce the sample application Workout, a Tizen example for monitoring health sensors on a wearable device.

The first blog, Workout -- A Tizen Sample App for Monitoring Health Sensors, presented the basic features of the application. The second blog, Adding Distance Traveled to the Tizen Workout Sample App, described how distance traveled is calculated.

In this blog, I will demonstrate another key feature of the app, Heart Rate Measurement (HRM), which shows the most recent heart rate intensity.

Intensity

Implementation

To start collecting data from the HRM sensor, first start Tizen.Sensor.HeartRateMonitor from TizenFX API.

HeartRateMonitorService.cs

public void Init()
{
    try
    {
        _hrm = new HRM
        {
            Interval = 1000,
            PausePolicy = SensorPausePolicy.None
        };
 
        _hrm.DataUpdated += OnDataUpdated;
    }
    catch (Exception)
    {
        NotSupported?.Invoke(this, EventArgs.Empty);
    }
}

Initiating HRM in this way invokes DataUpdated every one second and the sensor is not stopped even when the application is sent to the background. The data from the event is handled by the OnDataUpdated handler, which invokes the event with the single bpm value.

This event is listened to by the OnServiceDataUpdated handler in the HeartRateMonitorModel, where all information related to heart rate is calculated:

HeartRateMonitorModel.cs

private void OnServiceDataUpdated(object sender, int bpm)
{
    double normalizedBpm = Math.Clamp((bpm - _minBpm) / (double)(_maxBpm - _minBpm), 0, 1);
    int bpmRange = bpm < _minBpm ? 0 : Math.Min((int)((normalizedBpm * (_bpmRanges - 1)) + 1), _bpmRanges - 1);
 
    if (!_isMeasurementPaused)
    {
        _bpmRangeOccurrences[bpmRange]++;
    }
 
    Updated?.Invoke(this, new HeartRateMonitorUpdatedEventArgs(new HeartRateMonitorData
    {
        Bpm = bpm,
        BpmRange = bpmRange,
        BpmRangeOccurrences = _bpmRangeOccurrences,
        NormalizedBpm = normalizedBpm
    }));
}

However, let's start with the values that are used in the above method:

_maxBpm - this value is calculated during the class instantiation according to the formula: 220 - user age
_minBpm - this is half the value of _maxBpm

_minBpm and _maxBpm is used to calculate normalizedBpm, a value ranging from 0 to 1.

Next, the bpmRange to which the current HRM service value belongs is calculated: For bpm below _minBpm, bpmRange is set to 0. For bpm greater than or equal to _minBpm, bpmRange is set to either (_normalizedBpm * (_bpmRanges -1) + 1) or (_bpmRanges - 1), whichever value is smaller.

This calculated pulse interval is used as a position in an array, whose value is increased by 1. To obtain the most common pulse interval, find the index with the highest value associated with it.

DetailsPageViewModel.cs

Intensity = Array.LastIndexOf(bpmRangeOccurrences, bpmRangeOccurrences.Max()).ToString();

To display the range indication, Intensity is delivered to XAML and converted into text using a converter.

DetailsPageView.xaml.cs

<models:DetailsItemData Name="intensity"
                        Value="{Binding Intensity, Converter={StaticResource BpmRangeValueConverter}}"
                        Icon="images/details_intensity_icon.png"
                        IsActionButtonVisible="True">

Read more

To learn more about the implementation of the HRM sensor and the use of the data in the Workout app, see this tutorial

In the final blog of this series, you'll learn how CircleListView is used in the app.