7

I am trying to figure out the best way to monitor the accelerometer sensor with a polling rate of less than .25 milliseconds. I have implemented a UI option for the user to switch to a constant monitoring state and made it clear of the battery drain ramifications. Would a remote service be the best way over a daemon thread because of the way Android handles cleaning up memory and threads? The point is to make the accelerometer monitored as close to constantly as possible, battery drain be damned. And this monitoring needs to be long running, maybe even more than 24 hours straight, again I realize the power consumption consequences. Any suggested reading or code snippets will be appreciated.

Just a newbe looking for advice from the wisdom of the Android community. Thanks in advance,

-Steve

CLARIFICATION: I am trying to detect the instant there is a change in acceleration. My code discriminates by axis, but getting real time data from the accelerometer is my goal.

Kingsolmn
  • 1,868
  • 2
  • 23
  • 39
  • the accel on android is a Sensor btw. It pushes, you can't sensibly pull values from it! – Blitz Jan 20 '11 at 21:18
  • @LordT - On a re-read I realize that I could have phrased better. I am using a custom Listener Interface to receive the updates from the accel sensor. Thanks for pointing that out. – Kingsolmn Jan 20 '11 at 22:14
  • oh, okay, that makes more sense. As said, I'd go with a Service for this - using a Remote service will allow you to run in the background savely – Blitz Jan 20 '11 at 23:19
  • Steve - what did you learn from long running monitoring of accelerometer events? How much of drain was it? Did it vary much between devices? – brunobowden Sep 11 '13 at 08:04
  • @brunobowden I was able to find a solution that did not significantly impact battery/cpu/heat generation while monitoring > 24 hrs. The solution seems to have the same benefits regardless of device/OS version. Unfortunately the solution I came up with is proprietary, but I came to this solution based on other accelerometer based questions on SO. As far as power consumption, try http://stackoverflow.com/a/5177427/549510 – Kingsolmn Oct 02 '13 at 16:37

2 Answers2

2

We did this using Android Services - they can be started from an activity but remain running in the background. That's probably what you're looking for!
Some Howtos:

  1. http://developerlife.com/tutorials/?p=356
  2. http://developer.android.com/guide/topics/fundamentals.html#procthread
Blitz
  • 5,521
  • 3
  • 35
  • 53
  • Even though the poster is not interested in battery drain, but I'm a bit curious - how bad was it? – Demiurg Feb 17 '12 at 20:51
  • I don't remember the exact numbers, but for our full setup, we got about 10 hours out of a G1 (back in THOSE days :)) - However, that was crunching heartrate and other numbers at >300Hz and transmitting over bluetooth. I think we got more than 16 hours from just using the accelerometer – Blitz Feb 21 '12 at 14:38
  • 1
    What about device hibernation? Once the device goes to sleep and shuts those sensors you will not get any sensor updates. Did you run with wake lock all the time? – MikeL Feb 11 '15 at 14:19
1

Using a specific thread to monitor and wait is the best solution that gives you flexibility on the wait period. This is quite efficient as it does not requires any specific service.

class MonitorThread extends Thread {
    ...
    public void run() {
        for (;;) {
            long ms = 0;
            int nanos = 250000;
            ... // Do something or compute next delay to wait
            try {
                Thread.sleep(ms, nanos);
            } catch (InterruptedException ex) {
            }
        }
    }
}

See http://developer.android.com/reference/java/lang/Thread.html

You specify a very short delay (.250 ms) so this will be CPU intensive. You can probably use the result of the accelerometer to increase or reduce this delay. For example, if you detect that there is no acceleration, increase the delay (it's up to you but 100ms seems reasonable or even higher). As soon you detect something, reduce the delay. All this depends on your application.

ciceron
  • 586
  • 5
  • 10
  • On android, i would really use a handler for this, if you really needed this! – Blitz Jan 20 '11 at 21:19
  • @ciceron- I am trying to detect the instant there is a change in acceleration. My code discriminates by axis, but getting real time data from the accelerometer is the goal. Thanks for the suggestion on using the accel data to control the sleep interval, Good idea! – Kingsolmn Jan 20 '11 at 21:28