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

Accelerometer "onreading" event keeps silently stopping (Fitbit Ionic)

I'm trying to get Accelerometer readings from the Ionic, but the onreading event in the Accelerometer keeps silently stopping.

Code below:

const accelerometer = new Accelerometer({ frequency: 1, batch: 10 });

accelerometer.addEventListener("activate", () => {
    console.log("onactivate");
});

accelerometer.addEventListener("error", () => {
    console.log("onerror: " + error);
});

accelerometer.addEventListener("reading", () => {
    console.log("onreading");
});

accelerometer.start();

From what I understand, the above setup should trigger the "reading" event every 10 seconds (frequency of 1 reading per second with a batch size of 10 readings). I've been testing this and consistently the reading event is only triggered about two or three times before silently stopping. The error event is never triggered.

If I remove the batch option and just leave the frequency at 1 I have more success. However it still silently stops, sometimes after about 20 readings, sometimes after 8, other times it goes for about 8 minutes before silently stopping.

I've no idea what's going on. Is the Accelerometer reading event supposed to stop with no warning?

Best Answer
0 Votes
5 REPLIES 5

I've had the accelerometer running indefinitely without problems. I can't see anything obviously sus with your code. I use

accelerometer.onreading = function() {

but that should be equivalent to what you've got.

Peter McLennan
Gondwana Software
Best Answer

Yeah, I've tried with 

accelerometer.onreading = function() {

as well and still the same result. It's really strange.

Best Answer
0 Votes

Is it just the event handler which stops firing, or is it the whole app?

 

The App Timeout API terminates apps after 2 minutes of inactivity.

https://dev.fitbit.com/blog/2018-10-05-announcing-fitbit-os-2.2/#app-timeout-api

 

If that's not the issue, are you able to share more code? Have you checked the memory usage of your app? https://dev.fitbit.com/build/reference/device-api/system/#variable-memory

 

 

 

 

Best Answer
0 Votes

Looks like it's just the event handler!

 

I've set the app timeout to false:

me.appTimeoutEnabled = false;

And I've tried it with both keeping the display constantly on and letting the display timeout happen and the result is the same.

 

I've been checking memory like this:

import { memory } from "system";

let memoryLabel = document.getElementById("label-memory");

export function init() {
    memoryLabel.text =  memory.js.used + "/" + memory.js.total;
}

and it's consistently sitting at around 35000/65528. It's not maxing out so I don't think it's a memory issue?

The app is a Clock face because I need it to be constantly running - the clock screen is showing the time, heart rate, steps, sleep, and calories burned. It's hooking up to a companion app which is using websockets to listen for incoming triggers sent from a server. Once a trigger is received the "screen" changes to a breathing exercise, which is just an animation and a timer.

What's interesting though is that I did have the Accelerometer code in a separate JS file like so:

import { Accelerometer } from "accelerometer";

export function init() {
    initialiseAccelerometer();
}

function initialiseAccelerometer() {

    if (Accelerometer) {
        console.log("-- Yes device has accelerometer --");

        const accelerometer = new Accelerometer({ frequency: 1, batch: 10 });

        accelerometer.addEventListener("activate", () => {
            console.log("onactivate");
        });

        accelerometer.addEventListener("error", () => {
            console.log("onerror: " + error);
        });

        accelerometer.addEventListener("reading", () => {
            console.log("onreading");
        });

        accelerometer.start();

    } else {
        console.log("This device does NOT have an Accelerometer!");
    }

}

And would just call my init() function from index.js. While troubleshooting I've moved the accelerometer code directly into index.js and now the "onreading" event seems to be triggering pretty consistently.

Any ideas why that would be? I could just be being really silly by having separated my code out! It's my first Fitbit app so I'm not 100% sure what the best practice for Fitbit dev is!

Best Answer
0 Votes

Are you sure there aren't any other part of the code overridding that handler?

 

The other possibility is scope of the variables, try:

 

import { Accelerometer } from "accelerometer";

let accelerometer;
export function init() { initialiseAccelerometer(); } function initialiseAccelerometer() { if (Accelerometer) { console.log("-- Yes device has accelerometer --"); accelerometer = new Accelerometer({ frequency: 1, batch: 10 }); accelerometer.addEventListener("activate", () => { console.log("onactivate"); }); accelerometer.addEventListener("error", () => { console.log("onerror: " + error); }); accelerometer.addEventListener("reading", () => { console.log("onreading"); }); accelerometer.start(); } else { console.log("This device does NOT have an Accelerometer!"); } }
Best Answer
0 Votes