如何在传感器中使用onAccuracyChanged函数 [英] How to use onAccuracyChanged function in sensors

查看:207
本文介绍了如何在传感器中使用onAccuracyChanged函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个使用两个传感器的应用程序.

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

  1. TYPE_MAGNETIC_FIELD
  2. TYPE_GRAVITY
  1. TYPE_MAGNETIC_FIELD
  2. TYPE_GRAVITY

我初始化了各个传感器,然后在onSensorChanged函数中,正在获取数据并对其进行计算.

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

我有一个简单的问题,如何使用onAccuracyChanged函数过滤数据?我想要中等和高精度的数据!

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.

代码:

`@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;
            }
        }
    }`

据我了解,每当传感器报告新值时,就会调用onSensorChanged函数.因此我不能真正地显式调用该函数(即使可以,无论何时传感器报告新值都将调用该函数).

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.

推荐答案

检查我的答案以获得更详尽的答案.

Check this answer of mine for more elaborated answer.

假设这是onAccurayChanged函数.

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

我声明了一个全局变量并将其保持为0. 在onSensorChanged函数中,在进行所需的计算时,将置入if..else条件.如果con值为1,则仅在进行计算. 正在获取输出.但是请让我知道我的方法在某种程度上是错误的. 谢谢.

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.

这篇关于如何在传感器中使用onAccuracyChanged函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆