在Android中组合多个传感器时相同的值



我想同步加速度计和陀螺仪传感器。我为此制作了一个Android应用程序,并在数据库中插入了传感器的值。我尝试了这个代码片段,但是它重复了相同的值。

@Override
protected void onCreate(Bundle savedInstanceState) {
private boolean isGyroscopeSensorPresent = false;
private boolean isAccelerometerSensorPresent = false;
//sensor gyroscope
if(sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) != null) {
gyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
isGyroscopeSensorPresent = true;
} else {
isGyroscopeSensorPresent = false;
}
//sensor accelerometer
if(sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null) {
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
isAccelerometerSensorPresent = true;
} else {
isAccelerometerSensorPresent = false;
}
}
@Override
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
x_axis = event.values[0];
y_axis = event.values[1];
z_axis = event.values[2];
} if (sensor.getType() == Sensor.TYPE_GYROSCOPE) {
gyro_x_axis = event.values[0];
gyro_y_axis = event.values[1];
gyro_z_axis = event.values[2];
}
if (isAccelerometerSensorPresent && isGyroscopeSensorPresent){
mDBHelper.insert(x_axis, y_axis, z_axis, gyro_x_axis, gyro_y_axis, gyro_z_axis, time_gyro, subject, alphabet);
}
}

数据库输出如何解决这个重复的值问题?我只是想创建一个数据库与值写在橙色像在图片(如果你注意值写在橙色不重复)。如果你能帮忙,我会很高兴的。

谢谢。

传感器可能会在一段时间内报告,即使没有值的变化(例如,设备未触碰桌子),所以检查这个在你身边

@Override
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
float x_axis = this.x_axis, y_axis = this.y_axis, z_axis = this.z_axis;
if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
x_axis = event.values[0];
y_axis = event.values[1];
z_axis = event.values[2];
}
float gyro_x_axis = this.gyro_x_axis, gyro_y_axis = this.gyro_y_axis,
gyro_z_axis = this.gyro_z_axis;
if (sensor.getType() == Sensor.TYPE_GYROSCOPE) {
gyro_x_axis = event.values[0];
gyro_y_axis = event.values[1];
gyro_z_axis = event.values[2];
}
boolean exaclySame = this.x_axis == x_axis &&
this.y_axis == y_axis &&
this.z_axis == z_axis &&
this.gyro_x_axis == gyro_x_axis &&
this.gyro_y_axis == gyro_y_axis &&
this.gyro_y_axis == gyro_z_axis;
if (isAccelerometerSensorPresent && isGyroscopeSensorPresent && !exaclySame){
this.x_axis = x_axis;
this.y_axis = y_axis;
this.z_axis = z_axis;
this.gyro_x_axis = gyro_x_axis;
this.gyro_y_axis = gyro_y_axis;
this.gyro_z_axis = gyro_z_axis;
mDBHelper.insert(x_axis, y_axis, z_axis, gyro_x_axis, gyro_y_axis, gyro_z_axis, time_gyro, subject, alphabet);
}
}

,例如this.x_axis == x_axis行,this.x_axis指在类中声明的变量,x_axis指在方法中声明的局部变量

相关内容

  • 没有找到相关文章

最新更新