Workout, a Tizen Sample App: Gathering Heart Rate Data
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. 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.