jeudi 8 janvier 2015

How can I monitor multiple sensors on Android?


Vote count:

1




Basic idea: I would like to monitor the gyroscope and the accelerometer at the same time.



private SensorManager mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
private Sensor mAccSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
private Sensor mGyroSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);

@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mGyroSensor, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this, mAccSensor, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}

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

mGyroValues = event.values.clone();

float x = mGyroValues[0];
float y = mGyroValues[1];
float z = mGyroValues[2];

if (Math.abs(x) > 0.2 || Math.abs(y) > 0.2 || Math.abs(z) > 0.2) {
deviceStatus.setText("Your device IS moving");
} else {
deviceStatus.setText("Your device IS NOT moving");
}

} else if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

mAccValues = event.values.clone();

float x = mAccValues[0];
float y = mAccValues[1];
float z = mAccValues[2];
mAccelLast = mAccelCurrent;
mAccelCurrent = FloatMath.sqrt(x * x + y * y + z * z);
float delta = mAccelCurrent - mAccelLast;
mAccel = mAccel * 1.0f + delta;

if (mAccel > 0.2) {
deviceStatus.setText("Your device IS moving");
} else {
deviceStatus.setText("Your device IS NOT moving");
}
}


Theoretically, this should make the deviceStatus (just a TextView) have the text "Your device IS moving" anytime I pick up the phone (via accelerometer data) OR anytime I spin it while it is laying down on the table (via gyroscope data).


However, it does not appear to be changing the text when I spin it on the table. I am outputting the various sensor data to other TextViews so I can confirm that both sensors are sending out data. It just seems to me that only my accelerometer code is executing correctly. Any ideas on what I am doing wrong?



asked 1 min ago

YiweiG

411






How can I monitor multiple sensors on Android?

Aucun commentaire:

Enregistrer un commentaire