4

onSensorChanged(SensorEvent event)每次调用事件值后,重力传感器值都会增加。值增加,并在一点点时间(大约 1 分钟)后达到 NaN 值。有人可以说为什么会这样吗?代码:

private void recordData(SensorEvent e) {
    String label;
    int currentSensorId = -1;
    switch (e.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        currentSensorId = 0;
        label = LOG_LABEL_ACCELEROMETER;
        break;
    case Sensor.TYPE_GYROSCOPE:
        label = LOG_LABEL_GYROSCOPER;
        currentSensorId = 1;
        break;
    case Sensor.TYPE_GRAVITY:
        label = LOG_LABEL_GRAVITY;
        currentSensorId = 2;
        break;
    case Sensor.TYPE_ROTATION_VECTOR:
        label = LOG_LABEL_ROTATION_VECTOR;
        currentSensorId = 3;
        break;
    default:
        label = "UNKNOWN SENSOR TYPE!";
        break;
    }
    if (currentSensorId == -1)
        return;

    // Force set events frequency to value from config,
    // cause sensor events minimal frequency = 0.2 sec.
    timePassed[currentSensorId] = System.currentTimeMillis()
            - lastSensorEventTime[currentSensorId];
    if (timePassed[currentSensorId] < Config.SENSOR_DELAY_MILLISEC) {
        return;
    } else {
        lastSensorEventTime[currentSensorId] = System.currentTimeMillis();
    }

    if (this.pauseRecordingData || checkSDCardMemory())
        return;
    fileWriter.writeEvent(label, System.currentTimeMillis(), e.values);
}

public void writeEvent(String eventSource, long timestamp, float[] values) {
        Log.d(TAG, "writeEvent(); eventSource = " + eventSource);       


    if ( !this.fileExists ) createNewFile();        
    try {       
        String valuesString = eventSource + "\t" + String.format( "%d\t", timestamp);

        for (int i = 0; i < values.length; i++) {
            valuesString += String.format( "%f\t",values[i]);
            Log.d(TAG, "writeEvent(); values[" + i + "] = " + values[i]);
    }           
        buffWriter.append(valuesString + "\n");
    } catch (IOException e1) {              
          e1.printStackTrace();
          Log.d(TAG,"RECORDING EVENT TO FILE FAILED!");
    }               
}
4

1 回答 1

0

有两种可能:

  1. 最可能的一个:您的代码中有错误,例如,您从未重置变量或添加值(有关详细信息,请阅读http://developer.android.com/guide/topics/sensors/传感器_motion.html )。

  2. 您的重力传感器坏了(要检查,请在另一台设备上尝试)。

如需更多帮助,请发布您的代码,您如何访问和管理从重力传感器读取的值。

编辑:

正如我所提到的,您的代码中有一个错误。传感器读取延迟应设置为可能的选项之一:

int     SENSOR_DELAY_FASTEST     get sensor data as fast as possible
int     SENSOR_DELAY_GAME        rate suitable for games
int     SENSOR_DELAY_NORMAL      rate (default) suitable for screen orientation changes
int     SENSOR_DELAY_UI          rate suitable for the user interface 

而不是自定义值!

感谢您找到解决方案。

于 2012-10-09T12:11:58.420 回答