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

Heart beat sensor works on simulator but not on watch (Versa 3)

The heart rate in this clockface all of a sudden does not work. It continue to work on the simulator but not on the watch itself. I upgraded to SDK 6.0 and same thing. It works on the simulator but not the watch. Any help would be much appreciated.

 

import clock from "clock";
import document from "document";
import { preferences } from "user-settings";
import * as util from "../common/utils";
import { charger, battery } from "power";
import * as userActivity from "user-activity";
import { display } from "display";
import { HeartRateSensor } from "heart-rate";
import { vibration } from "haptics";

// Update the clock every second
clock.granularity = "seconds";

var newDate ="";
var dayDate ="";

// time display
let myLabel = document.getElementById("myLabel");
let myLabels = document.getElementById("myLabels");
let myLabelsec = document.getElementById("myLabelsec");
let myLabelssec = document.getElementById("myLabelssec");
let heartalert = document.getElementById("heartalert");

// date dispaly
let curMonth = document.getElementById("month");
let disDate = document.getElementById("date");
let weekDay= document.getElementById("week");
const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
const month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]


// read heart rate
if (HeartRateSensor) {
const hrm = new HeartRateSensor();
hrm.addEventListener("reading", () => {
console.log(`Current heart rate: ${hrm.heartRate}`);
let bpmValue = document.getElementById("heartlabel");
bpmValue.text = hrm.heartRate;
if (bpmValue.text > 134) {
vibration.start("nudge-max");
heartalert.text="Heart Rate Alert";
}
else {
heartalert.text="";
}

});
display.addEventListener("change", () => {
// Automatically stop the sensor when the screen is off to conserve battery
display.on ? hrm.start() : hrm.stop();
});
hrm.start();
}


// Update the <text> element every tick with the current time
clock.ontick = () => {

let today = new Date();
// check for date change
console.log(`dates: ${today}`);
newDate = today.getDate();

console.log(`newdates: ${newDate}`);
console.log(`daydates: ${dayDate}`);
// update date on date change
if (newDate != dayDate)
{
console.log(`dates change`);
dayDate = today.getDate();
disDate.text = today.getDate();

weekDay.text = days[today.getDay()];
curMonth.text = month[today.getMonth()];
}

let hours = today.getHours();
if (preferences.clockDisplay === "12h") {
// 12h format
hours = hours % 12 || 12;
} else {
// 24h format
hours = (hours);
}
let mins = util.zeroPad(today.getMinutes());
let secs = util.zeroPad(today.getSeconds());
myLabel.text = myLabels.text=`${hours}:${mins}:`;
myLabelsec.text = myLabelssec.text=`${secs}`;

updateSteps();
}
function updateSteps() {
let stepValue = document.getElementById("stepslabel");
stepValue.text = userActivity.today.adjusted.steps;
}

Best Answer
0 Votes
9 REPLIES 9

That code is not compatible with SDK 6. See Fix Broken Imports.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

Thanks I missed the change needed on "document" import and fixed that but it odes not solve the problem. The heart rate did not work on SDK5.0 before I upgrade to SDK6.0. I guess now I am facing 2 different issues, the syntax for the some of the imports and the issue with Heart rate sensor not working.

 

I have changed the imports to below. If I try to change to the new syntax for User-settings (import * as { preferences } from "user-settings";) I get a build error, so I leave that as is 

 

import clock from "clock";
import * as document from "document";
import * as userActivity from "user-activity";
import * as util from "../common/utils";
import { preferences } from "user-settings";
import { charger, battery } from "power";
import { display } from "display";
import { HeartRateSensor } from "heart-rate";
import { vibration } from "haptics";

 

I have got no errors for the above imports but the heart rate still does not work on the watch.

Best Answer
0 Votes

Is there a chance that it's a permissions issue? Did your clockface work previously?

Peter McLennan
Gondwana Software
Best Answer

Not sure if this is the cause, but I just updated my clockface to 6.0 from 4.2, and everything still works. One of the things different from your HR code and mine, is that I have a frequency as a parameter (see below)

 

if (HeartRateSensor) {
  const hrm = new HeartRateSensor({ frequency: 1 });
  hrm.addEventListener("reading", () => {
    heart.text = `${hrm.heartRate}`;
    // Other code

    }
  });
Best Answer
0 Votes

If not this, I agree with @Gondwana that it may be a permissions issue. Check your package.json and make sure it's updated and using the right permissions

Best Answer
0 Votes

Yes, it was working fine and all of a sudden it did not work. I could not figure out what went wrong, so I just tried upgrading to SDK6.0.

Best Answer
0 Votes

I checked the permission, its good and besides if its a permission issue, it should not work on the simulator.

Best Answer
0 Votes

I don't think the simulator checks permissions.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

@fibbyAlthough not pertaining to the problem

 

Your code has arrays of months and days.

 

As part of an optimisation I came up with a solution to eliminate them by using date().toStriing()

EG.

.toStriing().substr(0,3) instead for Weekday.

.toStriing().substr(4,3) instead for Month.

It might use less space, if its an issue.

 

 

const event = new Date('August 19, 1975 23:15:30');

console.log(event.toString());
// expected output: Tue Aug 19 1975 23:15:30 GMT+0200 (CEST)
// (note: your timezone may vary)

Author | ch, passion for improvement.

Best Answer
0 Votes