Sensor topic (1) - related introduction
1. Introduction to sensors:
Speaking of sensors, I believe everyone is familiar with them. For example, WeChat’s shake uses an acceleration sensor;
Definition of sensor: A physical device or biological organ that can detect and feel external signals, physical conditions (such as light, heat, Moderate) or chemical composition (such as smoke), and transmit the detected information to other devices or organs!
Types of sensors: Sensors can be divided from different angles, and the conversion principle (basic physics or chemistry of sensor operation Effect); purpose; output signal and production materials and processes, etc. Generally, they are divided according to their work: physical sensors and chemical sensors. Two categories! Mobile phones are basically equipped with physical sensors. The sensors installed on mobile phones include the following:
- Orientation sensor
- Acceleration Accelerometer sensor
- Gyroscope sensor
- Magnetic field sensor
- Proximity sensor
- Light sensor
- Pressure sensor
- Temperature sensor
- Gravity sensor (introduced in Android 2.3)
- Linear acceleration sensor (Linear acceleration sensor, introduced in Android 2.3)
- Rotation vector sensor (Rotation vector sensor, Android 2.3)
- relative Humidity sensor (Relative humidity sensor, Android 4.0)
- Near field communication (NFC) sensor (introduced in Android 2.3), NFC is different from others and has read and write functions.
Of course, in addition to these, there are others such as heart rate sensors, step sensors, fingerprint sensors, etc. Regarding the sensor types supported by Android devices, please see the official documentation: Sensor Summary part~
2. How to check the sensors supported by your phone?
The types of sensors mentioned above are certainly not available in all mobile phones. The types of sensors installed on each mobile phone And the number may be different. For example, the sensor types supported by the Nexus 5 I have are: Gravity, light, distance, air pressure and gyroscope! Surprisingly, the second generation of moto x has gravity, light, Distance and IR sensors! Regarding the sensor types supported by your mobile phone, you can go to the relevant reviews Websites such as: Zhongguancun Mobile Online, Pacific, etc. Search for your own model to view relevant parameters! Of course, we can also write our own code to see the sensor types supported by our mobile phone~
Code example:
Running renderings:
Code:
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/txt_show" android:layout_width="match_parent" android:layout_height="match_parent" /> </ScrollView></RelativeLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity { private TextView txt_show; private SensorManager sm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE); txt_show = (TextView) findViewById(R.id.txt_show); List allSensors = sm.getSensorList(Sensor.TYPE_ALL); StringBuilder sb = new StringBuilder(); sb.append("此手机有" + allSensors.size() + "个传感器,分别有:\n\n"); for(Sensor s:allSensors){ switch (s.getType()){ case Sensor.TYPE_ACCELEROMETER: sb.append(s.getType() + " 加速度传感器(Accelerometer sensor)" + "\n"); break; case Sensor.TYPE_GYROSCOPE: sb.append(s.getType() + " 陀螺仪传感器(Gyroscope sensor)" + "\n"); break; case Sensor.TYPE_LIGHT: sb.append(s.getType() + " 光线传感器(Light sensor)" + "\n"); break; case Sensor.TYPE_MAGNETIC_FIELD: sb.append(s.getType() + " 磁场传感器(Magnetic field sensor)" + "\n"); break; case Sensor.TYPE_ORIENTATION: sb.append(s.getType() + " 方向传感器(Orientation sensor)" + "\n"); break; case Sensor.TYPE_PRESSURE: sb.append(s.getType() + " 气压传感器(Pressure sensor)" + "\n"); break; case Sensor.TYPE_PROXIMITY: sb.append(s.getType() + " 距离传感器(Proximity sensor)" + "\n"); break; case Sensor.TYPE_TEMPERATURE: sb.append(s.getType() + " 温度传感器(Temperature sensor)" + "\n"); break; default: sb.append(s.getType() + " 其他传感器" + "\n"); break; } sb.append("设备名称:" + s.getName() + "\n 设备版本:" + s.getVersion() + "\n 供应商:" + s.getVendor() + "\n\n"); } txt_show.setText(sb.toString()); } }
3.Sensor sensor related methods and usage routines
From the example in 2, we can roughly summarize how to obtain the Sensor sensor and obtain some information related to the sensor. The process is as follows:
1) Sensor related methods
Step 1: Obtain the sensor manager:
SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE);
- Step 2: Obtain the list of sensor objects of the device:
List allSensors = sm.getSensorList(Sensor.TYPE_ALL);
- Step 3: Iteratively obtain the Sensor object, and then call the corresponding method to obtain the sensor-related information:
for(Sensor s:allSensors){ sensor.getName(); //获得传感器名称 sensor.getType(); //获得传感器种类 sensor.getVendor(); //获得传感器供应商 sensor.getVersion(); //获得传感器版本 sensor.getResolution(); //获得精度值 sensor.getMaximumRange(); //获得最大范围 sensor.getPower(); //传感器使用时的耗电量 }
2) Sensor usage routine
Generally, we rarely say to directly obtain the Sensor, and then obtain the above information! Because it's nothing The most important thing is to obtain the data collected by the sensor, such as obtaining the current atmospheric pressure. Or the values of the three angles of the direction sensor, or the value of the gyroscope, and most of the sensor data collection is The following routine:
~Step 1: Obtain the sensor manager:
SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE);
~Step 2: Call a specific method to obtain the required sensor:
For example, what you get here is the direction sensor. If you want to get the sensor, check the API yourself~:
Sensor mSensorOrientation = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
~Step 3: Implement the SensorEventListener interface and rewrite the onSensorChanged and onAccuracyChanged methods!
onSensorChanged: Will be called back when the value of the sensor changes
onAccuracyChanged: Will be called back when the progress of the sensor changes
@Override public void onSensorChanged(SensorEvent event) { final float[] _Data = event.values; this.mService.onSensorChanged(_Data[0],_Data[1],_Data[2]); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { }
We generally obtain sensor data from this SensorEvent. There is a values variable in this class. The type is Float[]. The variable has at most three elements , and different sensors have different meanings represented by the corresponding elements. For example, the first element in the direction sensor is the azimuth angle value, while the first value in the air pressure sensor is the air pressure value!
~Step 4: The SensorManager object calls registerListener to register the listener:
ms.registerListener(mContext, mSensorOrientation, android.hardware.SensorManager.SENSOR_DELAY_UI);
The method is also very simple, the corresponding parameters: Context object,Sensor sensor object, As well as the accuracy density of sensor’s delay time, there are four optional values:
- SENSOR_DELAY_FASTEST——Delay: 0ms
- SENSOR_DELAY_GAME——Delay: 20ms
- SENSOR_DELAY_UI——Delay: 60ms
- SENSOR_DELAY_NORMAL——Delay: 200ms
Of course, low delay means more frequent vehicle inspections, and it also means More power consumption, if it is not recommended to require very high accuracy Don’t use one that is too high-precision. Generally, use the third one more~Measure it yourself~
~Step 5: Unregister the listener:
is used up Just put it, a good habit, generally we can write it into the destruction method of Activity or Service:
ms.registerListener(mContext, mSensorOrientation, android.hardware.SensorManager.SENSOR_DELAY_UI);
Okay, the routine is very simple~
4. Download the sample code for this section:
Summary of this section:
Okay, this section will introduce you to the sensors in Android and how to understand the sensors supported by your phone. In addition to searching online, you can also write your own code to test. Then it also explains the method and process of obtaining sensor-related information. Finally, We also explained the routines for collecting sensor data. Later we will analyze the usage of some commonly used sensors, so stay tuned~
##