I'm developing simple Shake feature for my app and i faced a weird problem. When I test it on my Honor5x it works like a charm, but when i tried to perform it on Samsung S5 it's way too sensitive. Is it possible that accelerometer works differently on diffrent devices? If so is it possbile to modify coniditions depends on device/accelerometer accuracy?
Here's my code for detecting Shake event:
gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0]; gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1]; float accX = event.values[0] - gravity[0]; float accY = event.values[1] - gravity[1]; float maxSpeed = Math.max(accX, accY); if (maxSpeed > SHAKE_THRESHOLD) { if(shakesInRow == 0){ shakesInRow++; shakeTime = System.currentTimeMillis(); }else{ if((System.currentTimeMillis() - shakeTime) < SHAKE_TIME_DETECTOR){ shakesInRow++; }else{ shakesInRow = 0; shakeTime = 0; return; } if(shakesInRow>= SHAKE_IN_ROW) { shakesInRow= 0; runMainActivity(); } } }
You can try Square library for shake detection - seismic
They collect samples within certain period, and if 3/4 of those samples are accelerating then it means that there was a shaking.
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