0

I am trying to implement batching of accelerometer sensor and store the x,y,z values in file.I need to store all the accelerometer values.Ex: Accelerometer values for 10min.

Scenario: The device screen is off, the accelerometer is registered as SENSOR_DELAY_GAME and device is in sleep mode.

I have modified the following code from github https://github.com/googlesamples/android-BatchStepSensor.git to get the values

   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();
    }

The readings stored in the files are missing some of the accelerometer values during sleep and are only storing values upto 30 seconds after screen off.

Where should I implement the storing methods to so that I can get all the values of accelerometer?

Rohit_04
  • 21
  • 4

1 Answers1

1

For running a background task even when the app is not visible you should implement a Service.

In it, you will want to make use of wake locks to make sure the resources you need (CPU,...) stay available.

JimmyB
  • 12,101
  • 2
  • 28
  • 44
  • 1
    I want to avoid using wakelocks as it drains the battery.Is there any other way around to store all the values from batching FIFO in file? – Rohit_04 Jun 26 '19 at 10:49
  • I think there's a misconception here: The wake lock does not drain the battery. It's your app/program that *requires* the CPU to run, and the CPU uses power. You just can't have your program continuously do something and at the same time have the CPU sleep. So either use a wake lock or have your program stopped after some time by the OS's power management. – JimmyB Jun 26 '19 at 11:08
  • 1
    Yes.I agree on the power management.So,I wanted to use batching in order to avoid the use of wakelock for sensor readings to store all the reading from FIFO in the file.But I am confused on where to implement the methods fro storing in the logs. – Rohit_04 Jun 26 '19 at 16:33