package com.codename1.sensors

Cross platform access to the device motion sensors and high level gesture detection.

The entry point is MotionSensorManager. It exposes the raw sensors (accelerometer, gyroscope, magnetometer) plus the values the framework derives from them (gravity, linear acceleration and orientation), and it recognizes a set of common gestures (shake, flip, tilt, pick up and free fall) entirely in the core so the gestures work on every platform that has an accelerometer.

Reading the accelerometer:

MotionSensor accel = MotionSensorManager.getInstance().getSensor(MotionSensorManager.TYPE_ACCELEROMETER);
if (accel != null) {
    accel.addListener(new MotionSensorListener() {
        public void motionReceived(MotionEvent evt) {
            // evt.getX(), evt.getY(), evt.getZ() in m/s^2, delivered on the EDT
        }
    });
}

Reacting to a shake:

MotionSensorManager.getInstance().addGestureListener(GestureEvent.TYPE_SHAKE, new GestureListener() {
    public void gestureDetected(GestureEvent evt) {
        // the device was shaken
    }
});

Sampling is reference counted: the hardware sensors are only powered while a listener is registered, so always remove your listeners when you no longer need them. On iOS the build argument ios.NSMotionUsageDescription must be supplied to gain access to the motion hardware.

Types

class GestureEventA high level gesture detected by the framework from the motion sensor stream and delivered to a GestureListener.
interface GestureListenerReceives high level gestures (shake, flip, tilt, pick up, free fall) derived from the motion sensors.
class MotionEventA single immutable reading from a motion sensor delivered to a MotionSensorListener.
class MotionSensorRepresents a single motion sensor (for example the accelerometer or the gyroscope).
interface MotionSensorListenerReceives raw readings from a MotionSensor.
class MotionSensorManagerCross platform access to the device motion sensors (accelerometer, gyroscope, magnetometer and the derived gravity, linear acceleration and orientation) together with high level gesture detection (shake, flip, tilt, pick up and free fall).