Over a million developers have joined DZone.

Detecting a Windows Phone physical drop - Part 2 - Detecting the fall

· Mobile Zone

As you already know the theory behind the falling process, it is time to work with the code that will be able to detect the fall itself on the phone.

When the phone is free falling, the value of the composite (net) force is getting really close to zero - that is clearly see on the graph, when the size of the rectangles is shrinking in a matter of seconds.

That being said, I need to detect when the value drops and a threshold is hit. To begin with, I am setting the threshold to 0.5 and it can ultimately be adjusted to a lower value (for example, if the phone falls from a bigger altitude). I am also introducing a new flag - thresholdHit, that holds a Boolean value, determining whether the low threshold is hit and the phone is in a state of a free fall.

The default value is set to false, and I am checking both the current value of the net force and the flag each time the accelerometer reports value changes:

if ((f < 0.5) && (!thresholdHit))
{
    thresholdHit = true;
}

Once I get the flag ready, I need to check when the phone finally hits the ground. This is visible on the graph by a large spike:

This spike goes higher than the stationary graph on collision. So I set the default criticial threshold to be 1.5 and I am checking against it if the lower threshold was hit:

else if ((f > 1.5) && (thresholdHit))
{
    acc.Stop();
    Dispatcher.BeginInvoke(new Action(() => { MessageBox.Show("Hit the ground."); }));
}

After the collision, I can either wait for the graph to normalize, so that the net force value is returned to 1G, or I can perform a specific action directly after the hit.

You should be careful here, because in this case I am making one very important assumption - the phone doesn't hit anything else while falling, which might not always be case. That's why it is important sometimes to check for a constant value on the graph on the completion of the fall.

DO NOT TRY TO DROP YOUR PHONE. Instead, use this custom accelerometer motion and use it with the Windows Phone Emulator.

Some limitations that are present in the platform that might limit the functionality of the application, are the following:

  • No full-fledged background processes that can allow the application to timely alert about a fall.
  • No automated calls to a specific number, so the scenario shown in the initial project, where the owner might become unconscious/hurt and the phone will dial somebody for helpĀ  is not possible at the moment.
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}