IT. Expert System.

Android Reference

SensorManager


android.hardware

Class SensorManager

  • Direct Known Subclasses:
    SystemSensorManager


    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.

     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) {
         }
     }
     
    See Also:
    SensorEventListener, SensorEvent, Sensor
    • Constructor Detail

      • SensorManager

        public SensorManager()
    • Method Detail

      • getFullSensorList

        protected abstract List<Sensor> getFullSensorList()
        Gets the full list of sensors that are available.
      • getSensors

        @Deprecated
        public int getSensors()
        Deprecated. This method is deprecated, use getSensorList(int) instead
        Returns:
        available sensors.
      • getSensorList

        public List<Sensor> getSensorList(int type)
        Use this method to get the list of available sensors of a certain type. Make multiple calls to get sensors of different types or use Sensor.TYPE_ALL to get all the sensors.
        Parameters:
        type - of sensors requested
        Returns:
        a list of sensors matching the asked type.
        See Also:
        getDefaultSensor(int), Sensor
      • getDefaultSensor

        public Sensor getDefaultSensor(int type)
        Use this method to get the default sensor for a given type. Note that the returned sensor could be a composite sensor, and its data could be averaged or filtered. If you need to access the raw sensors use getSensorList.
        Parameters:
        type - of sensors requested
        Returns:
        the default sensors matching the asked type.
        See Also:
        getSensorList(int), Sensor
      • registerListener

        @Deprecated
        public boolean registerListener(SensorListener listener,
                                          int sensors)
        Deprecated. This method is deprecated, use registerListener(SensorEventListener, Sensor, int) instead.
        Registers a listener for given sensors.
        Parameters:
        listener - sensor listener object
        sensors - a bit masks of the sensors to register to
        Returns:
        true if the sensor is supported and successfully enabled
      • unregisterListener

        @Deprecated
        public void unregisterListener(SensorListener listener,
                                         int sensors)
        Deprecated. This method is deprecated, use unregisterListener(SensorEventListener, Sensor) instead.
        Unregisters a listener for the sensors with which it is registered.
        Parameters:
        listener - a SensorListener object
        sensors - a bit masks of the sensors to unregister from
      • getRotationMatrix

        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:

        • 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).
        • Y is tangential to the ground at the device's current location and points towards the magnetic North Pole.
        • Z points towards the sky and is perpendicular to the ground.

        world coordinate-system diagram.


        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.

        Parameters:
        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.
        Returns:
        true on success, false on failure (for instance, if the device is in free fall). On failure the output matrices are not modified.
        See Also:
        getInclination(float[]), getOrientation(float[], float[]), remapCoordinateSystem(float[], int, int, float[])
      • remapCoordinateSystem

        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:

        • Using the camera (Y axis along the camera's axis) for an augmented reality application where the rotation angles are needed:
          • remapCoordinateSystem(inR, AXIS_X, AXIS_Z, outR);

        • Using the device as a mechanical compass when rotation is Surface.ROTATION_90:
          • remapCoordinateSystem(inR, AXIS_Y, AXIS_MINUS_X, outR);

          Beware of the above example. This call is needed only to account for a rotation from its natural orientation when calculating the rotation angles (see 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.

        Parameters:
        inR - the rotation matrix to be transformed. Usually it is the matrix returned by getRotationMatrix(float[], float[], float[], float[]).
        X - defines on which world axis and direction the X axis of the device is mapped.
        Y - defines on which world axis and direction the Y axis of the device is mapped.
        outR - the transformed rotation matrix. inR and outR can be the same array, but it is not recommended for performance reason.
        Returns:
        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.
        See Also:
        getRotationMatrix(float[], float[], float[], float[])
      • getOrientation

        public static float[] getOrientation(float[] R,
                             float[] values)
        Computes the device's orientation based on the rotation matrix.

        When it returns, the array values is filled with the result:

        • values[0]: azimuth, rotation around the Z axis.
        • values[1]: pitch, rotation around the X axis.
        • values[2]: roll, rotation around the Y axis.

        The reference coordinate-system used is different from the world coordinate-system defined for the rotation matrix:

        • X is defined as the vector product Y.Z (It is tangential to the ground at the device's current location and roughly points West).
        • Y is tangential to the ground at the device's current location and points towards the magnetic North Pole.
        • Z points towards the center of the Earth and is perpendicular to the ground.

        inverted world coordinate-system diagram.

        All three angles above are in radians and positive in the counter-clockwise direction.

        Parameters:
        R - rotation matrix see getRotationMatrix(float[], float[], float[], float[]).
        values - an array of 3 floats to hold the result.
        Returns:
        The array values passed as argument.
        See Also:
        getRotationMatrix(float[], float[], float[], float[]), GeomagneticField
      • getAltitude

        public static float getAltitude(float p0,
                        float p)
        Computes the Altitude in meters from the atmospheric pressure and the pressure at sea level.

        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);

        Parameters:
        p0 - pressure at sea level
        p - atmospheric pressure
        Returns:
        Altitude in meters
      • getAngleChange

        public static void getAngleChange(float[] angleChange,
                          float[] R,
                          float[] prevR)
        Helper function to compute the angle change between two rotation matrices. Given a current rotation matrix (R) and a previous rotation matrix (prevR) computes the rotation around the z,x, and y axes which transforms prevR to R. outputs a 3 element vector containing the z,x, and y angle change at indexes 0, 1, and 2 respectively.

        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]  /
        
        Parameters:
        R - current rotation matrix
        prevR - previous rotation matrix
        angleChange - an an array of floats (z, x, and y) in which the angle change is stored
      • getRotationMatrixFromVector

        public static void getRotationMatrixFromVector(float[] R,
                                       float[] rotationVector)
        Helper function to convert a rotation vector to a rotation matrix. Given a rotation vector (presumably from a ROTATION_VECTOR sensor), returns a 9 or 16 element rotation matrix in the array R. R must have length 9 or 16. If R.length == 9, the following matrix is returned:
           /  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  /
        
        Parameters:
        rotationVector - the rotation vector to convert
        R - an array of floats in which to store the rotation matrix
      • getQuaternionFromVector

        public static void getQuaternionFromVector(float[] Q,
                                   float[] rv)
        Helper function to convert a rotation vector to a normalized quaternion. Given a rotation vector (presumably from a ROTATION_VECTOR sensor), returns a normalized quaternion in the array Q. The quaternion is stored as [w, x, y, z]
        Parameters:
        rv - the rotation vector to convert
        Q - an array of floats in which to store the computed quaternion


Content

Android Reference

Java basics

Java Enterprise Edition (EE)

Java Standard Edition (SE)

SQL

HTML

PHP

CSS

Java Script

MYSQL

JQUERY

VBS

REGEX

C

C++

C#

Design patterns

RFC (standard status)

RFC (proposed standard status)

RFC (draft standard status)

RFC (informational status)

RFC (experimental status)

RFC (best current practice status)

RFC (historic status)

RFC (unknown status)

IT dictionary

License.
All information of this service is derived from the free sources and is provided solely in the form of quotations. This service provides information and interfaces solely for the familiarization (not ownership) and under the "as is" condition.
Copyright 2016 © ELTASK.COM. All rights reserved.
Site is optimized for mobile devices.
Downloads: 1733 / 96487532. Delta: 0.04740 с