Does anyone have an source code example of how to detect 'take the watch off' in an application or widget?
Does anyone have an source code example of how to detect 'take the watch off' in an application or widget?
What you can do is check Activity.Info.currentHeartRate and if wrist HR is being used, a null indicates the watch isn't being worn. If external HR is involved you can see if that is taken off.
It's worth noting that some (mostly old) Connect IQ watches don't have wrist HR, like Approach S60, Epix, Forerunner 230, Forerunner 630, 920XT, Fenix 3 and Vivoactive (original). (Even though the FR230 was released in 2015, I bet some people are still running with one.)
I will probably have to look at the history of the HR sensor, because the widget will not receive an event that the HR monitor has been interrupted. I need that when the user takes off the watch, so that the widget again asks for the PIN similar to the one in the Garmin Pay screen.
The history is only saved every couple of minutes or so, but if you use a timer to check Activity.Info.currentHeartRate, say every few seconds, you can see it sooner.
No matter what you check, you'll need a timer to do the check.
Create/start a 1 sec timer:
var timer= new Timer.Timer(); timer.start( method(:onTimer), 1000, true );
Check for heart rate in the call back:
function onTimer() { var actInfo=Activity.getActivityInfo(); if(actInfo.currentHeartRate==null) { //watch is not being worn } else { //watch is being worn } }
I think checking with timers will drain the battery a lot.
You need to check based on something like a timer. There's no call back for "hr stopped". In the code I posted, I set a 1 sec time. If you're worried about battery, increase that to 5 seconds, 10 seconds, etc.
In a device app or a widget, you may already have a timer for.basic things like updating the display, so use that to check HR. No need for a second timer.
if you use something like enableSensorEvents, you can also use the callback for that - it's every second, and check the HR in the sensor info (passed to the callback for that)
With devices that support it you could do it now via Complications and check in the callback. Just be aware battery 'updates' are sent through regardless if the value has actually changed. So maybe not better than a timer with it's current behavior.