12

I am developing an application for Android Wear. It listens coordinates from an accelerometer sensor and finds a pattern.

To do this, when the user clicks a button, the service starts and begins to store coordinates in a List. Usually, the accelerometer sensor logs 4 to 5 coordinates per second.

The problem is sometimes the onSensorChanged() method does not receive data during several seconds causing losses of data and trouble to find a pattern.

Here is a gist of my service: https://gist.github.com/cpalosrejano/8f0e59e47124275136fc3d5d941faa07

Things I've tried:

  • I am using android:stopWithTask=false to prevent the service to stop when the activity dies.
  • I have also used a WakeLock to prevent the device go to sleep while the services are recording coordinates.

What am I doing wrong? Is there another way to receive callbacks from the accelerometer sensor without causing data loss?

Thanks in advance.

lapinkoira
  • 8,320
  • 9
  • 51
  • 94
Cristian
  • 514
  • 4
  • 21

2 Answers2

1

Its a bit late, but finnaly I found the solution.

I start the service in foreground, with method startForeground(int, Notification) so the service never wont stop. With this fix, you will never lost any SensorEvent.

Cristian
  • 514
  • 4
  • 21
0

What you can do is to write the data to a file on the device and read it from the file.

// When sensor value has changed
@Override
public void onSensorChanged(SensorEvent event){
    if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){

    //Perform a background task to store the data
    new SensorEventLoggerTask().execute(event);

    // And continue to do whatever you want and grab the data
    // The data will be saved in to a file on the device and read it from themre
    }
}
CodeWizard
  • 128,036
  • 21
  • 144
  • 167