Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Heart Rate Sensor listener for no reading

ANSWERED

I have an app built which listens for heart rate using the Heart Rate Sensor. When I take the watch off it does not pick up that there is no data being sensed by the sensor. Is there a listener to listen for when readings have stopped? I have checked to see if the heart rate monitor is activated or if it has produced an error, but neither of these are triggered if the watch is taken off. I cannot see anything else in the API that would listen for this or for why the last value is being held by the heart rate sensor.

 

// Declare a even handler that will be called every time a new HR value is received.
heartRateMonitor.onreading = function() {
// Peek the current sensor values
//console.log("Current heart rate: " + heartRateMonitor.heartRate);
if (!heartRateMonitor.activated || heartRateMonitor.onerror) {
heartRateLabel.innerText = "--";
}
heartRateLabel.innerText = heartRateMonitor.heartRate;
//lastValueTimestamp = Date.now();
}

 

Any suggestions?
 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

You seem to have stumbled upon a limitation with the current API implementation.

 

The onreading event only fires when there is a reading, so it just stops when the device is off wrist.

The onerror event doesn't fire, because offwrist isn't an error state.

 

We're evaluating how best to deal with this, but in the meantime you can periodically peek the reading and check its timestamp. When the device is offwrist, the reading timestamp does not get updated.

View best answer in original post

Best Answer
10 REPLIES 10

I read sensor data every 1/2 second. Heartrate:

// Create new HR Sensor handle
let hrm = new HeartRateSensor();

hrm.onreading = function() {
  // Peek the current sensor values
  hrText.innerText = hrm.heartRate || 0;

  // Stop monitoring the sensor
  hrm.stop();
}
// Update heartrate and steps
function updateOther() {
  // Update Heart Rate
  hrm.start();  
}

Set timer:

setInterval(updateOther, 500);

This will update even if off the wrist.

Best Answer
0 Votes

Thank you but this did not help. I have checked another clock that the watch is running and it's heart rate did not change when the watch was taken off either. I am guessing this is a bug?

Best Answer
0 Votes

Seems to be the same on the latest firmware version just out. 

Best Answer

@WauloK wrote:

Seems to be the same on the latest firmware version just out. 


Did you enable the HRM permissions in package.json?

Best Answer
0 Votes

Yes I have. I am having heaps of issues with the Heart Rate Sensor working at all. I am in the process of doing a factory reset

Best Answer
0 Votes

HRM works fine. Yes, I have the permissions on. He's concern is when you take the watch off you don't get a 0 heart rate. It stays at 86 or whatever it was last on.

Best Answer
0 Votes

You seem to have stumbled upon a limitation with the current API implementation.

 

The onreading event only fires when there is a reading, so it just stops when the device is off wrist.

The onerror event doesn't fire, because offwrist isn't an error state.

 

We're evaluating how best to deal with this, but in the meantime you can periodically peek the reading and check its timestamp. When the device is offwrist, the reading timestamp does not get updated.

Best Answer

We're evaluating how best to deal with this, but in the meantime you can periodically peek the reading and check its timestamp. When the device is offwrist, the reading timestamp does not get updated.

 

--

 

Thanks for this. I will use this as a work around for now.

Best Answer
0 Votes

I did this and used: 

 

ts = heartRate.timestamp;

now = Date.now();

 

As you can see I got wildly different times. I would have thought the the heart rate timestamp would have been synced to the watch time???

 

[22:35:15]ts: Sun Jan 04 1970 21:16:05 GMT+11:00 now: Mon Nov 13 2017 22:35:10 GMT+11:00

 

I have worked around it by collecting the timestamp in the on reading function:

 

heartRateMonitor.onreading = function() {

 

  // Update timestamp;
  lastValueTimestamp = Date.now();

  // Retrieve heart rate from sensor
  heartRate = heartRateMonitor.heartRate;

  // Add heart rate to document element
  heartRateLabel.text = heartRate;

}

 

and then checking it later 

 

// time now
var now = Date.now();
// difference between now and the last time a heart reading was taken.
var difference = (now - lastValueTimestamp) / 1000;

//console.log("ts: " + lastValueTimestamp + " now: " + now);
//console.log("ts diff: " + difference);

if (difference > 5) {

  heartRateLabel.text = "---";
  heartRateZoneLabel.text = "---";
}

 

Best Answer
0 Votes

Yup! My Clockface was rejected from the App Gallery because it didn't zero out when off wrist 😕

Best Answer
0 Votes