Added in API level 1

SensorManager


abstract class SensorManager
kotlin.Any
   ↳ android.hardware.SensorManager

SensorManager lets you access the device's sensors.

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.

In order to access sensor data at high sampling rates (i.e. greater than 200 Hz for SensorEventListener and greater than SensorDirectChannel.RATE_NORMAL for SensorDirectChannel), apps must declare the android.Manifest.permission#HIGH_SAMPLING_RATE_SENSORS permission in their AndroidManifest.xml file.

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) {
      }
  }
  

Summary

Nested classes
abstract

Used for receiving notifications from the SensorManager when dynamic sensors are connected or disconnected.

Constants
static Int

see remapCoordinateSystem

static Int

see remapCoordinateSystem

static Int

see remapCoordinateSystem

static Int

see remapCoordinateSystem

static Int

see remapCoordinateSystem

static Int

see remapCoordinateSystem

static Int

Index of the X value in the array returned by android.hardware.SensorListener#onSensorChanged

static Int

Index of the Y value in the array returned by android.hardware.SensorListener#onSensorChanged

static Int

Index of the Z value in the array returned by android.hardware.SensorListener#onSensorChanged

static Float

Gravity (estimate) on the first Death Star in Empire units (m/s^2)

static Float

Earth's gravity in SI units (m/s^2)

static Float

Jupiter's gravity in SI units (m/s^2)

static Float

Mars' gravity in SI units (m/s^2)

static Float

Mercury's gravity in SI units (m/s^2)

static Float

The Moon's gravity in SI units (m/s^2)

static Float

Neptune's gravity in SI units (m/s^2)

static Float

Pluto's gravity in SI units (m/s^2)

static Float

Saturn's gravity in SI units (m/s^2)

static Float

Sun's gravity in SI units (m/s^2)

static Float

Gravity on the island

static Float

Uranus' gravity in SI units (m/s^2)

static Float

Venus' gravity in SI units (m/s^2)

static Float

luminance under a cloudy sky in lux

static Float

luminance at night with full moon in lux

static Float

luminance at night with no moon in lux

static Float

luminance under an overcast sky in lux

static Float

luminance in shade in lux

static Float

luminance of sunlight in lux

static Float

Maximum luminance of sunlight in lux

static Float

luminance at sunrise in lux

static Float

Maximum magnetic field on Earth's surface

static Float

Minimum magnetic field on Earth's surface

static Float

Standard atmosphere, or average sea-level pressure in hPa (millibar)

static Int

Offset to the untransformed values in the array returned by android.hardware.SensorListener#onSensorChanged

static Int

Index of the untransformed X value in the array returned by android.hardware.SensorListener#onSensorChanged

static Int

Index of the untransformed Y value in the array returned by android.hardware.SensorListener#onSensorChanged

static Int

Index of the untransformed Z value in the array returned by android.hardware.SensorListener#onSensorChanged

static Int

A constant describing an accelerometer.

static Int

A constant that includes all sensors

static Int

get sensor data as fast as possible

static Int

rate suitable for games

static Int

rate (default) suitable for screen orientation changes

static Int

rate suitable for the user interface

static Int

A constant describing an ambient light sensor See SensorListener for more details.

static Int

A constant describing a magnetic sensor See SensorListener for more details.

static Int

Largest sensor ID

static Int

Smallest sensor ID

static Int

A constant describing an orientation sensor.

static Int

A constant describing an orientation sensor.

static Int

A constant describing a proximity sensor See SensorListener for more details.

static Int

This sensor is reporting data with maximum accuracy

static Int

This sensor is reporting data with low accuracy, calibration with the environment is needed

static Int

This sensor is reporting data with an average level of accuracy, calibration with the environment may improve the readings

static Int

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

The values returned by this sensor cannot be trusted, calibration is needed or the environment doesn't allow readings

static Int

A constant describing a temperature sensor See SensorListener for more details.

static Int

A constant describing a Tricorder See SensorListener for more details.

static Float

Standard gravity (g) on Earth.

Public methods
open Boolean

Cancels receiving trigger events for a trigger sensor.

open SensorDirectChannel!

Create a sensor direct channel backed by shared memory wrapped in HardwareBuffer object.

open SensorDirectChannel!

Create a sensor direct channel backed by shared memory wrapped in MemoryFile object.

open Boolean

Flushes the FIFO of all the sensors registered for this listener.

open static Float

Computes the Altitude in meters from the atmospheric pressure and the pressure at sea level.

open static Unit
getAngleChange(angleChange: FloatArray!, R: FloatArray!, prevR: FloatArray!)

Helper function to compute the angle change between two rotation matrices.

open Sensor?

Use this method to get the default sensor for a given type.

open Sensor?
getDefaultSensor(type: Int, wakeUp: Boolean)

Return a Sensor with the given type and wakeUp properties.

open MutableList<Sensor!>!

Use this method to get a list of available dynamic sensors of a certain type.

open static Float

Computes the geomagnetic inclination angle in radians from the inclination matrix I returned by getRotationMatrix.

open static FloatArray!

Computes the device's orientation based on the rotation matrix.

open static Unit

Helper function to convert a rotation vector to a normalized quaternion.

open static Boolean
getRotationMatrix(R: FloatArray!, I: FloatArray!, gravity: FloatArray!, geomagnetic: FloatArray!)

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:

open static Unit

Helper function to convert a rotation vector to a rotation matrix.

open MutableList<Sensor!>!

Use this method to get the list of available sensors of a certain type.

open Int

open Boolean

Tell if dynamic sensor discovery feature is supported by system.

open Unit

Add a DynamicSensorCallback to receive dynamic sensor connection callbacks.

open Unit

Add a DynamicSensorCallback to receive dynamic sensor connection callbacks.

open Boolean
registerListener(listener: SensorEventListener!, sensor: Sensor!, samplingPeriodUs: Int)

Registers a SensorEventListener for the given sensor at the given sampling frequency.

open Boolean
registerListener(listener: SensorEventListener!, sensor: Sensor!, samplingPeriodUs: Int, handler: Handler!)

Registers a SensorEventListener for the given sensor.

open Boolean
registerListener(listener: SensorEventListener!, sensor: Sensor!, samplingPeriodUs: Int, maxReportLatencyUs: Int)

Registers a SensorEventListener for the given sensor at the given sampling frequency and the given maximum reporting latency.

open Boolean
registerListener(listener: SensorEventListener!, sensor: Sensor!, samplingPeriodUs: Int, maxReportLatencyUs: Int, handler: Handler!)

Registers a SensorEventListener for the given sensor at the given sampling frequency and the given maximum reporting latency.

open Boolean
registerListener(listener: SensorListener!, sensors: Int)

Registers a listener for given sensors.

open Boolean
registerListener(listener: SensorListener!, sensors: Int, rate: Int)

Registers a SensorListener for given sensors.

open static Boolean

Rotates the supplied rotation matrix so it is expressed in a different coordinate system.

open Boolean

Requests receiving trigger events for a trigger sensor.

open Unit

Remove a DynamicSensorCallback to stop sending dynamic sensor connection events to that callback.

open Unit

Unregisters a listener for all sensors.

open Unit

Unregisters a listener for the sensors with which it is registered.

open Unit

Unregisters a listener for all sensors.

open Unit
unregisterListener(listener: SensorListener!, sensors: Int)

Unregisters a listener for the sensors with which it is registered.

Constants

AXIS_MINUS_X

Added in API level 3
static val AXIS_MINUS_X: Int

see remapCoordinateSystem

Value: 129

AXIS_MINUS_Y

Added in API level 3
static val AXIS_MINUS_Y: Int

see remapCoordinateSystem

Value: 130

AXIS_MINUS_Z

Added in API level 3
static val AXIS_MINUS_Z: Int

see remapCoordinateSystem

Value: 131

AXIS_X

Added in API level 3
static val AXIS_X: Int

see remapCoordinateSystem

Value: 1

AXIS_Y

Added in API level 3
static val AXIS_Y: Int

see remapCoordinateSystem

Value: 2

AXIS_Z

Added in API level 3
static val AXIS_Z: Int

see remapCoordinateSystem

Value: 3

DATA_X

Added in API level 1
Deprecated in API level 15
static val DATA_X: Int

Deprecated: use Sensor instead.

Index of the X value in the array returned by android.hardware.SensorListener#onSensorChanged

Value: 0

DATA_Y

Added in API level 1
Deprecated in API level 15
static val DATA_Y: Int

Deprecated: use Sensor instead.

Index of the Y value in the array returned by android.hardware.SensorListener#onSensorChanged

Value: 1

DATA_Z

Added in API level 1
Deprecated in API level 15
static val DATA_Z: Int

Deprecated: use Sensor instead.

Index of the Z value in the array returned by android.hardware.SensorListener#onSensorChanged

Value: 2

GRAVITY_DEATH_STAR_I

Added in API level 1
static val GRAVITY_DEATH_STAR_I: Float

Gravity (estimate) on the first Death Star in Empire units (m/s^2)

Value: 3.5303614E-7f

GRAVITY_EARTH

Added in API level 1
static val GRAVITY_EARTH: Float

Earth's gravity in SI units (m/s^2)

Value: 9.80665f

GRAVITY_JUPITER

Added in API level 1
static val GRAVITY_JUPITER: Float

Jupiter's gravity in SI units (m/s^2)

Value: 23.12f

GRAVITY_MARS

Added in API level 1
static val GRAVITY_MARS: Float

Mars' gravity in SI units (m/s^2)

Value: 3.71f

GRAVITY_MERCURY

Added in API level 1
static val GRAVITY_MERCURY: Float

Mercury's gravity in SI units (m/s^2)

Value: 3.7f

GRAVITY_MOON

Added in API level 1
static val GRAVITY_MOON: Float

The Moon's gravity in SI units (m/s^2)

Value: 1.6f