Question

How can I determine whether an Android device has moved? By "moved", I mean that if it is laying still on a table and someone picks it up, I can detect that it has moved. I was thinking of using the gyroscope sensor but am not sure if that is the best solution.

Was it helpful?

Solution

I don't think you have much choices if you want to detect minimal movement. The gyroscope/accelerometer sensor is the way to go. Even there you need to add some filtering, since the accelerometer has some wavering.

GPS is not nearly accurate enough to be used to notice movement from table to hand.

Monitoring the proximity sensor might be of some use, but anything blocking that triggers it, and it really does not tell you if the phone is moving.

If you want to consume a lot of battery, use the camera. Stream some input through a filter, which determines if the image is moving.

OTHER TIPS

You can use accelerometer to solve this. If there is no external force on the device, vector sum of accelerometer sensor values will be only gravity. If there is a change in vectorsum of gravity, then there is a force. If this force is significant, you can assume device is moving.

If vector sum is equal to gravity with +/- threshold its stable lying on table.

Code will look like below, (Note: MovingAverage below means moving average of 50 samples of accelerometer)

bool IsDeviceStill(const sensors_event_t& event)
{
    if (event.type == SENSOR_TYPE_ACCELEROMETER) {
        const vec3_t acc(event.data);
        /*Avoiding square root for better performance*/
        float vecsum = acc.x*acc.x + acc.y*acc.y + acc.z*acc.z; 
        vec3_t variance = mVariance->movingAverage(vecsum);

        vec3_t var = mOffsetAvg->movingAverage(abs(variance.x - vecsum));

        ALOGE("Current variance x:%f AvgVarianc:%f ",variance.x,var.x);
        if(var.x < 2) /*Threshold is 2, Standard deviation should less than 2 */
            mDeviceStill = true;
        else 
            mDeviceStill = false;

    }
    ALOGE("%s Device is still:%d\n", __FUNCTION__, mDeviceStill);
    return mDeviceStill;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top