Sensors in Android (total)

Nowadays, many functions of mobile phones are completed by sensors, such as mobile phones can automatically adjust the screen brightness (light sensor), and the function of shaking the mobile phone (acceleration sensor). But do you know what are the sensors in Android?

According to Android official display, Android sensors can be divided into three categories

The Android platform supports three types of sensors:

  • motion sensor

    These sensors measure acceleration and rotational forces along three axes. This category includes accelerometers, gravity sensors, gyroscopes, and rotation vector sensors.

  • environmental sensor

    These sensors measure various environmental parameters such as ambient air temperature and pressure, illuminance and humidity. This category includes barometers, photometers and thermometers.

  • position sensor

    These sensors measure the physical location of the device. This category includes orientation sensors and magnetometers.

Common sensors currently supported by smartphones

sensor name in Java local interface name value
Accelerometer TYPE_ACCELEROMETER                 SENSOR_TYPE_ACCELEROMETER   1
Magnetic field sensor TYPE_MAGNETIC_FIELD SENSOR_TYPE_MAGNETIC_FIELD  2
direction sensor TYPE_ORIENTATION SENSOR_TYPE_ORIENTATION  3
Gyro sensor TYPE_GYROSCOPE SENSOR_TYPE_GYROSCOPE   4
light sensor TYPE_LIGHT SENSOR_TYPE_LIGHT  5
Pressure Sensor TYPE_PRESSURE SENSOR_TYPE_PRESSURE     6
Temperature Sensor TYPE_AMBIENT_TEMPERATURE SENSOR_TYPE_TEMPERATURE 7
distance sensor TYPE_PROXIMITY SENSOR_TYPE_PROXIMITY    8

Next, let's talk about the three types of sensors

Type 1: Motion Sensor

The possible architectures of the sensors vary depending on the sensor type:

  • Gravity, Linear Acceleration, Rotation Vector, Active Motion, Step Counter and Step Detector sensors are hardware or software based.
  • Accelerometer and gyroscope sensors are always hardware based.

The second type: environmental sensor

The Android platform provides four sensors for environmental sensors that allow you to monitor various environmental attributes. You can use these sensors to monitor relative ambient humidity, illuminance, ambient pressure, and ambient temperature near your Android device. All four environmental sensors are hardware-based and only available after the device manufacturer builds them into the device.

The third type: distance sensor

The Android platform provides distance sensors with two sensors that allow you to determine the location of the device: the geomagnetic field sensor and the accelerometer. The Android platform also provides a sensor that allows you to determine the distance between the device's face and an object (called a proximity sensor ). Geomagnetic field sensors and proximity sensors are hardware based. Most phone and tablet manufacturers include geomagnetic field sensors. Likewise, phone manufacturers often include a proximity sensor to determine when the phone is held close to the user's face (for example, during a call). To determine a device's orientation, you can use readings from the device's accelerometer and geomagnetic field sensors.

Get started with the introduction

sensor frame

class/interface Introduction
SensorManager You can use this class to create an instance of the sensor service. This class provides various methods for accessing and listing sensors, registering and unregistering sensor event listeners, and obtaining orientation information. This class also provides several sensor constants for reporting sensor accuracy, setting the data acquisition rate, and calibrating the sensor.
Sensor You can use this class to create an instance of a specific sensor. This class provides various methods that allow you to determine the sensor's capabilities.
SensorEvent The system uses this class to create sensor event objects that provide information about sensor events. A sensor event object includes the following information: raw sensor data, the type of sensor that generated the event, the accuracy of the data, and the timestamp of the event.
SensorEventListener You can use this interface to create two callback methods to receive notifications (sensor events) when sensor values ​​change or sensor accuracy changes.

Get the sensor manager object

// 获取传感器管理者对象
SensorManager mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

Get the specified sensor object

//获取全部的传感器
List list = sensorManager.getSensorList(Sensor.TYPE_ALL);
// 获取指定的传感器
Sensor sensor = mSensorManager.getDefaultSensor(int type);
//例:获取加速度传感器
Sensor sensor=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

Unregister

//解除注册--不要忘记了,把它加在合适的地方
sensorManager.unregisterListener(this,sensor);

Get full sensor code

public class SensorActivity extends AppCompatActivity {

    private SensorManager sensorManager;
    private List<Sensor> list;
    private Button mGetSensor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor);
        mGetSensor = findViewById(R.id.get_sensor);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        list = sensorManager.getSensorList(Sensor.TYPE_ALL);
        mGetSensor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSensor();
            }
        });
    }

    public void setSensor() {
        Log.d("传感器:", "一共有" + list.size() + "个传感器");
        for (int i = 0; i < list.size(); i++) {
            String sensortext = "对应的type" + list.get(i).getType()
                    + "  设备名称:" + list.get(i).getName()
                    + "  设备版本:" + list.get(i).getVersion()
                    + "  供应商:" + list.get(i).getVendor();
            switch (list.get(i).getType()) {
                case Sensor.TYPE_ACCELEROMETER:
                    Log.d("传感器:", "加速度传感器-->" + sensortext);
                    break;
                case Sensor.TYPE_GYROSCOPE:
                    Log.d("传感器:", "陀螺仪传感器-->" + sensortext);
                    break;
                case Sensor.TYPE_LIGHT:
                    Log.d("传感器:", "环境光线传感器-->" + sensortext);
                    break;
                case Sensor.TYPE_MAGNETIC_FIELD:
                    Log.d("传感器:", "电磁场传感器-->" + sensortext);
                    break;
                case Sensor.TYPE_ORIENTATION:
                    Log.d("传感器:", "方向传感器-->" + sensortext);
                    break;
                case Sensor.TYPE_PRESSURE:
                    Log.d("传感器:", "压力传感器-->" + sensortext);
                    break;
                case Sensor.TYPE_PROXIMITY:
                    Log.d("传感器:", "距离传感器-->" + sensortext);
                    break;
                case Sensor.TYPE_TEMPERATURE:
                    Log.d("传感器:", "温度传感器-->" + sensortext);
                    break;
                case Sensor.TYPE_GRAVITY:
                    Log.d("传感器:", "重力传感器-->" + sensortext);
                    break;
                case Sensor.TYPE_LINEAR_ACCELERATION:
                    Log.d("传感器:", "线性加速度传感器-->" + sensortext);
                    break;
                case Sensor.TYPE_ROTATION_VECTOR:
                    Log.d("传感器:", "旋转矢量传感器-->" + sensortext);
                    break;
                case Sensor.TYPE_RELATIVE_HUMIDITY:
                    Log.d("传感器:", "周围环境相对湿度传感器-->" + sensortext);
                    break;
            }
        }
    }
}

Detailed methods of using various sensors will be discussed later

Portal

The sensor in Android---acceleration sensor

The sensor in Android --- magnetic field sensor

The sensor in Android --- light sensor

The sensor in Android --- temperature sensor

 

Guess you like

Origin blog.csdn.net/lanrenxiaowen/article/details/108051656