Search code examples
androidwear-osaccelerometer

Why can't I change the delay in accelerometer collection? Android wear


With a research project, that I am working on, I am required to collect accelerometer data from an android watch (I am using an Asus ZenWatch 3 if that helps) but the problem I am having is that no matter the delay I specify I always receive 100 events / second or 100HZ.

I have tried changing the delay from SensorManager.SENSOR_DELAY_NORMAL, to SensorManager.SENSOR_DELAY_GAME, SensorManager.SENSOR_DELAY_UI, SensorManager.SENSOR_DELAY_FASTEST, and even manually specifying the delay to 20000000 and similar values in microseconds. No matter what I specify the delay as it continues to deliver at a rate of 100HZ. I have trimmed down my code to concisely show what I've done. What am I doing wrong?

Edit: In reference I have been using https://developer.android.com/guide/topics/sensors/sensors_overview#java as a reference for this project. It states that SENSOR_DELAY_NORMAL should be a delay of 200,000 ms although with my tests that is not what I am getting.

public class MainActivity extends WearableActivity implements SensorEventListener {

private ExecutorService cachedPool;
private LinkedList<String> xyzValues = new LinkedList<>();
private final String TAG = MainActivity.class.getName(),DELIM = ",";
private SensorManager mSensorManager;
private byte[] bytes;
private Estimator estimator;
private int puffNum = 0,sessionNum = 0,num = 0, BATCH_SIZE = 500, SENSOR_DELAY = 20000000;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    cachedPool = Executors.newCachedThreadPool(); //part of delivering messages to the phone.

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); //Keeps the app working - no screen off

    initSensors();
}
private void initSensors()
{
    try
    {
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        if(mSensorManager != null) 
        {
            mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        }
    }
    catch(NullPointerException e)
    {
        Log.d(TAG,"FATAL ERROR in initSensors: "+e.getMessage());
    }
}
@Override
public void onSensorChanged(SensorEvent event) //Current sampling rate is 100HZ, taking every 4th point gives 25Hz
{
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        //changes event.timestamp to human readable time.
        String xyzValue = dateFormatted + DELIM + event.values[0] + DELIM + event.values[1] + DELIM + event.values[2] + DELIM + puffNum + DELIM + sessionNum;
        xyzValues.add(xyzValue);
        Log.d(TAG,"xyzValues size:" +xyzValues.size());
        if(xyzValues.size() >= BATCH_SIZE)
        {
            //do the stuff I need such as transmit data
            xyzValues.clear();
        }
    }
}

@Override 
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
    //do something with this
}
@Override
protected void onResume()
{
    super.onResume();
    mSensorManager.registerListener(this, mSensor,SENSOR_DELAY);
    mGoogleApiClient.connect();
}
@Override
protected void onPause()
{
    super.onPause();
    mSensorManager.unregisterListener(this);
    mGoogleApiClient.disconnect();
}

}


Solution

  • Delay values are not always honored. From the documentation:

    samplingPeriodUs int: ... This is only a hint to the system. Events may be received faster or slower than the specified rate. Usually events are received faster...