Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

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.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}