onSenserChanged function not called in android studio

24 Views Asked by At

I am making a application to measure the distance my android phone, but I don't know why my onSensorChanged function is not called while running.

Log.d("safik","sadfas;dfkasjf"); -> I added this line check if the function is called of not and I got that this function is not getting called.

I have used 3 sensors in this project(accelerometer, gravity and orientation).

the code was running fine but all of a sudden the project was not running, after some time I got to know that the onSensorChanged function was not getting called.

here is my code.

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;


import android.annotation.SuppressLint;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements SensorEventListener {
    private TextView textview;
    private SensorManager sensorManager;
    private double axx,ayy,azz,ax1,ay1,az1,ax2,ay2,az2,ax3,ay3,az3,ax4,ay4,az4,a;

    private CountDownTimer mCountDownTimer;
    private double zThetaDegree,xThetaDegree,yThetaDegree,zTheta,xTheta,yTheta;
    private double gravity;
    private double distace = 0,u = 0,dt = 0,time1 = 0,converter = 1000000000,d = 0;
    private boolean stage1,stage2,stage3,stage4,p,n;
    private int repeat;

    private Sensor sensorAcceleration,sensorOrientation,sensorGravity;


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textview = findViewById(R.id.textView);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensorAcceleration = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
        sensorOrientation = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        sensorGravity = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);


    }


    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        Log.d("safik","sadfas;dfkasjf"); 



        if(sensorEvent.sensor.getType()==Sensor.TYPE_GRAVITY){
            gravity = Math.sqrt(Math.pow(sensorEvent.values[0],2)+Math.pow(sensorEvent.values[1],2)+Math.pow(sensorEvent.values[2],2));

            Log.d("gravity",Double.toString(gravity));
        }

        if(sensorEvent.sensor.getType()==Sensor.TYPE_ORIENTATION){
            zThetaDegree = (double) sensorEvent.values[0];
            xThetaDegree = (double) sensorEvent.values[1];
            yThetaDegree = (double) sensorEvent.values[2];

            zTheta = (zThetaDegree * 3.1415)/180;
            xTheta = (xThetaDegree * 3.1415)/180;
            yTheta = (yThetaDegree * 3.1415)/180;

            Log.d("theta",Double.toString(xTheta));
        }

        if (sensorEvent.sensor.getType()==Sensor.TYPE_LINEAR_ACCELERATION){

            dt =(SystemClock.elapsedRealtimeNanos()) - time1;
            dt = dt/(double) 1000000000;
            time1 = SystemClock.elapsedRealtimeNanos();

            if(dt > 100000){
                dt = 0;
            }


            ax1 = (double) sensorEvent.values[3];
            ay1 = (double) sensorEvent.values[4];
            az1 = (double) sensorEvent.values[5];

            //filtering rotation around y axis
            az2 = (az1 * Math.cos(yTheta)) - (ax1 * Math.sin(yTheta));
            ax2 = (az1 * Math.sin(yTheta)) + (ax1 * Math.cos(yTheta));
            ay2 = ay1;

            //filtering rotation around x axis

            az3 = (az2 * Math.cos(xTheta)) - (ax2 * Math.sin(xTheta));
            ax3 = ax2;
            ay3 = (ay2 * Math.cos(xTheta)) + (az2 * Math.sin(xTheta));

            //filtering rotation around z axis

            az4 = az3;
            ax4 = (ax3 * Math.cos(zTheta)) - (ay3 * Math.sin(zTheta));
            ay4 = (ay3 * Math.cos(zTheta)) + (ax3 * Math.sin(zTheta));

            //filtering gravity

            az4 = az4 - gravity;
            Log.d("az4", Double.toString(az4));



            a = az4;


            if(a == 0){
                stage1 = true;
            }

            if(stage1){
                if(a != 0){
                    stage2 = true;
                    stage1 = false;
                    if(a > 0){
                        p = true;
                        n = false;
                    }
                    if(a < 0){
                        n = true;
                        p = false;
                    }
                }
            }

            if(stage2){
                if(p){
                    if(a < 0){
                        stage3 = true;
                        stage2 = false;
                    }
                }
                if(n){
                    if(a > 0){
                        stage3 = true;
                        stage2 = false;
                    }
                }
            }

            if(stage3){
                if(a == 0){
                    stage4 = true;
                    stage3 = false;
                }
            }

            if(stage4){
                u = 0;
                stage4 = false;
            }

            if(a < 0){
                if(-a < 0.09){
                    a = 0;
                }
            }
            if(a > 0){
                if(a < 0.09){
                    a = 0;
                }
            }


            if(a == 0){
                repeat = repeat + 1;
                if(repeat >= 10){
                    u = 0;
                    repeat = 0;
                }
            }
            else{
                repeat = 0;
            }

            u = u + a*dt;
            distace = u*dt + ((a*dt*dt)/2);

            d = d + distace;



            textview.setText("distance is" + d + "   " + a);
            Log.d("aaa", "d:" + Double.toString(d) + "   " + "distance:" + Double.toString(distace) + "   " + "    " + "u:" + Double.toString(u) + "     " + "a:" + Double.toString(a) + "    " + "dt:" + Double.toString(dt) + "    " + Double.toString(SystemClock.elapsedRealtimeNanos()));
        }


    }




    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }

    @Override
    protected void onResume(){
        super.onResume();
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY),SensorManager.SENSOR_DELAY_NORMAL);


    }

    protected void onPause(){
        super.onPause();

        sensorManager.unregisterListener(this);
    }
}
0

There are 0 best solutions below