Accuracy is always 0 in SENSOR_ACCELEROMETER (Android)
I'm programming a application that needs to get data from the accelerometer sensor.
I have overwritten both onAccuracyChanged() and onSensorChanged() functions. It is suppoused that those functions are only called when accuracy or values of the sensor changes, but in my case they change many times per second, and always returning the same values.
@Ov开发者_StackOverflowerride
public void onAccuracyChanged(Sensor sensor, int accuracy) {
if(sensor.getType()==Sensor.TYPE_ACCELEROMETER){
Log.d(TAG, "Accuracy has changed: "+accuracy);
}
}
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
x = event.values[SensorManager.DATA_X];
y = event.values[SensorManager.DATA_Y];
z = event.values[SensorManager.DATA_Z];
Log.d(TAG, "Sensor changed: "+ event.accuracy+", "+x+", "+y+", "+z);
}
}
This is annoying, but it is also annoying that the accuracy is always 0 (SensorManager.SENSOR_STATUS_UNRELIABLE), so I can't trust it.
What can be happening? The device is a Samsung Galaxy S, so it has accelerometer.
Thanks!
u can the value returned is in float value and very accurate u can just convert them to integer or even use condition to get result only when the phone in specific position
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
x = event.values[SensorManager.DATA_X];
y = event.values[SensorManager.DATA_Y];
z = event.values[SensorManager.DATA_Z];
if( x > -3 && x < 3 && y > -3 && y < 3 && z < -7 && z > -13 ){
// this will return value only when the phone is upside down on the table
// u can set as ur own liking
Log.d(TAG, "Sensor changed: "+ event.accuracy+", "+x+", "+y+", "+z);
}
}
}
精彩评论