I am using
window.addEventListener('devicemotion', event => this.watchDeviceMotion(event))
watchDeviceMotion(event: any) {
let motionData = {
x: event.acceleration.x,
y: event.acceleration.y,
z: event.acceleration.z,
}
let rotationData = {
alpha: event.rotationRate.alpha,
beta: event.rotationRate.beta,
gamma: event.rotationRate.gamma
}
if (this.getSensorData) {
// console.log(JSON.stringify(motionData))
// console.log(JSON.stringify(rotationData))
this.userData.motionData.push(motionData)
this.userData.rotationData.push(rotationData)
}
}
to access accelerometer data on an Android device using Ionic with Angular. When inside the app, the event works at a frequency of 60 Hz but when the app is switched to background, the frequency drops around 9-10 Hz for the first 5 mins and at some point while still in background it goes back at 60 Hz and remains like that indefinitely.
I am also using the background plugin with
this.backgroundMode.enable();
this.backgroundMode.on('activate').subscribe(() => {
this.backgroundMode.disableWebViewOptimizations();
});
I tried to add disableBatteryOptimizations() to the background plugin with the corresponding condition in config.xml but still no luck.
I also tried the plugins for Device Motion and Gyroscope but they behave the same.
How should I prevent the frequency from slowing down? Has anyone else encountered this problem before?
It is unclear which version of Android you are using, but working against the system usually leads nowhere. Guess you might have tested that with an elder version, because since Android 9, the sampling rate should have a frequency of 0Hz, when the application is in the background. This is due to a new power management with "App Standby Buckets", where only the "active" one isn't restricted.
See the Android 9 behavior changes:
Limited access to sensors in background Android 9 limits the ability for background apps to access user input and sensor data. If your app is running in the background on a device running Android 9, the system applies the following restrictions to your app:
- Your app cannot access the microphone or camera.
- Sensors that use the continuous reporting mode,
such as accelerometers and gyroscopes, don't receive events.- Sensors that use the on-change or one-shot reporting modes don't receive events.
The only way to prevent this is manually setting the bucket to "active":
adb shell am set-standby-bucket packagename active|working_set|frequent|rare
Source: Test power-related issues and there's also an UsageStatsManager.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With