Question

I've designed an app that tracks when I'm moving using the accelerometer, and may vibrate the phone depending on various thresholds I may have hit. One of the issues I faced is that this vibration directly corrupts the accelerometer data.

This answer suggested using a low pass filter, but I don't think that will work in this application (I need a relatively fast response time, and the vibrations cause large spikes in the data).

The solution I'm currently using is to set a flag to true right before the phone vibrates, call a postDelayed function that will set this back to false after a certain time (1 second in fine), and in the onSensorChanged function, simply return if the flag is true.

In other words:

isPhoneVibrating = true; // Start to vibrate     
phoneVibrator.vibrate(VibratePattern, -1);
mHandler.postDelayed(vibrateSensorOff, 1000); // 1 seconds wait before making false

The vibrateSensorOff only contains

isPhoneVibrating = false;

and like I said, in the onSensorChanged function I return if the flag isPhoneVibrating is true.

I don't like this solution because I need to manage this extra flag in the various states of this app. Are there other possible solutions that don't require an extra flag?

Was it helpful?

Solution

You could have two singletons that keep track if the phone is vibrating or not and manage the Vibrator and the Accelerometer.

The Accelerometer manager should expose the methods that you use for the Accelerometer and should pass on all events if the Vibrator isn't running (this is just like a filter for the accelerometer data). You should have an internal flag for if the phone is vibrating on the Accelerometer manager and methods to set it.

The Vibrator manager should expose the methods that you use for the Vibrator and should also update the Accelerometer manager's flag when the phone starts vibrating.

These are basically wrappers for the Vibrator and Accelerometer instances in your app, but they should allow you to use the Accelerometer without having to worry about the Vibrator flag everywhere in your app (only the manager objects should take care of that flag).

I know this solution does require an extra flag, but it allows you to manage that flag in just two places (in the two singletons) and you don't have to keep track of it in your entire app. If you do implement the two singletons, make sure that you don't use the Vibrator or Accelerometer directly (as you would have to keep track of the vibration flag to do that).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top