androidaccelerometer

Android : Detect phone fall using accelerometer


I am working on a task were a phone fall needs to be detected. Below is the code I am using, but its not up to mark. It only detects 1 in 10 times. Is there any way I can optimize the below code?

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

        }


    }

Solution

  • Found this method much more accurate..

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

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