Search code examples
androidaccelerometer

Reading Accelerometer data in Android


What would be the best method in introducing a delay in the accelerometer readings of an Android application. This code will be running in the intent service and it will run for long periods of time (3-6 hours at least). I'd like to make it resource efficient as well. I'm using a Thread.Sleep() for now and I was wondering if there would be a better method to do this.

Also in the example provided in the documentation, they use a high pass filter to filter out the gravity. But would it be possible to consider the sum of all the readings on the 3 axes to be grater than 10 and simply subtract that value from the sum? (I'm only interested in the net acceleration on the phone not the directions).

Thanks!


Solution

  • Dear god do NOT use sleep to delay accelerometer readings. That won't actually delay readings, it will just make you react to them slower- the system will still be making readings at the same rate and in fact queue them up. If you want less frequent readings, specify so when you set up your sensor listener. It takes a frequency parameter.

    You wouldn't sum the 3 and subtract 10- you'd sum their squares and subtract 100 (or more exactly, 9.8^2). But even that isn't quite right, as sensors aren't perfect, and an at rest device may not read exactly 9.8 (especially if you aren't at sea level- the gravity is less in Denver than it is in New York). Instead you should take a long term average and find out what that particular device reads at rest. Which might be what a high pass filter was doing, although I'd have to see the code to know for sure.