Zag Gol
Zag Gol

Reputation: 1076

android interval between sensor listeners

I wrote a simple android code, that listens to acceleration and orientation sensors, and writes the acceleration x,y,z values, and the angel with north.

In the app i am trying to set a 30 seconds interval between sending sensors event to onSensorChanged() function, but it's still updating a few times every second.

this is the code:

package com.example.sensors;

import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;

public class MainActivity extends Activity implements SensorEventListener{

     private SensorManager sensorManager;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          sensorManager = (SensorManager)this.getSystemService(SENSOR_SERVICE);
    }

       @Override
       public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
       }

       @Override
       public void onSensorChanged(SensorEvent event) {
             if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) //
             {
                 float x = event.values[0];
                 float y = event.values[1];
                 float z = event.values[2];
                 Log.i("acceleration:", "x, y, z" + " " + x + " " + y + " " + z + " ");  
             }               
             else if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) //@deprecated. use TYPE_MAGNETIC_FIELD
             {
                 Log.i("orentation: " , "north:" + event.values[0]);
             }
         }

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

         }

         @Override
         protected void onResume() {
           super.onResume();
           // register this class as a listener for the accelerometer sensors and orientation
           sensorManager.registerListener(this,
               sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),//This method get the default sensor for a given type
               30000000);//SensorManager.SENSOR_DELAY_NORMAL
           sensorManager.registerListener(this,
                   sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),//This method get the default sensor for a given type
                   30000000); 
         }

         @Override
         protected void onPause() {
           // unregister listener
           super.onPause();
           sensorManager.unregisterListener(this);
         }

}

Upvotes: 0

Views: 2164

Answers (1)

Pedro S
Pedro S

Reputation: 43

You can't set a custom interval for sensor rates. The slowest rate possible is SENSOR_DELAY_UI, and the samples per second depend on the device. You will need to filter these values by saving the last time you printed the values, then wait until the difference between that time and the current time is over 30 seconds and print again.

Upvotes: 1

Related Questions