安卓 : Detect phone fall using accelerometer

标签 android accelerometer

我正在处理一项需要检测手机掉落的任务。下面是我正在使用的代码,但它不符合标准。它只能检测到 10 次中的 1 次。有什么方法可以优化以下代码吗?

@Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        // TODO Auto-generated method stub
        Sensor mySensor = sensorEvent.sensor;

        if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            index++;
            accelValuesX[index] = sensorEvent.values[0];
            accelValuesY[index] = sensorEvent.values[1];
            accelValuesZ[index] = sensorEvent.values[2];
            if(index >= 127){
                index = 0;
                accelManage.unregisterListener(this);
                callFallRecognition();
                accelManage.registerListener(this, senseAccel, SensorManager.SENSOR_DELAY_NORMAL);
            }
        }
    }


    public void callFallRecognition(){
        float prev = 0;
        float curr = 0;
        prev = 10;
        for(int i=11;i<128;i++){
            curr = accelValuesZ[i];
            if(Math.abs(prev - curr) > 20 ){
                Toast.makeText(this, "Fall detected", Toast.LENGTH_LONG).show();
                sendSMS();
            }

        }


    }

最佳答案

发现这种方法更准确..

            accelValuesX[index] = sensorEvent.values[0];
            accelValuesY[index] = sensorEvent.values[1];
            accelValuesZ[index] = sensorEvent.values[2];

            rootSquare=Math.sqrt(Math.pow(accelValuesX[index],2)+Math.pow(accelValuesY[index],2)+Math.pow(accelValuesZ[index],2));
            if(rootSquare<2.0)
            {

            Toast.makeText(this, "Fall detected", Toast.LENGTH_SHORT).show();

            }

引用:http://www.jcomputers.us/vol9/jcp0907-07.pdf

关于安卓 : Detect phone fall using accelerometer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33705240/

相关文章:

java - 为什么我一次只能从 okhttp.Response InputStream 读取 2048 个字节?

ios - 3d 空间中 iOS 设备的运动检测

java - 我应该使用哪个数组/列表来保留静态索引?

android - 跨平台套接字库 - Android 和 IOS

android - 获取所有联系人并获取所有类型的单个联系人姓名

Android - 如何动态更改 fragment 布局

iphone - Android/iOS如何使用传感器确定距离的微小变化?

android - iOS/Android 设备方向(俯仰、偏航、滚动)。用加速度计好还是陀螺仪好?

android - SENSOR_ACCELEROMETER (Android) 中的准确度始终为 0

java - 如何将加速器数据减少到两位小数?