0

Is there a way to get the values from an Android sensor say every 5 minutes?

For example, is there a way I can poll the AMBIENT_TEMPERATURE sensor every 5 minutes regardless of there being a change in temperature or not?

Right now, I can only access the values from onSensorChanged(SensorEvent arg0) when it gets called so if the temperature didn't change from start up, I never can access the temperature value?

Thanks! =]

corgichu
  • 2,488
  • 3
  • 27
  • 43
  • have you read this? http://stackoverflow.com/questions/6985396/is-there-any-android-api-to-find-sense-room-temperature-programmatically-in-andr – sschrass Sep 07 '12 at 21:30

2 Answers2

0

As far as I recall the senors return a value every onSensorChanged-event. You can set up a thread, that reads in the values every 5 minutes.

Edit:

public class OrientationHandler implements SensorEventListener {
    private SensorManager sensorManager = null;
    private static float[] values = new float[3];

    public OrientationHandler(Context context) {
        sensorManager = (SensorManager) context
                        .getSystemService(Context.SENSOR_SERVICE);
        sensorManager.registerListener(this, sensorManager
                                       .getDefaultSensor(Sensor.TYPE_ORIENTATION),
                                       SensorManager.SENSOR_DELAY_FASTEST);
    }

    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
            synchronized (this) {
                OrientationHandler.values = event.values;
            }
        }
    }
}

OrientationHandler.values can be accessed from outside the class (if you write yourself a public Getter-method).

sschrass
  • 6,331
  • 6
  • 39
  • 53
  • yeah, but what it it never changed so it doesn't get called ever? My problem is getting the first value. I wrote up a test app to try it out and I can't get the "ambient temperature" value because `onSensorChanged` never got called? – corgichu Sep 07 '12 at 21:11
  • save the values in the onSensorChanged(SensorEvent event) to a class variable that can be accessed from outside. – sschrass Sep 07 '12 at 21:15
  • so onSensorChanged(SensorEvent event) will only update that class variable. – sschrass Sep 07 '12 at 21:16
  • oh *now* I've got your problem. Damn. Its late over here. – sschrass Sep 07 '12 at 21:28
  • but I never even get to `onSensorChanged` so there's no way to initialize that class variable with anything useful. I just read the link you posted and yeah.. the battery temperature would be pretty useless for me. Thanks for the help though! =] – corgichu Sep 07 '12 at 21:40
  • have you tried something like Log.i(TAG, "onSensorChanged was called") inside onSensorChanged() to check if it is called initially? If it will not be called I would file a bug report. – sschrass Sep 08 '12 at 08:23
0

You could try using an alarm service an setting it repeating every five minutes.

I never worked with sensors though, anyway I think there is some way to get the sensor value once.

I suggest you to read this first and then this to learn how to set a repeating alarm which on it's broadcast receiver could update your sensor info.

Hope this helps =)

Max Rasguido
  • 447
  • 5
  • 26