The first thing I would check is your time interval. I am not certain it is being converted properly. The timestamp is listed as:
public long timestamp The time in nanosecond at which the event happened
And so in the documentation we see the conversion is done by dividing by 1 billion instead of 1 thousand.
float dT = (event.timestamp - timestamp) / 1000000000.0f;
timestamp = event.timestamp;
That said I think that change will just shrink the values but to calculate real world distance you need to watch your units.
Next, when reading about these things people are always going on about how you need to know your initial position for each reading to make any sense with respect to real world linear measurements. I do not see you tracking your previous acceleration which will make each following measurement's integration based on instantaneous acceleration, or the change in acceleration, instead of full acceleration.
Try something like this:
final int X = 0;
double distance[];
double init_vel[];
double total_Accel[];
void dblIntegrate(SensorEvent event){
double data[] = new double[3];
for(int i = 0; i < event.lenght; i++){
data[i] = (double)event[i];
total_Accel[i] += data[i];
vel[i] = init_vel[i] + (total_Accel[i] * dt);
init_vel[i] = vel[i];
....rinse and repeate to get distance
(not using the accel data of course)
}
}
I realize you understand this but for anyone else is reading this => remember you can't have the counting vars total_Accel or init_vel re-instantiated each time you call dblIntegrate().
If you are doing it right you should see total_Accel go from zero to some max value then back to zero every time you move the device. This means you are adding equal parts positive and negative acceleration each time the device moves in any direction.
I believe this is the property of acceleration that is the hardest to understand properly because for the device to go from resting state to resting state your total_acceleration will go from zero to a max pos/neg value back to zero to the oppposite max value and then back to zero.
For example, if I move a phone resting on its back on a table with the bottom facing my chest to the right for one meter you will get something like this:
total_Accel = 0.0 (0 Meters)
total_Accel = 0.5
total_Accel = 1.0
total_Accel = 1.5 (approx .25 Meters)
total_Accel = 1.0
total_Accel = 0.5
total_Accel = 0.0 (if accel is perfectly distributed, .5 Meters)
total_Accel = -0.5
total_Accel = -1.0
total_Accel = -1.5 (approx .75 Meters)
total_Accel = -1.0
total_Accel = -0.5
total_Accel = 0.0 (1 Meter)
In the example you can start to see why simply doubly integrating the change in acceleration isn't going to give you the real change in velocity/displacement.
I hope so anyway because writing this took a lot longer than I had thought it would! :)