I want to collect data from the accelerometer, gyroscope and magnetometer all at once. I am currently using the SensorManager
service which is based on the SensorEvent
. The problem with this is that I can only get the data from 1 sensor at a given point in time. So in time t1 I am getting data from accelerator, in t2 I am getting data from gyroscope and in t3 i am getting data from magnetometer.
My code is as follows:
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
sText.setText("S: " + event.sensor.getStringType());
}
else if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE){
sText.setText("S: " + event.sensor.getStringType());
}
else if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){
sText.setText("S: " + event.sensor.getStringType());
}
}
Instead of having to work with the three sensors at 3 different times, I want to be able to collect the data from all sensors in one time interval. Is this possible?
A little late, but may be useful for others - you can use simple workaround:
select sensors with highest update frequency and "synchronize" all other sensors data with them via global variables. E.g. (for accelerometer as main sensor):
...
float mAccX;
float mAccY;
float mAccZ;
float mGyroX;
float mGyroY;
float mGyroZ;
float mMagX;
float mMagY;
float mMagZ;
...
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
sText.setText("S: " + event.sensor.getStringType());
mAccX = event.values[0];
mAccY = event.values[1];
mAccZ = event.values[2];
// use all sensors data here
processAllSensorsData(mAccX, mAccY, mAccZ,
mGyroX, mGyroY, mGyroZ,
mMagX, mMagY, mMagZ);
}
else if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE){
sText.setText("S: " + event.sensor.getStringType());
mGyroX = event.values[0];
mGyroY = event.values[1];
mGyroZ = event.values[2];
}
else if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){
sText.setText("S: " + event.sensor.getStringType());
mMagX = event.values[0];
mMagY = event.values[1];
mMagZ = event.values[2];
}
}
Or use some kind of interpolation (linear of quadratic), but in that case you get current sensors values with some delay.
And also use separate SensorEventListener
for each sensor.