Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shake event works differently on various devices Shake Event custom implementation

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();
                }
            }
        }
like image 630
Kacper Furmanski Avatar asked Nov 09 '22 08:11

Kacper Furmanski


1 Answers

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.

like image 173
NazarK Avatar answered Nov 15 '22 06:11

NazarK