Introduction to sensor types in Android

Speaking of sensors, we are more familiar with the gravity sensor and light sensor of the mobile phone. Because many of us sense the rotation of the mobile phone, and the screen goes off when close to the ear when making a call, these are all due to the sensor. Today I will introduce the types of sensors in Android and explain how to obtain the sensors supported by the current mobile phone.

1. Types of sensors in Android

serial number Sensor type of Sensor Sensor name Description
1 TYPE_ACCELEROMETER Acceleration Often used for shake function
2 TYPE_MAGNETIC_FIELD magnetic field  
3 TYPE_ORIENTATION direction

Deprecated, replaced by getOrientation method

4 TYPE_GYROSCOPE Gyro Used to sense the rotation and tilt of the phone
5 TYPE_LIGHT Light Used to sense the intensity of the light on the front of the phone
6 TYPE_PRESSURE pressure Used to sense air pressure
7 TYPE_TEMPERATURE temperature Deprecated, replaced by type 13
8 TYPE_PROXIMITY distance  
9 TYPE_GRAVITY gravity  
10 TYPE_LINEAR_ACCELERATION Linear acceleration  
11 TYPE_ROTATION_VECTOR Rotation vector  
12 TYPE_RELATIVE_HUMIDITY Relative humidity  
13 TYPE_AMBIENT_TEMPERATURE Ambient temperature  
14 TYPE_MAGNETIC_FIELD_UNCALIBRATED No calibrated magnetic field  
15 TYPE_GAME_ROTATION_VECTOR Uncalibrated rotation vector  
16 TYPE_GYROSCOPE_UNCALIBRATED Uncalibrated gyroscope  
17 TYPE_SIGNIFICANT_MOTION Special action  
18 TYPE_STEP_DETECTOR Walk detection An event is triggered every time the user takes a step
19 TYPE_STEP_COUNTER Walk count Record the number of steps after activation
20 TYPE_GEOMAGNETIC_ROTATION_VECTOR Geomagnetic rotation vector  
21 TYPE_HEART_RATE Heart rate Use of wearable devices, such as bracelets
22 TYPE_TILT_DETECTOR Tilt detection  
23 TYPE_WAKE_GESTURE Wake up gesture  
24 TYPE_GLANCE_GESTURE Swipe gesture  
25 TYPE_PICK_UP_GESTURE Pick up gesture  

2. Get an example of all the sensor types of the current mobile phone

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv_sensor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>

MainActivity.java

public class MainActivity extends BaseActivity {

    private TextView tv_sensor;

    @Override
    protected MvcBaseModel getModelImp() {
        return null;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initWidget() {
        tv_sensor = findViewById(R.id.tv_sensor);
        SensorManager mSensorMgr = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        // 获取当前设备支持的传感器列表
        List<Sensor> sensorList = mSensorMgr.getSensorList(Sensor.TYPE_ALL);
        String content = "";
        for (int i = 0; i < sensorList.size(); i++) {
            if (sensorList.get(i).getType() == Sensor.TYPE_ACCELEROMETER) {
                //加速度传感器
                content += i + "." + "加速度传感器" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_MAGNETIC_FIELD) {
                //磁场传感器
                content += i + "." + "磁场传感器" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_ORIENTATION) {
                //方向传感器
                content += i + "." + "方向传感器" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_GYROSCOPE) {
                //陀螺仪
                content += i + "." + "陀螺仪" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_LIGHT) {
                //光线
                content += i + "." + "光线" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_TEMPERATURE) {
                //温度
                content += i + "." + "温度" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_PRESSURE) {
                //压力
                content += i + "." + "压力" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_PROXIMITY) {
                //距离
                content += i + "." + "距离" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_GRAVITY) {
                //重力
                content += i + "." + "重力" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
                //线性加速度
                content += i + "." + "线性加速度" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_ROTATION_VECTOR) {
                //旋转矢量
                content += i + "." + "旋转矢量" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_RELATIVE_HUMIDITY) {
                //相对湿度
                content += i + "." + "相对湿度" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) {
                //环境温度
                content += i + "." + "环境温度" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED) {
                //无标定磁场
                content += i + "." + "无标定磁场" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_GAME_ROTATION_VECTOR) {
                //无标定旋转矢量
                content += i + "." + "无标定旋转矢量" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_GYROSCOPE_UNCALIBRATED) {
                //未校准陀螺仪
                content += i + "." + "未校准陀螺仪" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_SIGNIFICANT_MOTION) {
                //特殊动作
                content += i + "." + "特殊动作" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_STEP_DETECTOR) {
                //步行检测
                content += i + "." + "步行检测" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_STEP_COUNTER) {
                //步行计数
                content += i + "." + "步行计数" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_STEP_COUNTER) {
                //步行计数
                content += i + "." + "步行计数" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR) {
                //地磁旋转矢量
                content += i + "." + "地磁旋转矢量" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == Sensor.TYPE_HEART_RATE) {
                //心跳速率
                content += i + "." + "心跳速率" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == 22) {//Sensor.TYPE_TILT_DETECTOR
                //倾斜检测
                content += i + "." + "倾斜检测" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == 23) {//Sensor.TYPE_WAKE_GESTURE
                //唤醒手势
                content += i + "." + "唤醒手势" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == 24) {//Sensor.TYPE_GLANCE_GESTURE
                //掠过手势
                content += i + "." + "掠过手势" + ":" + sensorList.get(i).getName() + "\n";
            } else if (sensorList.get(i).getType() == 25) {//Sensor.TYPE_PICK_UP_GESTURE
                //抬起手势
                content += i + "." + "抬起手势" + ":" + sensorList.get(i).getName() + "\n";
            } else {
                content += i + "." + sensorList.get(i).getType() + ":" + sensorList.get(i).getName() + "\n";
            }
        }
        tv_sensor.setText(content);
    }
}

 

Guess you like

Origin blog.csdn.net/weixin_38322371/article/details/115298996