04-16-2019 08:22
04-16-2019 08:22
Is there a reliable way to 'background wake' a Watchface app to have it perform background duties (checking for messages, updating data) while the device screen is off and the clockTick event is not being fired?
I've noticed my app, after a long period of inactivity, will display very out-of-date data to the user when they raise the watch to wake it, which will then quickly update on the next tick, but it's a noticeable delay.
I'd rather have it so that the data is refreshed in the background, every 5 mins, so the data immediately displayed to the user is 'fresh'.
Or is the preferred way of doing this to fire a data refresh on the screen state change event to 'on'?
Thanks.
Answered! Go to the Best Answer.
04-16-2019 14:07
04-16-2019 14:07
I'd probably use the wakeInterval in the companion, and send a new file to the device when you have data changed.
Then on the device you just need to listen to the `newfile` event, and process files when you receive them.
// companion
const MILLISECONDS_PER_MINUTE = 1000 * 60;
me.wakeInterval = 5 * MILLISECONDS_PER_MINUTE;
me.addEventListener("wakeinterval", getData);
if (me.launchReasons.wokenUp) {
getData();
}
function getData() {}
// device
import { inbox } from "file-transfer";
function processAllFiles() {
let fileName;
while (fileName = inbox.nextFile()) {
if (fileName) {
console.log(`Received: ${fileName}`);
}
}
}
inbox.addEventListener("newfile", processAllFiles);
processAllFiles();
04-16-2019 09:47
04-16-2019 09:47
So I did a bit more reading. It seems the only way to do this is with a setInterval or setTimeout function.
I'd really apprecaite any feedback from the FitBit team on this. What I want to acheive is for the clockface to check for backend data update every 5 minutes while the display is not active and process any haptic alerts required. So I have created a bgUpdate() function that runs the required tasks.
On load I set:
let bgUpdateTimer = setTimeout(function(){ bgUpdate() }, 300000);
Then I add a handler to the screen state change event to either cancel the background update if the screen turns on (as I will let ontick handle it) or reset the timer if the screen turns off.
display.onchange = ((evt) => {
if(display.on){
clearTimeout(bgUpdateTimer);
} else {
bgUpdateTimer = setTimeout(function(){ bgUpdate() }, 300000);
}
});
Then my general background update function, which also resets the timer when complete:
function bgUpdate(){
// Request new data from companion
requestData();
// Wait 5 seconds and process any new data
setTimeout(function(){
processNewData();
processAlerts();
bgUpdateTimer = setTimeout(function(){ bgUpdate() }, 300000);
},5000);
}
Is this a 'good' way of doing it? Can you advise if this will significantly effect battery life?
04-16-2019 14:07
04-16-2019 14:07
I'd probably use the wakeInterval in the companion, and send a new file to the device when you have data changed.
Then on the device you just need to listen to the `newfile` event, and process files when you receive them.
// companion
const MILLISECONDS_PER_MINUTE = 1000 * 60;
me.wakeInterval = 5 * MILLISECONDS_PER_MINUTE;
me.addEventListener("wakeinterval", getData);
if (me.launchReasons.wokenUp) {
getData();
}
function getData() {}
// device
import { inbox } from "file-transfer";
function processAllFiles() {
let fileName;
while (fileName = inbox.nextFile()) {
if (fileName) {
console.log(`Received: ${fileName}`);
}
}
}
inbox.addEventListener("newfile", processAllFiles);
processAllFiles();
04-16-2019 14:37
04-16-2019 14:37
I was using wakeInterval in the Companion but it didn't seem to be working reliably and I was finding it hard to debug because the Simulator doesn't support wakeInterval.
Is support for wakeInterval planned for the sim? Or is there any way to get device logs out when running on a real device?
Thanks.
04-16-2019 15:11
04-16-2019 15:11
You can get console logs from the real device and real phone when you connect to the developer bridge on those same devices.
04-17-2019 01:17
04-17-2019 01:17
I actually don't own a FitBit device myself 😂 so I'm having to develop 'blind'. I've got a couple of testers helping me but I have to use the GAM method as I can't see anyway to hook the Dev Bridge into someone else's account. Is it possible?
Thanks for all your help Jon.