Brief Introduction: RECENTLY, I met a project that needed to use sensors. I did some research on sensors. Here I summarize the use of sensors in Android.

For sensors, there are three physical axes that may be used with respect to the device, explained here:

There are three types of sensors, but the values retrieved by some sensor methods may be calculated based on multiple basic sensors. Here is only a superficial distinction:

Motion sensors

Motion sensor: Measures acceleration and rotation forces along three axes. This category includes accelerometers, gravity sensors, gyroscopes and rotation vector sensors.

  • TYPE_ACCELEROMETER: Measures the acceleration force m/s2 applied to the device on all three physical axes (x, Y, and Z), including gravity.

  • TYPE_GRAVITY: Measures the gravity m/s2 applied to the device on all three physical axes (x, y, z).

  • TYPELINEARACCELERATION: Measures the acceleration force m/s2 applied to the device on all three physical axes (X, Y and Z), excluding gravity.

Environmental sensors

Ambient sensors: Measure various environmental parameters, such as ambient air temperature and pressure, lighting and humidity. This category includes barometers, photometers and thermometers.

  • TYPE_LIGHT: Measures the ambient light level (irradiance) in lx units.

  • TYPE_PRESSURE: hPa or MBAR is used to measure ambient air pressure.

  • TYPE_PROXIMITY: measures the proximity (cm) of an object relative to the device view screen. The sensor is usually used to determine if the phone is being held to a person’s ear.

  • TYPERELATIVEHUMIDITY: Measures relative environmental humidity in percentage (%).

  • TYPEAMBIENTTEMPERATURE: Measures ambient temperature in degrees Celsius (°C).

  • TYPETEMPERATURE: Measures the device temperature in degrees Celsius (°C). This sensor implementation varies from device to device, and this sensor has been replaced with a TYPEAMBIENT_TEMPERATURE sensor in API level 14

Position sensors

Position sensors: These sensors measure the physical position of the device. This category includes direction sensors and magnetometers.

  • TYPE_GYROSCOPE: Measures the rotation rate of a device in rad/s around each of the three physical axes (X, Y, and Z).

  • TYPEMAGNETICFIELD: Measures the ambient geomagnetic field in μT for all three physical axes (x, y, z).

  • TYPEROTATIONVECTOR: Measures the direction of the device by providing three elements of the device rotation vector.

  • TYPE_ORIENTATION: The degree of rotation of the measurement device around all three physical axes (x, Y, z). Starting with API level 3, you can obtain the inclination matrix and rotation matrix of the device by using gravity and geomagnetic sensors and the getRotationMatrix () method. (Also use position sensor)

For sensor monitoring, system API encapsulation is very convenient, just need to implement interface rewrite method, here to monitor acceleration as an example, the rest are very similar:

Sensor.TYPE_ACCELEROMETER

1. Obtain the SensorManager and register the listener


     

    private lateinit var sensorManager: SensorManager

    override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)

    sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager

    sensorManager.registerListener(

    this,

    sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),

    SensorManager.SENSOR_DELAY_NORMAL

    )

    }

Copy the code

2. Implement SensorEventListener rewrite method


     

    override fun onAccuracyChanged(sensor: Sensor? , accuracy: Int) {

    Log.e("xx", "onAccuracyChanged")

    }

    override fun onSensorChanged(event: SensorEvent?) {

    if (event? .sensor? .type == Sensor.TYPE_ACCELEROMETER) {

    val xAccelerate = event.values? .get(0)

    val yAccelerate = event.values? .get(1)

    val zAccelerate = event.values? .get(2)

    Log.e("xx", "xAccelerate:$xAccelerate\nyAccelerate:$yAccelerate\nzAccelerate:$zAccelerate")

    }

    }

Copy the code

Simple as that, but remember to stop listening to the sensor during the corresponding life cycle!!

Android guide:

https://developer.android.com/guide/topics/sensors/sensorsenvironment

Android various sensor use:

https://blog.csdn.net/weixin38379772/article/details/79069494

The complete code address: https://github.com/loosaSH/android-sensors

–END–

Identify the QR code, follow us