I have the following code which gives a beep when the (mobile) device is nudged slightly:
let audio = new Audio('ack.mp3');
function handleMotionEvent(event) {
let x = event.accelerationIncludingGravity.x;
let y = event.accelerationIncludingGravity.y;
if (Math.abs(x) + Math.abs(y) > 2.2) {
audio.play();
}
}
window.addEventListener("devicemotion", handleMotionEvent, true);
It works fine, but not at all when the device is locked. Is there any way I can detect this while the device is locked?
Seems you have to acquire a partial wake lock for that operation with. PowerManager class.
PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
PowerManager.WakeLock lock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SensorRead");
lock.acquire();
window.addEventListener("devicemotion", handleMotionEvent, true);
You need also this permission in the AndroidManifest.xml
:
<uses-permission android:name="android.permission.WAKE_LOCK" />
More Info
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