public abstract class SensorManager extends Object
SensorManager lets you access the device's sensors
. Get an instance of this class by calling
Context.getSystemService()
with the argument
Context.SENSOR_SERVICE
.
Always make sure to disable sensors you don't need, especially when your activity is paused. Failing to do so can drain the battery in just a few hours. Note that the system will not disable sensors automatically when the screen turns off.
Note: Don't use this mechanism with a Trigger Sensor, have a look
at TriggerEventListener
. Sensor.TYPE_SIGNIFICANT_MOTION
is an example of a trigger sensor.
public class SensorActivity extends Activity implements SensorEventListener { private final SensorManager mSensorManager; private final Sensor mAccelerometer; public SensorActivity() { mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); } protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); } protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); } public void onAccuracyChanged(Sensor sensor, int accuracy) { } public void onSensorChanged(SensorEvent event) { } }
SensorEventListener
,
SensorEvent
,
Sensor
Modifier and Type | Class and Description |
---|---|
static class |
SensorManager.DynamicSensorCallback
Used for receiving notifications from the SensorManager when dynamic sensors are connected or
disconnected.
|
Modifier and Type | Field and Description |
---|---|
static int |
AXIS_MINUS_X
|
static int |
AXIS_MINUS_Y
|
static int |
AXIS_MINUS_Z
|
static int |
AXIS_X
|
static int |
AXIS_Y
|
static int |
AXIS_Z
|
static int |
DATA_X
Deprecated.
use
Sensor instead. |
static int |
DATA_Y
Deprecated.
use
Sensor instead. |
static int |
DATA_Z
Deprecated.
use
Sensor instead. |
static float |
GRAVITY_DEATH_STAR_I
Gravity (estimate) on the first Death Star in Empire units (m/s^2)
|
static float |
GRAVITY_EARTH
Earth's gravity in SI units (m/s^2)
|
static float |
GRAVITY_JUPITER
Jupiter's gravity in SI units (m/s^2)
|
static float |
GRAVITY_MARS
Mars' gravity in SI units (m/s^2)
|
static float |
GRAVITY_MERCURY
Mercury's gravity in SI units (m/s^2)
|
static float |
GRAVITY_MOON
The Moon's gravity in SI units (m/s^2)
|
static float |
GRAVITY_NEPTUNE
Neptune's gravity in SI units (m/s^2)
|
static float |
GRAVITY_PLUTO
Pluto's gravity in SI units (m/s^2)
|
static float |
GRAVITY_SATURN
Saturn's gravity in SI units (m/s^2)
|
static float |
GRAVITY_SUN
Sun's gravity in SI units (m/s^2)
|
static float |
GRAVITY_THE_ISLAND
Gravity on the island
|
static float |
GRAVITY_URANUS
Uranus' gravity in SI units (m/s^2)
|
static float |
GRAVITY_VENUS
Venus' gravity in SI units (m/s^2)
|
static float |
LIGHT_CLOUDY
luminance under a cloudy sky in lux
|
static float |
LIGHT_FULLMOON
luminance at night with full moon in lux
|
static float |
LIGHT_NO_MOON
luminance at night with no moon in lux
|
static float |
LIGHT_OVERCAST
luminance under an overcast sky in lux
|
static float |
LIGHT_SHADE
luminance in shade in lux
|
static float |
LIGHT_SUNLIGHT
luminance of sunlight in lux
|
static float |
LIGHT_SUNLIGHT_MAX
Maximum luminance of sunlight in lux
|
static float |
LIGHT_SUNRISE
luminance at sunrise in lux
|
static float |
MAGNETIC_FIELD_EARTH_MAX
Maximum magnetic field on Earth's surface
|
static float |
MAGNETIC_FIELD_EARTH_MIN
Minimum magnetic field on Earth's surface
|
static float |
PRESSURE_STANDARD_ATMOSPHERE
Standard atmosphere, or average sea-level pressure in hPa (millibar)
|
static int |
RAW_DATA_INDEX
Deprecated.
use
Sensor instead. |
static int |
RAW_DATA_X
Deprecated.
use
Sensor instead. |
static int |
RAW_DATA_Y
Deprecated.
use
Sensor instead. |
static int |
RAW_DATA_Z
Deprecated.
use
Sensor instead. |
static int |
SENSOR_ACCELEROMETER
Deprecated.
use
Sensor instead. |
static int |
SENSOR_ALL
Deprecated.
use
Sensor instead. |
static int |
SENSOR_DELAY_FASTEST
get sensor data as fast as possible
|
static int |
SENSOR_DELAY_GAME
rate suitable for games
|
static int |
SENSOR_DELAY_NORMAL
rate (default) suitable for screen orientation changes
|
static int |
SENSOR_DELAY_UI
rate suitable for the user interface
|
static int |
SENSOR_LIGHT
Deprecated.
use
Sensor instead. |
static int |
SENSOR_MAGNETIC_FIELD
Deprecated.
use
Sensor instead. |
static int |
SENSOR_MAX
Deprecated.
use
Sensor instead. |
static int |
SENSOR_MIN
Deprecated.
use
Sensor instead. |
static int |
SENSOR_ORIENTATION
Deprecated.
use
Sensor instead. |
static int |
SENSOR_ORIENTATION_RAW
Deprecated.
use
Sensor instead. |
static int |
SENSOR_PROXIMITY
Deprecated.
use
Sensor instead. |
static int |
SENSOR_STATUS_ACCURACY_HIGH
This sensor is reporting data with maximum accuracy
|
static int |
SENSOR_STATUS_ACCURACY_LOW
This sensor is reporting data with low accuracy, calibration with the
environment is needed
|
static int |
SENSOR_STATUS_ACCURACY_MEDIUM
This sensor is reporting data with an average level of accuracy,
calibration with the environment may improve the readings
|
static int |
SENSOR_STATUS_NO_CONTACT
The values returned by this sensor cannot be trusted because the sensor
had no contact with what it was measuring (for example, the heart rate
monitor is not in contact with the user).
|
static int |
SENSOR_STATUS_UNRELIABLE
The values returned by this sensor cannot be trusted, calibration is
needed or the environment doesn't allow readings
|
static int |
SENSOR_TEMPERATURE
Deprecated.
use
Sensor instead. |
static int |
SENSOR_TRICORDER
Deprecated.
use
Sensor instead. |
static float |
STANDARD_GRAVITY
Standard gravity (g) on Earth.
|
protected static String |
TAG |
Constructor and Description |
---|
SensorManager() |
Modifier and Type | Method and Description |
---|---|
boolean |
cancelTriggerSensor(TriggerEventListener listener,
Sensor sensor)
Cancels receiving trigger events for a trigger sensor.
|
protected abstract boolean |
cancelTriggerSensorImpl(TriggerEventListener listener,
Sensor sensor,
boolean disable) |
boolean |
flush(SensorEventListener listener)
Flushes the FIFO of all the sensors registered for this listener.
|
protected abstract boolean |
flushImpl(SensorEventListener listener) |
static float |
getAltitude(float p0,
float p)
Computes the Altitude in meters from the atmospheric pressure and the
pressure at sea level.
|
static void |
getAngleChange(float[] angleChange,
float[] R,
float[] prevR)
Helper function to compute the angle change between two rotation matrices.
|
Sensor |
getDefaultSensor(int type)
Use this method to get the default sensor for a given type.
|
Sensor |
getDefaultSensor(int type,
boolean wakeUp)
Return a Sensor with the given type and wakeUp properties.
|
List<Sensor> |
getDynamicSensorList(int type)
Use this method to get a list of available dynamic sensors of a certain type.
|
protected abstract List<Sensor> |
getFullDynamicSensorList()
Gets the full list of dynamic sensors that are available.
|
protected abstract List<Sensor> |
getFullSensorList()
Gets the full list of sensors that are available.
|
static float |
getInclination(float[] I)
Computes the geomagnetic inclination angle in radians from the
inclination matrix I returned by
getRotationMatrix(float[], float[], float[], float[]) . |
static float[] |
getOrientation(float[] R,
float[] values)
Computes the device's orientation based on the rotation matrix.
|
static void |
getQuaternionFromVector(float[] Q,
float[] rv)
Helper function to convert a rotation vector to a normalized quaternion.
|
static boolean |
getRotationMatrix(float[] R,
float[] I,
float[] gravity,
float[] geomagnetic)
Computes the inclination matrix I as well as the rotation matrix
R transforming a vector from the device coordinate system to the
world's coordinate system which is defined as a direct orthonormal basis,
where:
X is defined as the vector product Y.Z (It is tangential to
the ground at the device's current location and roughly points East).
|
static void |
getRotationMatrixFromVector(float[] R,
float[] rotationVector)
Helper function to convert a rotation vector to a rotation matrix.
|
List<Sensor> |
getSensorList(int type)
Use this method to get the list of available sensors of a certain type.
|
int |
getSensors()
Deprecated.
This method is deprecated, use
getSensorList(int) instead |
boolean |
initDataInjection(boolean enable)
For testing purposes only.
|
protected abstract boolean |
initDataInjectionImpl(boolean enable) |
boolean |
injectSensorData(Sensor sensor,
float[] values,
int accuracy,
long timestamp)
For testing purposes only.
|
protected abstract boolean |
injectSensorDataImpl(Sensor sensor,
float[] values,
int accuracy,
long timestamp) |
boolean |
isDynamicSensorDiscoverySupported()
Tell if dynamic sensor discovery feature is supported by system.
|
void |
registerDynamicSensorCallback(SensorManager.DynamicSensorCallback callback)
Add a
DynamicSensorCallback to receive dynamic sensor connection callbacks. |
void |
registerDynamicSensorCallback(SensorManager.DynamicSensorCallback callback,
Handler handler)
Add a
DynamicSensorCallback to receive dynamic sensor connection callbacks. |
protected abstract void |
registerDynamicSensorCallbackImpl(SensorManager.DynamicSensorCallback callback,
Handler handler) |
boolean |
registerListener(SensorEventListener listener,
Sensor sensor,
int samplingPeriodUs)
Registers a
SensorEventListener for the given
sensor at the given sampling frequency. |
boolean |
registerListener(SensorEventListener listener,
Sensor sensor,
int samplingPeriodUs,
Handler handler)
Registers a
SensorEventListener for the given
sensor. |
boolean |
registerListener(SensorEventListener listener,
Sensor sensor,
int samplingPeriodUs,
int maxReportLatencyUs)
Registers a
SensorEventListener for the given
sensor at the given sampling frequency and the given maximum reporting latency. |
boolean |
registerListener(SensorEventListener listener,
Sensor sensor,
int samplingPeriodUs,
int maxReportLatencyUs,
Handler handler)
Registers a
SensorEventListener for the given
sensor at the given sampling frequency and the given maximum reporting latency. |
boolean |
registerListener(SensorListener listener,
int sensors)
Deprecated.
This method is deprecated, use
registerListener(SensorEventListener, Sensor, int)
instead. |
boolean |
registerListener(SensorListener listener,
int sensors,
int rate)
Deprecated.
This method is deprecated, use
registerListener(SensorEventListener, Sensor, int)
instead. |
protected abstract boolean |
registerListenerImpl(SensorEventListener listener,
Sensor sensor,
int delayUs,
Handler handler,
int maxReportLatencyUs,
int reservedFlags) |
static boolean |
remapCoordinateSystem(float[] inR,
int X,
int Y,
float[] outR)
Rotates the supplied rotation matrix so it is expressed in a different
coordinate system.
|
boolean |
requestTriggerSensor(TriggerEventListener listener,
Sensor sensor)
Requests receiving trigger events for a trigger sensor.
|
protected abstract boolean |
requestTriggerSensorImpl(TriggerEventListener listener,
Sensor sensor) |
void |
unregisterDynamicSensorCallback(SensorManager.DynamicSensorCallback callback)
Remove a
DynamicSensorCallback to stop sending dynamic sensor connection events to that
callback. |
protected abstract void |
unregisterDynamicSensorCallbackImpl(SensorManager.DynamicSensorCallback callback) |
void |
unregisterListener(SensorEventListener listener)
Unregisters a listener for all sensors.
|
void |
unregisterListener(SensorEventListener listener,
Sensor sensor)
Unregisters a listener for the sensors with which it is registered.
|
void |
unregisterListener(SensorListener listener)
Deprecated.
This method is deprecated, use
unregisterListener(SensorEventListener)
instead. |
void |
unregisterListener(SensorListener listener,
int sensors)
Deprecated.
This method is deprecated, use
unregisterListener(SensorEventListener, Sensor)
instead. |
protected abstract void |
unregisterListenerImpl(SensorEventListener listener,
Sensor sensor) |
protected static final String TAG
@Deprecated public static final int SENSOR_ORIENTATION
Sensor
instead.SensorListener
for more details.@Deprecated public static final int SENSOR_ACCELEROMETER
Sensor
instead.SensorListener
for more details.@Deprecated public static final int SENSOR_TEMPERATURE
Sensor
instead.SensorListener
for more details.@Deprecated public static final int SENSOR_MAGNETIC_FIELD
Sensor
instead.SensorListener
for more details.@Deprecated public static final int SENSOR_LIGHT
Sensor
instead.SensorListener
for more details.@Deprecated public static final int SENSOR_PROXIMITY
Sensor
instead.SensorListener
for more details.@Deprecated public static final int SENSOR_TRICORDER
Sensor
instead.SensorListener
for more details.@Deprecated public static final int SENSOR_ORIENTATION_RAW
Sensor
instead.SensorListener
for more details.@Deprecated public static final int SENSOR_ALL
Sensor
instead.@Deprecated public static final int SENSOR_MIN
Sensor
instead.@Deprecated public static final int SENSOR_MAX
Sensor
instead.@Deprecated public static final int DATA_X
Sensor
instead.SensorListener.onSensorChanged(int, float[])
@Deprecated public static final int DATA_Y
Sensor
instead.SensorListener.onSensorChanged(int, float[])
@Deprecated public static final int DATA_Z
Sensor
instead.SensorListener.onSensorChanged(int, float[])
@Deprecated public static final int RAW_DATA_INDEX
Sensor
instead.SensorListener.onSensorChanged(int, float[])
@Deprecated public static final int RAW_DATA_X
Sensor
instead.SensorListener.onSensorChanged(int, float[])
@Deprecated public static final int RAW_DATA_Y
Sensor
instead.SensorListener.onSensorChanged(int, float[])
@Deprecated public static final int RAW_DATA_Z
Sensor
instead.SensorListener.onSensorChanged(int, float[])
public static final float STANDARD_GRAVITY
public static final float GRAVITY_SUN
public static final float GRAVITY_MERCURY
public static final float GRAVITY_VENUS
public static final float GRAVITY_EARTH
public static final float GRAVITY_MOON
public static final float GRAVITY_MARS
public static final float GRAVITY_JUPITER
public static final float GRAVITY_SATURN
public static final float GRAVITY_URANUS
public static final float GRAVITY_NEPTUNE
public static final float GRAVITY_PLUTO
public static final float GRAVITY_DEATH_STAR_I
public static final float GRAVITY_THE_ISLAND
public static final float MAGNETIC_FIELD_EARTH_MAX
public static final float MAGNETIC_FIELD_EARTH_MIN
public static final float PRESSURE_STANDARD_ATMOSPHERE
public static final float LIGHT_SUNLIGHT_MAX
public static final float LIGHT_SUNLIGHT
public static final float LIGHT_SHADE
public static final float LIGHT_OVERCAST
public static final float LIGHT_SUNRISE
public static final float LIGHT_CLOUDY
public static final float LIGHT_FULLMOON
public static final float LIGHT_NO_MOON
public static final int SENSOR_DELAY_FASTEST
public static final int SENSOR_DELAY_GAME
public static final int SENSOR_DELAY_UI
public static final int SENSOR_DELAY_NORMAL
public static final int SENSOR_STATUS_NO_CONTACT
public static final int SENSOR_STATUS_UNRELIABLE
public static final int SENSOR_STATUS_ACCURACY_LOW
public static final int SENSOR_STATUS_ACCURACY_MEDIUM
public static final int SENSOR_STATUS_ACCURACY_HIGH
public static final int AXIS_X
public static final int AXIS_Y
public static final int AXIS_Z
public static final int AXIS_MINUS_X
public static final int AXIS_MINUS_Y
public static final int AXIS_MINUS_Z
protected abstract List<Sensor> getFullSensorList()
protected abstract List<Sensor> getFullDynamicSensorList()
@Deprecated public int getSensors()
getSensorList(int)
insteadpublic List<Sensor> getSensorList(int type)
Sensor.TYPE_ALL
to get all the
sensors.
NOTE: Both wake-up and non wake-up sensors matching the given type are
returned. Check Sensor.isWakeUpSensor()
to know the wake-up properties
of the returned Sensor
.
type
- of sensors requestedgetDefaultSensor(int)
,
Sensor
public List<Sensor> getDynamicSensorList(int type)
Sensor.TYPE_ALL
to get all dynamic sensors.
NOTE: Both wake-up and non wake-up sensors matching the given type are
returned. Check Sensor.isWakeUpSensor()
to know the wake-up properties
of the returned Sensor
.
type
- of sensors requestedSensor
public Sensor getDefaultSensor(int type)
getSensorList
.type
- of sensors requestedgetSensorList(int)
,
Sensor
public Sensor getDefaultSensor(int type, boolean wakeUp)
For example,
Sensor.TYPE_ACCELEROMETER
, true) returns a wake-up accelerometer
sensor if it exists. Sensor.TYPE_PROXIMITY
, false) returns a non wake-up proximity
sensor if it exists. Sensor.TYPE_PROXIMITY
, true) returns a wake-up proximity sensor
which is the same as the Sensor returned by getDefaultSensor(int)
.
Note: Sensors like Sensor.TYPE_PROXIMITY
and Sensor.TYPE_SIGNIFICANT_MOTION
are declared as wake-up sensors by default.
type
- type of sensor requestedwakeUp
- flag to indicate whether the Sensor is a wake-up or non wake-up sensor.Sensor.isWakeUpSensor()
@Deprecated public boolean registerListener(SensorListener listener, int sensors)
registerListener(SensorEventListener, Sensor, int)
instead.listener
- sensor listener objectsensors
- a bit masks of the sensors to register totrue
if the sensor is supported and successfully
enabled@Deprecated public boolean registerListener(SensorListener listener, int sensors, int rate)
registerListener(SensorEventListener, Sensor, int)
instead.listener
- sensor listener objectsensors
- a bit masks of the sensors to register torate
- rate of events. This is only a hint to the system. events may be
received faster or slower than the specified rate. Usually events
are received faster. The value must be one of
SENSOR_DELAY_NORMAL
, SENSOR_DELAY_UI
,
SENSOR_DELAY_GAME
, or SENSOR_DELAY_FASTEST
.true
if the sensor is supported and successfully
enabled@Deprecated public void unregisterListener(SensorListener listener)
unregisterListener(SensorEventListener)
instead.listener
- a SensorListener object@Deprecated public void unregisterListener(SensorListener listener, int sensors)
unregisterListener(SensorEventListener, Sensor)
instead.listener
- a SensorListener objectsensors
- a bit masks of the sensors to unregister frompublic void unregisterListener(SensorEventListener listener, Sensor sensor)
Sensor.TYPE_SIGNIFICANT_MOTION
.
Use cancelTriggerSensor(TriggerEventListener, Sensor)
instead.
listener
- a SensorEventListener objectsensor
- the sensor to unregister fromunregisterListener(SensorEventListener)
,
registerListener(SensorEventListener, Sensor, int)
public void unregisterListener(SensorEventListener listener)
listener
- a SensorListener objectunregisterListener(SensorEventListener, Sensor)
,
registerListener(SensorEventListener, Sensor, int)
protected abstract void unregisterListenerImpl(SensorEventListener listener, Sensor sensor)
public boolean registerListener(SensorEventListener listener, Sensor sensor, int samplingPeriodUs)
SensorEventListener
for the given
sensor at the given sampling frequency.
The events will be delivered to the provided SensorEventListener
as soon as they are
available. To reduce the power consumption, applications can use
registerListener(SensorEventListener, Sensor, int, int)
instead and specify a
positive non-zero maximum reporting latency.
In the case of non-wake-up sensors, the events are only delivered while the Application
Processor (AP) is not in suspend mode. See Sensor.isWakeUpSensor()
for more details.
To ensure delivery of events from non-wake-up sensors even when the screen is OFF, the
application registering to the sensor must hold a partial wake-lock to keep the AP awake,
otherwise some events might be lost while the AP is asleep. Note that although events might
be lost while the AP is asleep, the sensor will still consume power if it is not explicitly
deactivated by the application. Applications must unregister their SensorEventListener
s in their activity's onPause()
method to avoid consuming power
while the device is inactive. See registerListener(SensorEventListener, Sensor, int,
int)
for more details on hardware FIFO (queueing) capabilities and when some sensor events
might be lost.
In the case of wake-up sensors, each event generated by the sensor will cause the AP to
wake-up, ensuring that each event can be delivered. Because of this, registering to a wake-up
sensor has very significant power implications. Call Sensor.isWakeUpSensor()
to check
whether a sensor is a wake-up sensor. See
registerListener(SensorEventListener, Sensor, int, int)
for information on how to
reduce the power impact of registering to wake-up sensors.
Note: Don't use this method with one-shot trigger sensors such as
Sensor.TYPE_SIGNIFICANT_MOTION
. Use
requestTriggerSensor(TriggerEventListener, Sensor)
instead. Use
Sensor.getReportingMode()
to obtain the reporting mode of a given sensor.
listener
- A SensorEventListener
object.sensor
- The Sensor
to register to.samplingPeriodUs
- The rate sensor events
are
delivered at. This is only a hint to the system. Events may be received faster or
slower than the specified rate. Usually events are received faster. The value must
be one of SENSOR_DELAY_NORMAL
, SENSOR_DELAY_UI
,
SENSOR_DELAY_GAME
, or SENSOR_DELAY_FASTEST
or, the desired delay
between events in microseconds. Specifying the delay in microseconds only works
from Android 2.3 (API level 9) onwards. For earlier releases, you must use one of
the SENSOR_DELAY_*
constants.true
if the sensor is supported and successfully enabled.registerListener(SensorEventListener, Sensor, int, Handler)
,
unregisterListener(SensorEventListener)
,
unregisterListener(SensorEventListener, Sensor)
public boolean registerListener(SensorEventListener listener, Sensor sensor, int samplingPeriodUs, int maxReportLatencyUs)
SensorEventListener
for the given
sensor at the given sampling frequency and the given maximum reporting latency.
This function is similar to registerListener(SensorEventListener, Sensor, int)
but
it allows events to stay temporarily in the hardware FIFO (queue) before being delivered. The
events can be stored in the hardware FIFO up to maxReportLatencyUs
microseconds. Once
one of the events in the FIFO needs to be reported, all of the events in the FIFO are
reported sequentially. This means that some events will be reported before the maximum
reporting latency has elapsed.
When maxReportLatencyUs
is 0, the call is equivalent to a call to
registerListener(SensorEventListener, Sensor, int)
, as it requires the events to be
delivered as soon as possible.
When sensor.maxFifoEventCount()
is 0, the sensor does not use a FIFO, so the call
will also be equivalent to registerListener(SensorEventListener, Sensor, int)
.
Setting maxReportLatencyUs
to a positive value allows to reduce the number of
interrupts the AP (Application Processor) receives, hence reducing power consumption, as the
AP can switch to a lower power state while the sensor is capturing the data. This is
especially important when registering to wake-up sensors, for which each interrupt causes the
AP to wake up if it was in suspend mode. See Sensor.isWakeUpSensor()
for more
information on wake-up sensors.
Note: Don't use this method with one-shot trigger sensors such as
Sensor.TYPE_SIGNIFICANT_MOTION
. Use
requestTriggerSensor(TriggerEventListener, Sensor)
instead. listener
- A SensorEventListener
object
that will receive the sensor events. If the application is interested in receiving
flush complete notifications, it should register with
SensorEventListener2
instead.sensor
- The Sensor
to register to.samplingPeriodUs
- The desired delay between two consecutive events in microseconds.
This is only a hint to the system. Events may be received faster or slower than
the specified rate. Usually events are received faster. Can be one of
SENSOR_DELAY_NORMAL
, SENSOR_DELAY_UI
,
SENSOR_DELAY_GAME
, SENSOR_DELAY_FASTEST
or the delay in
microseconds.maxReportLatencyUs
- Maximum time in microseconds that events can be delayed before
being reported to the application. A large value allows reducing the power
consumption associated with the sensor. If maxReportLatencyUs is set to zero,
events are delivered as soon as they are available, which is equivalent to calling
registerListener(SensorEventListener, Sensor, int)
.true
if the sensor is supported and successfully enabled.registerListener(SensorEventListener, Sensor, int)
,
unregisterListener(SensorEventListener)
,
flush(SensorEventListener)
public boolean registerListener(SensorEventListener listener, Sensor sensor, int samplingPeriodUs, Handler handler)
SensorEventListener
for the given
sensor. Events are delivered in continuous mode as soon as they are available. To reduce the
power consumption, applications can use
registerListener(SensorEventListener, Sensor, int, int)
instead and specify a
positive non-zero maximum reporting latency.
Note: Don't use this method with a one shot trigger sensor such as
Sensor.TYPE_SIGNIFICANT_MOTION
. Use
requestTriggerSensor(TriggerEventListener, Sensor)
instead. listener
- A SensorEventListener
object.sensor
- The Sensor
to register to.samplingPeriodUs
- The rate sensor events
are
delivered at. This is only a hint to the system. Events may be received faster or
slower than the specified rate. Usually events are received faster. The value must
be one of SENSOR_DELAY_NORMAL
, SENSOR_DELAY_UI
,
SENSOR_DELAY_GAME
, or SENSOR_DELAY_FASTEST
or, the desired
delay between events in microseconds. Specifying the delay in microseconds only
works from Android 2.3 (API level 9) onwards. For earlier releases, you must use
one of the SENSOR_DELAY_*
constants.handler
- The Handler
the sensor events
will be delivered to.true
if the sensor is supported and successfully enabled.registerListener(SensorEventListener, Sensor, int)
,
unregisterListener(SensorEventListener)
,
unregisterListener(SensorEventListener, Sensor)
public boolean registerListener(SensorEventListener listener, Sensor sensor, int samplingPeriodUs, int maxReportLatencyUs, Handler handler)
SensorEventListener
for the given
sensor at the given sampling frequency and the given maximum reporting latency.listener
- A SensorEventListener
object
that will receive the sensor events. If the application is interested in receiving
flush complete notifications, it should register with
SensorEventListener2
instead.sensor
- The Sensor
to register to.samplingPeriodUs
- The desired delay between two consecutive events in microseconds.
This is only a hint to the system. Events may be received faster or slower than
the specified rate. Usually events are received faster. Can be one of
SENSOR_DELAY_NORMAL
, SENSOR_DELAY_UI
,
SENSOR_DELAY_GAME
, SENSOR_DELAY_FASTEST
or the delay in
microseconds.maxReportLatencyUs
- Maximum time in microseconds that events can be delayed before
being reported to the application. A large value allows reducing the power
consumption associated with the sensor. If maxReportLatencyUs is set to zero,
events are delivered as soon as they are available, which is equivalent to calling
registerListener(SensorEventListener, Sensor, int)
.handler
- The Handler
the sensor events
will be delivered to.true
if the sensor is supported and successfully enabled.registerListener(SensorEventListener, Sensor, int, int)
protected abstract boolean registerListenerImpl(SensorEventListener listener, Sensor sensor, int delayUs, Handler handler, int maxReportLatencyUs, int reservedFlags)
public boolean flush(SensorEventListener listener)
onFlushCompleted
is called
after all the events in the batch at the time of calling this method have been delivered
successfully. If the hardware doesn't support flush, it still returns true and a trivial
flush complete event is sent after the current event for all the clients registered for this
sensor.listener
- A SensorEventListener
object
which was previously used in a registerListener call.true
if the flush is initiated successfully on all the sensors
registered for this listener, false if no sensor is previously registered for this
listener or flush on one of the sensors fails.IllegalArgumentException
- when listener is null.registerListener(SensorEventListener, Sensor, int, int)
protected abstract boolean flushImpl(SensorEventListener listener)
public void registerDynamicSensorCallback(SensorManager.DynamicSensorCallback callback)
DynamicSensorCallback
to receive dynamic sensor connection callbacks. Repeat
registration with the already registered callback object will have no additional effect.callback
- An object that implements the
DynamicSensorCallback
interface for receiving callbacks.IllegalArgumentException
- when callback is null.#addDynamicSensorCallback(DynamicSensorCallback, Handler)
public void registerDynamicSensorCallback(SensorManager.DynamicSensorCallback callback, Handler handler)
DynamicSensorCallback
to receive dynamic sensor connection callbacks. Repeat
registration with the already registered callback object will have no additional effect.callback
- An object that implements the
DynamicSensorCallback
interface for receiving callbacks.handler
- The Handler
the sensor connection events
will be delivered to.IllegalArgumentException
- when callback is null.public void unregisterDynamicSensorCallback(SensorManager.DynamicSensorCallback callback)
DynamicSensorCallback
to stop sending dynamic sensor connection events to that
callback.callback
- An object that implements the
DynamicSensorCallback
interface for receiving callbacks.public boolean isDynamicSensorDiscoverySupported()
true
if dynamic sensor discovery is supported, false
otherwise.protected abstract void registerDynamicSensorCallbackImpl(SensorManager.DynamicSensorCallback callback, Handler handler)
protected abstract void unregisterDynamicSensorCallbackImpl(SensorManager.DynamicSensorCallback callback)
public static boolean getRotationMatrix(float[] R, float[] I, float[] gravity, float[] geomagnetic)
Computes the inclination matrix I as well as the rotation matrix R transforming a vector from the device coordinate system to the world's coordinate system which is defined as a direct orthonormal basis, where:
By definition:
[0 0 g] = R * gravity (g = magnitude of gravity)
[0 m 0] = I * R * geomagnetic (m = magnitude of geomagnetic field)
R is the identity matrix when the device is aligned with the world's coordinate system, that is, when the device's X axis points toward East, the Y axis points to the North Pole and the device is facing the sky.
I is a rotation matrix transforming the geomagnetic vector into
the same coordinate space as gravity (the world's coordinate space).
I is a simple rotation around the X axis. The inclination angle in
radians can be computed with getInclination(float[])
.
Each matrix is returned either as a 3x3 or 4x4 row-major matrix depending on the length of the passed array:
If the array length is 16:
/ M[ 0] M[ 1] M[ 2] M[ 3] \ | M[ 4] M[ 5] M[ 6] M[ 7] | | M[ 8] M[ 9] M[10] M[11] | \ M[12] M[13] M[14] M[15] /This matrix is ready to be used by OpenGL ES's
glLoadMatrixf(float[], int)
.
Note that because OpenGL matrices are column-major matrices you must transpose the matrix before using it. However, since the matrix is a rotation matrix, its transpose is also its inverse, conveniently, it is often the inverse of the rotation that is needed for rendering; it can therefore be used with OpenGL ES directly.
Also note that the returned matrices always have this form:
/ M[ 0] M[ 1] M[ 2] 0 \ | M[ 4] M[ 5] M[ 6] 0 | | M[ 8] M[ 9] M[10] 0 | \ 0 0 0 1 /
If the array length is 9:
/ M[ 0] M[ 1] M[ 2] \ | M[ 3] M[ 4] M[ 5] | \ M[ 6] M[ 7] M[ 8] /
The inverse of each matrix can be computed easily by taking its transpose.
The matrices returned by this function are meaningful only when the device is not free-falling and it is not close to the magnetic north. If the device is accelerating, or placed into a strong magnetic field, the returned matrices may be inaccurate.
R
- is an array of 9 floats holding the rotation matrix R when
this function returns. R can be null.
I
- is an array of 9 floats holding the rotation matrix I when
this function returns. I can be null.
gravity
- is an array of 3 floats containing the gravity vector expressed in
the device's coordinate. You can simply use the
values
returned by a
SensorEvent
of a
Sensor
of type
TYPE_ACCELEROMETER
.
geomagnetic
- is an array of 3 floats containing the geomagnetic vector
expressed in the device's coordinate. You can simply use the
values
returned by a
SensorEvent
of a
Sensor
of type
TYPE_MAGNETIC_FIELD
.true
on success, false
on failure (for
instance, if the device is in free fall). Free fall is defined as
condition when the magnitude of the gravity is less than 1/10 of
the nominal value. On failure the output matrices are not modified.getInclination(float[])
,
getOrientation(float[], float[])
,
remapCoordinateSystem(float[], int, int, float[])
public static float getInclination(float[] I)
getRotationMatrix(float[], float[], float[], float[])
.I
- inclination matrix see getRotationMatrix(float[], float[], float[], float[])
.getRotationMatrix(float[], float[], float[], float[])
,
getOrientation(float[], float[])
,
GeomagneticField
public static boolean remapCoordinateSystem(float[] inR, int X, int Y, float[] outR)
Rotates the supplied rotation matrix so it is expressed in a different
coordinate system. This is typically used when an application needs to
compute the three orientation angles of the device (see
getOrientation(float[], float[])
) in a different coordinate system.
When the rotation matrix is used for drawing (for instance with OpenGL
ES), it usually doesn't need to be transformed by this function,
unless the screen is physically rotated, in which case you can use
Display.getRotation()
to
retrieve the current rotation of the screen. Note that because the user
is generally free to rotate their screen, you often should consider the
rotation in deciding the parameters to use here.
Examples:
remapCoordinateSystem(inR, AXIS_X, AXIS_Z, outR);
Surface.ROTATION_90
:
remapCoordinateSystem(inR, AXIS_Y, AXIS_MINUS_X, outR);
getOrientation(float[], float[])
). If the rotation matrix is also used
for rendering, it may not need to be transformed, for instance if your
Activity
is running in landscape mode.
Since the resulting coordinate system is orthonormal, only two axes need to be specified.
inR
- the rotation matrix to be transformed. Usually it is the matrix
returned by getRotationMatrix(float[], float[], float[], float[])
.X
- defines the axis of the new cooridinate system that coincide with the X axis of the
original coordinate system.Y
- defines the axis of the new cooridinate system that coincide with the Y axis of the
original coordinate system.outR
- the transformed rotation matrix. inR and outR should not be the same
array.true
on success. false
if the input
parameters are incorrect, for instance if X and Y define the same
axis. Or if inR and outR don't have the same length.getRotationMatrix(float[], float[], float[], float[])
public static float[] getOrientation(float[] R, float[] values)
When it returns, the array values are as follows:
Applying these three rotations in the azimuth, pitch, roll order transforms an identity matrix to the rotation matrix passed into this method. Also, note that all three orientation angles are expressed in radians.
R
- rotation matrix see getRotationMatrix(float[], float[], float[], float[])
.values
- an array of 3 floats to hold the result.getRotationMatrix(float[], float[], float[], float[])
,
GeomagneticField
public static float getAltitude(float p0, float p)
Typically the atmospheric pressure is read from a
Sensor.TYPE_PRESSURE
sensor. The pressure at sea level must be
known, usually it can be retrieved from airport databases in the
vicinity. If unknown, you can use PRESSURE_STANDARD_ATMOSPHERE
as an approximation, but absolute altitudes won't be accurate.
To calculate altitude differences, you must calculate the difference
between the altitudes at both points. If you don't know the altitude
as sea level, you can use PRESSURE_STANDARD_ATMOSPHERE
instead,
which will give good results considering the range of pressure typically
involved.
float altitude_difference =
getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, pressure_at_point2)
- getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, pressure_at_point1);
p0
- pressure at sea levelp
- atmospheric pressurepublic static void getAngleChange(float[] angleChange, float[] R, float[] prevR)
Each input matrix is either as a 3x3 or 4x4 row-major matrix depending on the length of the passed array:
If the array length is 9, then the array elements represent this matrix
/ R[ 0] R[ 1] R[ 2] \ | R[ 3] R[ 4] R[ 5] | \ R[ 6] R[ 7] R[ 8] /
If the array length is 16, then the array elements represent this matrix
/ R[ 0] R[ 1] R[ 2] R[ 3] \ | R[ 4] R[ 5] R[ 6] R[ 7] | | R[ 8] R[ 9] R[10] R[11] | \ R[12] R[13] R[14] R[15] /See
getOrientation(float[], float[])
for more detailed definition of the output.R
- current rotation matrixprevR
- previous rotation matrixangleChange
- an an array of floats (z, x, and y) in which the angle change
(in radians) is storedpublic static void getRotationMatrixFromVector(float[] R, float[] rotationVector)
/ R[ 0] R[ 1] R[ 2] \ | R[ 3] R[ 4] R[ 5] | \ R[ 6] R[ 7] R[ 8] /If R.length == 16, the following matrix is returned:
/ R[ 0] R[ 1] R[ 2] 0 \ | R[ 4] R[ 5] R[ 6] 0 | | R[ 8] R[ 9] R[10] 0 | \ 0 0 0 1 /
rotationVector
- the rotation vector to convertR
- an array of floats in which to store the rotation matrixpublic static void getQuaternionFromVector(float[] Q, float[] rv)
rv
- the rotation vector to convertQ
- an array of floats in which to store the computed quaternionpublic boolean requestTriggerSensor(TriggerEventListener listener, Sensor sensor)
When the sensor detects a trigger event condition, such as significant motion in
the case of the Sensor.TYPE_SIGNIFICANT_MOTION
, the provided trigger listener
will be invoked once and then its request to receive trigger events will be canceled.
To continue receiving trigger events, the application must request to receive trigger
events again.
listener
- The listener on which the
TriggerEventListener.onTrigger(TriggerEvent)
will be delivered.sensor
- The sensor to be enabled.IllegalArgumentException
- when sensor is null or not a trigger sensor.protected abstract boolean requestTriggerSensorImpl(TriggerEventListener listener, Sensor sensor)
public boolean cancelTriggerSensor(TriggerEventListener listener, Sensor sensor)
Note that a Trigger sensor will be auto disabled if
TriggerEventListener.onTrigger(TriggerEvent)
has triggered.
This method is provided in case the user wants to explicitly cancel the request
to receive trigger events.
listener
- The listener on which the
TriggerEventListener.onTrigger(TriggerEvent)
is delivered.It should be the same as the one used
in requestTriggerSensor(TriggerEventListener, Sensor)
sensor
- The sensor for which the trigger request should be canceled.
If null, it cancels receiving trigger for all sensors associated
with the listener.IllegalArgumentException
- when sensor is a trigger sensor.protected abstract boolean cancelTriggerSensorImpl(TriggerEventListener listener, Sensor sensor, boolean disable)
public boolean initDataInjection(boolean enable)
injectSensorData(Sensor, float[], int, long)
to inject sensor data into the HAL.enable
- True to initialize a client in DATA_INJECTION mode.
False to clean up the native resources.protected abstract boolean initDataInjectionImpl(boolean enable)
public boolean injectSensorData(Sensor sensor, float[] values, int accuracy, long timestamp)
initDataInjection(boolean)
before this method to set the HAL in data injection mode. This
method should be called only if a previous call to initDataInjection has been successful and
the HAL and SensorService are already opreating in data injection mode.sensor
- The sensor to inject.values
- Sensor values to inject. The length of this
array must be exactly equal to the number of
values reported by the sensor type.accuracy
- Accuracy of the sensor.timestamp
- Sensor timestamp associated with the event.IllegalArgumentException
- when the sensor is null,
data injection is not supported by the sensor, values
are null, incorrect number of values for the sensor,
sensor accuracy is incorrect or timestamps are
invalid.protected abstract boolean injectSensorDataImpl(Sensor sensor, float[] values, int accuracy, long timestamp)