4

How to detect rotation around Y axis of phone?

android phone axis

I am novice in android. I would like to detect 180 degrees rotation. I would like to detect for example if user flip phone which lies on a table or if user rotate his phone in his pocket.

I have read a lot of articles but I really don't understand how to get phone position and then compute angle between another position.

I have found for example this article but I don't know what to do with array named orientation:

Get device angle by using getOrientation() function

Thanks!

// Here is my solution. Not perfectly logical but works quite good:

public class FlipListener implements SensorEventListener {

    SensorManager sensorMgr;
    FlipEventReceiver receiver;

    public FlipListener(Context context, FlipEventReceiver receiver) {
        this.receiver = receiver;
        sensorMgr = (SensorManager) context.getSystemService(Activity.SENSOR_SERVICE);
        sensorMgr.registerListener(this, sensorMgr.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_UI);
    }   

    public void onResume() {
        sensorMgr.registerListener(this, sensorMgr.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_UI);
    }

    public void onPause() {
        sensorMgr.unregisterListener(this);
        clearStack();
    }

    private static final int IGNORE_FLIPS_AFTER_FLIP = 2500;
    private static final int SAMPLING_INTERVAL = 60;
    private static final int MINIMAL_STACK_SIZE_TO_FLIP = 2; // Shouldn't be lower than 2
    private static final float FLIP_RADIANS = (float)Math.toRadians(140);
    private static final int STACK_MAX_SIZE = 38;

    private List<Float> stack = new ArrayList<Float>();
    private long lastAdd = 0;
    private long lastFlip = 0;

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
            rotationRateAroundYChanged((float)event.values[1]);
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    private void rotationRateAroundYChanged(float rotationRateAroundY) {
        long currentTime = System.currentTimeMillis();

        if (lastFlip != 0 && (currentTime - lastFlip) < IGNORE_FLIPS_AFTER_FLIP) {
            return;
        }

        if( (currentTime - lastAdd) >= SAMPLING_INTERVAL ) {
            if( Math.abs(rotationRateAroundY) > 0.3 ) { // Smaller values are unimportant. They can make only mess.
                addToStack(rotationRateAroundY);
                checkForFlip();
            }
        }
    }

    private void checkForFlip() {

        int stackSize = stack.size();
        if( stackSize < MINIMAL_STACK_SIZE_TO_FLIP ) return;
        float approximateAngleSummary = 0;
        float val;

        for(int i = 0; i < stackSize; i++) {
            val = Math.abs(stack.get(i).floatValue());
            // "+ Math.pow(val/4.58, 2) )" don't have a sense. Simply it works better with it.
            approximateAngleSummary += ( (val + Math.pow(val/4.58, 2) ) / 1000 ) * SAMPLING_INTERVAL;

            if( approximateAngleSummary >= FLIP_RADIANS ) {
                triggerFlipDetected();
                clearStack();
                return;
            }
        }
    }

    private void clearStack() {
        stack.clear();
    }

    private void addToStack(float val) {
        lastAdd = System.currentTimeMillis();
        int stackSize = stack.size();
        if( stackSize > 0 && ((stack.get(stackSize-1) > 0 ? 1 : -1) != (val>0?1:-1) || stackSize > STACK_MAX_SIZE) ) {
            clearStack();
        }
        stack.add(val);
    }

    private void triggerFlipDetected() {
        lastFlip = System.currentTimeMillis();

        receiver.onFlipDetected();
    }

    public interface FlipEventReceiver {
        public void onFlipDetected();
    }
}

Usage:

public class FlipTestActivity extends Activity implements FlipEventReceiver {

FlipListener flipListener;
    boolean flipListenerActive = true;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flip_test);

    flipListener = new FlipListener(this, this);
}

public void onFlipDetected() {
    // What to do when flip detected
}

    @Override
protected void onResume() {
    super.onResume();
    if( !flipListenerActive ) { 
        flipListener.onResume();
        flipListenerActive = true;
    }
}

    @Override
protected void onPause() {
    super.onPause();
    if( flipListenerActive ) {
        flipListener.onPause();
        flipListenerActive = false;
    }
}

}
Community
  • 1
  • 1
1daemon1
  • 1,001
  • 3
  • 11
  • 29

1 Answers1

0

SensorManager is a class that lets you access the device's sensors. Check here

Here is a nice tutorial about sensors

AndyFaizan
  • 1,833
  • 21
  • 30