This article shares the specific code for Android sensor data acquisition for your reference. The specific content is as follows
Next articleGet wifi list, let's continue to talk about sensors in this article. Still look at the code
We first define a sensor tool class (because I only get some of the sensors here, and I need to get the extra sensors and configure them yourself. The tool class provides different construction methods through overloading):
public class SensorUtils implements SensorEventListener { private SensorManager manager; /** * @param context Multiple sensors * @param sensorList */ public void RegisterSensor(Context context, List<Sensor> sensorList){ manager = (SensorManager) (SENSOR_SERVICE); for(Sensor sensors : sensorList){ (this,sensors,SensorManager.SENSOR_DELAY_NORMAL); } } /** * @param context Single sensor * @param sensor */ public void RegisterSensor(Context context, Sensor sensor){ manager = (SensorManager) (SENSOR_SERVICE); (this,sensor,SensorManager.SENSOR_DELAY_NORMAL); } @Override public void onSensorChanged(SensorEvent event) { switch (()){ case Sensor.TYPE_ACCELEROMETER: //Set callback monitoring for the acceleration sensor (event); break; case Sensor.TYPE_GYROSCOPE: //Set callback monitoring for the gyroscope sensor (event); break; case Sensor.TYPE_MAGNETIC_FIELD: //Set callback monitoring for the magnetic field sensor (event); break; case Sensor.TYPE_PRESSURE: //Set callback monitoring for the barometer sensor (event); break; } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } //Remember to cancel registration after use public void UnRegisterSensor(){ if (manager != null){ (this); } } public interface SensorCallBack{ void acceleratedCallBack(SensorEvent event); void gyroscopeCallBack(SensorEvent event); void magneticFieldCallBack(SensorEvent event); void pressureCallBack(SensorEvent event); } private SensorCallBack sensorCallBack; public SensorUtils(SensorCallBack sensorCallBack){ = sensorCallBack; } }
Then we use the tool class in the activity:
//initializationsensorUtils = new SensorUtils(this); (this, sensorList);
Don't forget to implement callback listening in SensorUtil in activity
public class SensorActivity extends BaseActivity<BaseViewModel, ActivitySensorBinding> implements { //do something }
Then implement the function you want in the callback listening set by yourself
@Override public void acceleratedCallBack(SensorEvent event) { //You can do what you want if (event != null) { float[] values = ; float x1 = values[0]; float y1 = values[1]; float z1 = values[2]; StringBuffer stringBuffer = new StringBuffer(); (getString(.accelerometer_number)).append("\n"); (getString(.x1)).append(x1).append("\n"); (getString(.y1)).append(y1).append("\n"); (getString(.z1)).append(z1); if (!()) { (() -> (())); } if (flagAccelerated) { ().insertSensor(new TableBean(null,(),null,null,null)); } flagAccelerated = false; } } @Override public void gyroscopeCallBack(SensorEvent event) { //You can do what you want if (event != null) { float x2 = [0]; float y2 = [1]; float z2 = [2]; StringBuffer stringBuffer = new StringBuffer(); (getString(.gyroscope_number)).append("\n"); (getString(.x2)).append(x2).append("\n"); (getString(.y2)).append(y2).append("\n"); (getString(.z2)).append(z2); if (!()) { (() -> (())); } if (flagGyroscope) { ().insertSensor(new TableBean(null,null,(),null,null)); } flagGyroscope = false; } } @SuppressLint("DefaultLocale") @Override public void magneticFieldCallBack(SensorEvent event) { //You can do what you want if (event != null) { float x3 = [0]; float y3 = [1]; float z3 = [2]; StringBuffer stringBuffer = new StringBuffer(); (getString(.magnetic_field_number)).append("\n"); (getString(.x3)).append(("%.2f", x3)).append("\n"); (getString(.y3)).append(("%.2f", y3)).append("\n"); (getString(.z3)).append(("%.2f", z3)); if (!()) { (() -> (())); } if (flagMagneticField) { ().insertSensor(new TableBean(null,null,null,(),null)); } flagMagneticField = false; } } @Override public void pressureCallBack(SensorEvent event) { //You can do what you want if (event != null) { float x4 = [0]; String str4 = getString(.pressure_number) + x4; if (!()) { (() -> (str4)); } if (flagPressure) { ().insertSensor(new TableBean(null,null,null,null,str4)); } flagPressure = false; } }
I store the data through the button click event into the database. Note that the sensor's callback is real-time, so if you want to obtain a certain moment, you need to use boolean to determine whether you need to obtain the callback data.
Finally, remember to cancel the listening, because I registered in the activity, so canceling the registration is also done in the activity:
protected void onStop() { (); (); }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.