2

I am new to Android and I try to do a simple app that can log accelerometer, Gyro and GPS into a single csv file (background) so I could analyze the data later. After some digging in google, all the info I get are kinda old and some are not really working.

So, the questions are:

  1. If I do all the task in UI thread like this https://stackoverflow.com/a/4343827/2985850, I wonder is there a delay between each sensor logging? As I want all the sensor run concurrently at the same sampling rate.

  2. So far, I notice there are 2 ways to accomplish this: using multi-threading or Service. But, (correct me if I'm wrong) the service is also running on UI thread so that mean if I do it the Service way, the logging process also run in a single thread and this might probably cause some delay on the logging process between each sensor?

If there is a better way to accomplish this or any good example feel free to drop 'em here Thanks! :)

Community
  • 1
  • 1
Lcr
  • 75
  • 7

1 Answers1

0

According to my experience I will try to answer your questions

  1. Yes there is time difference between different sensor reading, in that case you have 2 options

    a - You can also put a timestamp on your cvs file (you can get it by event.timestamp)

    b - You can get sensor readings with a static frequency instead of SensorManager.SENSOR_DELAY_GAME (you can give time here as a ms, for example if you write 10 instead of SensorManager.SENSOR_DELAY_GAME your device get sensor reading every 10 ms (100Hz))

  2. I also use it as a service and did not get any delay

Also I want to add one more small thing, if you want to get data for a small amount of time you can always use System.out.println() and it is faster than writing to a file

fakturk
  • 415
  • 7
  • 16
  • If I put it as timestamp isn't it still have a time difference between each reading? I want it like, let say time A, 3 different sensor reading at time A. – Lcr Dec 26 '16 at 22:22
  • One more question about service, in my main activity I will need to start it like startService(xxx). So, if I start services for the 3 sensors one by one, isn't it the loader will put rest on the queue before the first service finish? – Lcr Dec 26 '16 at 22:30
  • you can start all services in one class you don't need to crate service class for each service. Just register all three sensors in this sensorService class. – fakturk Dec 27 '16 at 05:11
  • as you said you will get time difference between them but you can interpolate them or as I suggested you can get sensor readings with a static frequency (but also in this case sometimes small time gaps can be occur but they are in ms level and not affect much) – fakturk Dec 27 '16 at 05:13
  • in my case, all the time gaps are very sensitive...I will need the to combine gyro and accelerometer and get the vibration in every location when the car is moving – Lcr Dec 27 '16 at 21:47
  • As I suggested before you can give your sensor reading gaps instead of writing SensorManager.SENSOR_DELAY_GAME. For example I read sensor data every 10 ms ( SM.registerListener(this, SM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 10);) – fakturk Dec 27 '16 at 21:57
  • Yeap I'm thinking to do so. Thanks for the suggestion – Lcr Dec 28 '16 at 04:08