0

I am making an app in which am using two sensors.

  1. TYPE_MAGNETIC_FIELD
  2. TYPE_GRAVITY

I initialized the respective sensors and then in onSensorChanged function, am fetching the data and doing the calculations on the same.

I have one simple question, how can I use onAccuracyChanged function to filter out data? I want the data with medium and high accuracy!!

I printed basic statements to see what kind of accuracy am getting while debugging the app.

Code :

`@Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // Do something here if sensor accuracy changes.
        // You must implement this callback in your code.
        //  I initialized mValuen as mValuen = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

        if (sensor == mValuen) {
            switch (accuracy) {
                case 0:
                    System.out.println("Unreliable");
                    break;
                case 1:
                    System.out.println("Low Accuracy");
                    break;
                case 2:
                    System.out.println("Medium Accuracy");
                    break;
                case 3:
                    System.out.println("High Accuracy");
                    break;
            }
        }
    }`

As per my understanding, whenever a sensor reports a new value onSensorChanged function is called. So I can't really call that function explicitly(Even if I could,that will anyway be called upon whenever the sensor reports a new value).

All of my calculations are in that function. How do I filter out data with medium and high accuracy. Thanks.

driftking9987
  • 1,673
  • 1
  • 32
  • 63

1 Answers1

0

Check this answer of mine for more elaborated answer.

Let's say this is the onAccurayChanged function.

public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // Do something here if sensor accuracy changes.
        // You must implement this callback in your code.
        if (sensor == mValuen) {
            switch (accuracy) {
                case 0:
                    System.out.println("Unreliable");
                    con=0;
                    break;
                case 1:
                    System.out.println("Low Accuracy");
                    con=0;
                    break;
                case 2:
                    System.out.println("Medium Accuracy");
                    con=1;

                    break;
                case 3:
                    System.out.println("High Accuracy");
                    con=1;
                    break;
            }
        }
    }

I declared a global variable and kept it as 0. And in the onSensorChanged function, where am doing the required calculations, am putting an if..else condition. If the con value is 1, then only am doing the calculations. Am getting the output. But please let me know if my approach is wrong in some way or other. Thanks.

Community
  • 1
  • 1
driftking9987
  • 1,673
  • 1
  • 32
  • 63