1

I'm developing an app which needs access to 'the last 5 seconds worth of data' for a given sensor. My plan is to store this data somehow, then when I request the data, it'll return any data obtained within the last 5 seconds. I'm not sure how to do this effectively, given that:

  1. Dalvik doesn't like having lots of objects being created, so I can't make an object for each sensor reading which contains the value + timestamp.

  2. I need to be constantly storing new data in some sort of structure, whilst retrieving a specific portion of it.

My only other requirement is that data which is older than 5 seconds should be disposed of. This isn't really critical, but I imagine retaining the data would eat at memory.

I currently have a onSensorChanged listener set up, so I don't need help with that.

Any ideas?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Glitch
  • 2,785
  • 1
  • 27
  • 50
  • I'm assuming you're working with [SensorManager](http://developer.android.com/reference/android/hardware/SensorManager.html), why not polling with a [Timer](http://developer.android.com/reference/java/util/Timer.html) every second, storing those values wherever you see fit, then just querying such repository when needed? – Nano Taboada Oct 09 '11 at 05:40
  • have take a look at SensorEventListener (http://developer.android.com/reference/android/hardware/SensorEventListener.html) so when you get new data until it changes. I would advice to save this information locally on a db and access through Cursor using LoaderManager – Necronet Oct 09 '11 at 05:40
  • @NanoTaboada That's the problem, what repository to use, and how to query it? – Glitch Oct 09 '11 at 05:46
  • @Necronet Yeah I'm using onSensorChanged, I accidentally wrote the wrong method in the question. Updating now. I'll take a look at using a database. – Glitch Oct 09 '11 at 05:47
  • Why not giving [SQLDroid](http://code.google.com/p/sqldroid/) a try? – Nano Taboada Oct 09 '11 at 05:48

1 Answers1

1

maybe pre-allocate enough objects to store 5 seconds of data and store them in a pool. get objects from the pool, add time stamp and value and insert into sorted set. remove any old objects after you insert a one and put them back into the pool. or maybe you can get by with just a circular buffer http://en.wikipedia.org/wiki/Circular_buffer

Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
  • One problem I can see doing this is that I have no idea how many objects I'll need. I guess I could limit the pollrate of the sensor, but that's there's no way for me to know what `SENSOR_DELAY_FASTEST` etc. really means, so the upper bound is indeterminate. I guess I could just "overwrite" objects as they fill up, but I'd rather avoid that if there's a clean way to do it. – Glitch Oct 09 '11 at 05:31
  • run some tests or do some calculations and get some idea of how many you might need. maybe allocate more if needed. – Ray Tayek Oct 09 '11 at 05:41
  • Ok, that should work. Is there a class I should be looking at for implementing a 'pool'? – Glitch Oct 09 '11 at 05:51
  • 1
    Try this: [How would you code an efficient Circular Buffer in Java or C#](http://stackoverflow.com/questions/590069/how-would-you-code-an-efficient-circular-buffer-in-java-or-c) – Nano Taboada Oct 09 '11 at 06:06
  • @Glitch - the circular buffer sounds like it would be the easiest to get up and running and may have the smallest memory footprint - you could use two arrays, one for the timestamp and one for the data – Ray Tayek Oct 09 '11 at 08:01
  • @glitch i was thinking two arrays one for the time and one for the data – Ray Tayek Oct 10 '11 at 00:09
  • @RayTayek What advantage would that have over a 2D array? Wouldn't that make it harder to associate the time with the sample? Also, when I'm reading from the circular buffers, there's a good chance it'll be written to at the same time. Should I make a temporary 'snapshot' of the buffer every 5 seconds to do my calculations? – Glitch Oct 10 '11 at 08:35
  • @glitch - 2d array vs 2 arrays - the index will tie the timestamp and sample together. yes, you do have to worry about race conditions - you will need to use synchronized or a semaphore. – Ray Tayek Oct 10 '11 at 18:07
  • Ok, cool. Since I don't mind old data being overwritten, I think it may be easier for me to just have a queue, and then when it fills up I just dequeue before enqueuing the new value. This means I don't have to worry about keeping track of pointers - it's probably easier to synchronize too. – Glitch Oct 11 '11 at 06:16