Get multiple sensor data in Android at the same time

Now I'm trying to create a vibration monitoring application. I use the accelerometer to complete the task, when the recorded acceleration exceeds a certain threshold, I call it a trigger. When there is a trigger, I want to register data on acceleration, magnetic field, light level (from different sensors) at the time of launch to the file.

Now the problem is that I can receive data from a separate sensor, but I could not understand how to receive data from several sensors at the same time. For example: can I configure a sensor to monitor accelerometer changes when I record acceleration data, can I also get data from other sensors at the same time?

Thanks in advance.

+5
source share
1 answer

Yes, you can do it like this:

private SensorManager manager;
private SensorEventListener listener;

manager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
listener = new SensorEventListener() {
    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        Sensor sensor = event.sensor;
        if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            ...
        }
        else if (sensor.getType() == Sensor.TYPE_GYROSCOPE) {
            ...
        }
    }
}

manager.registerListener(listener, manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
manager.registerListener(listener, manager.getDefaultSensor(TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
+6
source

All Articles