Question
I'm trying to retrieve heart rate data using the Human Activity Monitor's HRM functionality with the following code:
var HRUtils = {
abc: function() {
var count = 0;
function onsuccessCB(heartInfo) {
console.log("Rate: " + heartInfo.heartRate);
count++;
if(count >= 100) {
tizen.humanactivitymonitor.stop("HRM");
}
}
function onerrorCB(error) {
alert("Error!");
}
function onchangedCB(heartData) {
tizen.humanactivitymonitor.getHumanActivityData("HRM", onsuccessCB, onerrorCB);
}
tizen.humanactivitymonitor.start("HRM", onchangedCB);
}
};
The code works intermittently - sometimes it returns correct values (75, 73, 74... 80) but other times it returns all zeros (0, 0, 0, 0...). I can't determine why this inconsistency occurs.
Answer
Problem Understanding
The issue appears to be related to the HRM sensor's initialization and stabilization period. The inconsistent readings (correct values vs. zeros) suggest:
- The sensor needs time to stabilize and get accurate readings
- The device (like Gear S2) needs to be properly fitted on the wrist
- There might be initialization timing issues in the code
Solution Methods
-
Allow Sensor Stabilization Time:
- Wait 5-10 seconds after starting the HRM before expecting accurate readings
- Implement a delay mechanism or initial warm-up period
-
Check Device Fit:
- Ensure the wearable device is properly fitted on the wrist
- Make sure the sensor area has good contact with skin
-
Review Sample Code:
- Examine the official HeartRateMonitor sample app in Tizen IDE
- Consider using the TizenBeat open-source project as reference
Code Examples
Here's an improved version of the code with stabilization handling:
var HRUtils = {
monitorHeartRate: function() {
var count = 0;
var isStabilized = false;
var stabilizationTimer = null;
function onsuccessCB(heartInfo) {
if (isStabilized) {
console.log("Rate: " + heartInfo.heartRate);
count++;
if(count >= 100) {
tizen.humanactivitymonitor.stop("HRM");
clearTimeout(stabilizationTimer);
}
}
}
function onerrorCB(error) {
console.error("HRM Error: " + error.message);
}
function onchangedCB(heartData) {
tizen.humanactivitymonitor.getHumanActivityData("HRM", onsuccessCB, onerrorCB);
}
tizen.humanactivitymonitor.start("HRM", onchangedCB);
// Allow 5 seconds for sensor stabilization
stabilizationTimer = setTimeout(function() {
isStabilized = true;
console.log("Sensor stabilized - beginning data collection");
}, 5000);
}
};
Additional Tips
-
For reference implementations:
- Access the HeartRateMonitor sample in Tizen IDE: File → New → Tizen Web Project → Online Sample → HeartRateMonitor
- Check the TizenBeat project: https://github.com/alycecil/TizenBeat
-
Remember that HRM sensors:
- Work best when the user remains still during measurement
- May require periodic recalibration
- Performance can vary based on skin type and environmental conditions