02-15-2022 17:16
02-15-2022 17:16
My app's goal is to ping the user every 5 minutes: if the user roll their wrist, then I am sure the user is awake and if they miss to roll wrist for 3 times, I'm sure they are asleep.
Here is what I do:
Below is the code I drafted:
// console.log("App code started");
import { vibration } from "haptics";
import * as document from "document";
import sleep from "sleep";
import * as messaging from "messaging";
import * as fs from "fs";
import { Gyroscope } from "gyroscope";
import { Accelerometer } from "accelerometer";
import { me as appbit } from "appbit";
import * as scientific from "scientific";
var minInterval = 5*60;
var noResponse = 0;
var ping = false;
var observe = false;
var responded = false;
// prevents the app to timeout due to lack of activity
appbit.appTimeoutEnabled = false;
// instantiate sleep state observer
if (sleep) {
sleep.addEventListener("change", () => {
console.log(`User sleep state is: ${sleep.state}`);
});
} else {
console.warn("Sleep API not supported on this device, or no permission")
}
// open messaging pipe to companion
messaging.peerSocket.addEventListener("open", (evt) => {
console.log("Ready to send messages");
});
// verify if device has accelerometer, then start it
if (Accelerometer) {
console.log("This device has an Accelerometer!");
// add event listener to accelerometer
var accelerometer = new Accelerometer({ frequency: 10, batch:1});
accelerometer.addEventListener("reading", () => {
console.log(`ACC reading ${scientific.sum(scientific.diff(accelerometer.readings.x))}, ${scientific.sum(scientific.diff(accelerometer.readings.y))}, ${scientific.sum(scientific.diff(accelerometer.readings.z))}`);
});
accelerometer.start();
} else {
console.log("This device does NOT have an Accelerometer!");}
var pingInterval = setInterval(pingUser, 5*60*1000);
function pingUser(){;
vibration.start("nudge");
}
The main difficulty for me is to identify how to capture the specific movement: should I add reading frequency and batch? Where do I put this logic? In the pingUser function, eventlistener or should I create another setInterval that goes after the pingUser?
02-22-2022 04:42
02-22-2022 04:42
The accelerometer reading event is probably the best place to capture the movement. We don't provide examples of capturing specific movements, but a quick Google should reveal the type of math and logic involved for detecting gestures with an accelerometer.
02-22-2022 12:03
02-22-2022 12:03
I actually managed to do it. I think I started from your example of the three balls. The hardest part was to set the right moment to get the readings. Thanks!!