Android:使用加速度计检测手机跌落

问题描述 投票:0回答:2

我正在执行一项需要检测手机跌落的任务。下面是我正在使用的代码,但它不符合标准。它仅检测到十分之一。有什么办法可以优化下面的代码吗?

@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();
            }

        }


    }
android accelerometer
2个回答
3
投票

发现这个方法更准确..

            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


0
投票

您经验中最准确的跌倒检测解决方案

© www.soinside.com 2019 - 2024. All rights reserved.