0

I'm trying to get only 1 event from orientation sensor.

My code is:

@Override
public void onSensorChanged(SensorEvent event) {
Log(event);
unregisterSensor(...);
}

Then in a thread every 1000ms I do:

registerSensor();
Thread.sleep(2000);

The problem is that if I unregister the sensor after at the end of the method (like I've done) then next time (when I re-register the sensor) it will log the same value event catched before (while moving the phone). If I give onSensorChanged the ability to run twice (using a counter), then it detects the right value and logs this value twice.

What's the problem with the method called only once?

Hoconosc
  • 416
  • 9
  • 24

2 Answers2

1

According to your code, in onSensorChanged() you log a myevent, not the event you get as the argument.

Besides, that -- why don't you tell the LocationManager to only send you new events every 1 or 2 seconds instead of all that hassle with registering and unregistering? You can do this using the minTime parameter of the LocationManager#requestLocationUpdates() method.

Chinmay Kanchi
  • 62,729
  • 22
  • 87
  • 114
Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • "myevent" is a mistake of mine, the code is much longer. But I don't need the orientation to get the GPS position of the phone. I use orientation sensor only to get the inclination of the phone. – Hoconosc Oct 12 '12 at 17:20
  • There is a `getOrientation` method or similar in the API to get the phone inclination (I'll look it up later) and post here, if you cannot find it. – Ridcully Oct 12 '12 at 17:31
  • I'm using it, I got it working perfectly. My problem is that it delivers events at stratospheric rates and this will definitely drain the battery. So I'm using the "thread solution". – Hoconosc Oct 12 '12 at 17:33
  • Hm, perhaps, instead of using the Sensor stuff, you could directly read the orientation via `getResources().getConfiguration().orientation` every 2 seconds. See: http://stackoverflow.com/questions/2795833/check-orientation-on-android-phone – Ridcully Oct 12 '12 at 18:43
  • I need the inclination in degrees, not if the phone is portrait or landscape. – Hoconosc Oct 12 '12 at 18:55
  • One new idea - why not constantly listen to the events and just skip them if they are newer than 1 or 2 seconds? And - what most people do, as the values vary a lot - is to calculate some kind of average from the last n events. There's even an example code in the Sensor API Doc. – Ridcully Oct 12 '12 at 19:06
  • This way is not battery friendly because the onSensorChanged is called more than 20 times at second (even if the body of the method is skipped using a condition). At the moment I think the tread solution is the more battery friendly. – Hoconosc Oct 12 '12 at 19:19
0

I wasn't aware of sensor chaching the events, but I'm not surprised either, as I saw similar behaviour in Locationlistener for network provider.

Having said that, I would suggest that you to let the onSensorChanged() be called a few times, drop the first (that is cached) and average (or use a low pass filter) the following ones. You will get better accuracy and a couple of eventes more will not end up your battery :-)

good luck.

Luis
  • 11,978
  • 3
  • 27
  • 35
  • The problem is that if I leave the sensor registered without unregistering and re-registering it every X seconds the onSensorChanged would called more than 20 times at second. The average could be good for accuracy but not good for battery. I was only trying to read the first value correctly and skip the other ones (and do this every X seconds). I think having a thread running is more battery saving that leave sensor registered and onSensorChanged launched >20 times on second. – Hoconosc Oct 13 '12 at 06:54