加速度计中的传感器批处理

时间:2019-06-26 05:28:40

标签: android sensor batching

我正在尝试实现加速度传感器的批处理并将x,y,z值存储在文件中。我需要存储所有加速度传感器值。例如:加速度传感器值持续10分钟。

场景:设备屏幕关闭,加速度计注册为SENSOR_DELAY_GAME,设备处于睡眠模式。

我从github修改了以下代码 https://github.com/googlesamples/android-BatchStepSensor.git获取值

   private String[] mEventDelaysAccelero = new String[EVENT_QUEUE_LENGTH];

 /**
     * Listener that handles step sensor events for step detector and step counter sensors.
     */
    private final SensorEventListener mListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            // BEGIN_INCLUDE(sensorevent)
            // store the delay of this event
            recordDelay(event);

           if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                final String delayString = getDelayStringAccelero();

                String rmsValues="";
                String[] buffer=delayString.split(",");
                for(int i=0;i<buffer.length;i++){
                    String[] buffer1=delayString.split(":");
                    Double x=Double.parseDouble(buffer1[0]);
                    Double y=Double.parseDouble(buffer1[1]);
                    Double z=Double.parseDouble(buffer1[2]);
                     float rms=(float) Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
                    //Store in file
                    Logger.e(MainActivity.context,TAG,i+")RMS="+rms+" Timestamp="+buffer1[3]);
                }
                // Get accelerometer values.
                mSteps = (int) event.values[0];

                // Update the card with the latest values
                getCardStream().getCard(CARD_COUNTING)
                        .setTitle(getString(R.string.counting_title, mSteps))
                        .setDescription(getString(R.string.counting_description,
                                getString(R.string.sensor_accelerometer), mMaxDelay, rmsValues));
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
    };

    /**
     * Records the delay for the event.
     *
     * @param event
     */
    private void recordDelay(SensorEvent event) {
        if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
            // Calculate the delay from when event was recorded until it was received here in ms
            // Event timestamp is recorded in us accuracy, but ms accuracy is sufficient here
            long timeInMillis = (new Date()).getTime();// + (event.timestamp - System.nanoTime()) / 1000000L;
            mEventDelaysAccelero[mEventData] = event.values[0]+":"+event.values[1]+":"+event.values[2]+":"+timeInMillis;
            // Increment length counter
            mEventLength = Math.min(EVENT_QUEUE_LENGTH, mEventLength + 1);
            // Move pointer to the next (oldest) location
            mEventData = (mEventData + 1) % EVENT_QUEUE_LENGTH;

        }
    }

private String getDelayStringAccelero() {
        mDelayStringBuffer.setLength(0);
        // Loop over all recorded delays and append them to the buffer as a decimal
        for (int i = 0; i < mEventLength; i++) {
            if (i > 0) {
                mDelayStringBuffer.append(", ");
            }
            final int index = (mEventData + i) % EVENT_QUEUE_LENGTH;
            final String delay = mEventDelaysAccelero[i];
            mDelayStringBuffer.append(delay);
        }

        return mDelayStringBuffer.toString();
    }

文件中存储的读数在睡眠过程中缺少某些加速度计值,并且仅在屏幕关闭后30秒内存储这些值。

我应该在哪里实现存储方法,以便获得加速度计的所有值?

1 个答案:

答案 0 :(得分:0)

要运行后台任务,即使应用程序不可见,也应实施Service

在其中,您将要使用wake locks来确保所需的资源(CPU,...)保持可用。