Class MotionSensor
java.lang.Object
com.codename1.sensors.MotionSensor
Represents a single motion sensor (for example the accelerometer or the
gyroscope). Obtain an instance from
MotionSensorManager.getSensor(int) and start receiving readings by
registering a MotionSensorListener:
MotionSensor accel = MotionSensorManager.getInstance().getSensor(MotionSensorManager.TYPE_ACCELEROMETER);
if (accel != null) {
accel.addListener(new MotionSensorListener() {
public void motionReceived(MotionEvent evt) {
label.setText("x=" + evt.getX() + " y=" + evt.getY() + " z=" + evt.getZ());
}
});
}
Sampling is reference counted: the underlying hardware sensor is only active while at least one listener is registered, so always remove your listener (typically when the form is no longer showing) to conserve battery.
-
Method Summary
Modifier and TypeMethodDescriptionvoidRegisters a listener that will receive readings on the EDT.longThe timestamp of the most recent reading in milliseconds.intgetType()The type of this sensor, one of theMotionSensorManager.TYPE_*constants.floatgetX()The most recent value of the x axis, or0if no reading has arrived yet.floatgetY()The most recent value of the y axis, or0if no reading has arrived yet.floatgetZ()The most recent value of the z axis, or0if no reading has arrived yet.booleanWhether at least one reading has been received from this sensor.booleanWhether this sensor is available on the current device.voidRemoves a previously registered listener.
-
Method Details
-
getType
public int getType()The type of this sensor, one of theMotionSensorManager.TYPE_*constants. -
isSupported
public boolean isSupported()Whether this sensor is available on the current device. A sensor with no hardware backing can still be obtained but will never deliver readings. -
addListener
Registers a listener that will receive readings on the EDT. Registering the first listener starts the underlying hardware sensor.
Parameters
l: the listener to add, ignored ifnullor already registered
-
removeListener
Removes a previously registered listener. Removing the last listener stops the underlying hardware sensor.
Parameters
l: the listener to remove
-
getX
public float getX()The most recent value of the x axis, or0if no reading has arrived yet. -
getY
public float getY()The most recent value of the y axis, or0if no reading has arrived yet. -
getZ
public float getZ()The most recent value of the z axis, or0if no reading has arrived yet. -
hasReading
public boolean hasReading()Whether at least one reading has been received from this sensor. -
getTimestamp
public long getTimestamp()The timestamp of the most recent reading in milliseconds.
-