title: Android: How to control onSensorChange sampling rate
body: <p>I am a bit puzzled by the sensor reading rates in Android. The code below reports delays of ~53 ms (ZTE Blade, rate sensor events is set to SENSOR_DELAY_FASTEST). </p>
  
  <pre><code>public void onSensorChanged(SensorEvent event) {
   synchronized (this) {
    switch (event.sensor.getType()) { 
     case Sensor.TYPE_MAGNETIC_FIELD:
       TimeNew = event.timestamp;
       delay = (long)((TimeNew - TimeOld)/1000000);
       TimeOld = TimeNew;
       Log.d("Test", delay + " ms");
      break;
      }
     }
    }
  </code></pre>
  
  <p>The log:</p>
  
  <pre><code>DEBUG/Test(23024): 52 ms
  DEBUG/Test(23024): 53 ms
  DEBUG/Test(23024): 54 ms
  DEBUG/Test(23024): 56 ms
  DEBUG/Test(23024): 52 ms
  DEBUG/Test(23024): 52 ms
  DEBUG/Test(23024): 55 ms
  DEBUG/Test(23024): 52 ms
  </code></pre>
  
  <p>If we want to average say 100 samples, and then save the data, the time between each 100th sample will vary significantly. That is presumably because the sensor value is not changing in regular time periods.</p>
  
  <p>But am I missing something ? Is there a way to get (more) regular measurements (e.g. 100 ms) ? Or perhaps I should go for averaging over a specific period of time instead of no.of samples ?</p>
  
  <p>Also 50ms seems to be a bit long time. Could that be hardware limitation of the device ? Will this number vary on different platforms ?</p>
  
  <p>Any advice is appreciated.</p>