简体   繁体   English

Android - 获取接近传感器的最小值和最大值

[英]Android - get proximity sensor min and max value

The app i am building makes use of the proximity sensor. 我正在构建的应用程序使用接近传感器。 Trying it in different phones i found that the proximity sensor has different (min,max) values in different phones (my htc sensation has 0 for covered and 9 for uncovered while xperia has 0 for covered and 1 for uncovered).So the question is: How do i get the proximity sensor's min max values ?? 在不同的手机中尝试它我发现接近传感器在不同的手机中有不同的(最小,最大)值(我的htc感觉有0覆盖,9覆盖未覆盖,而xperia有0覆盖,1覆盖未覆盖)。所以问题是:我如何获得接近传感器的最小值?

I think minimum value for proximity Sensor is 0 for all devices. 我认为接近传感器的最小值对于所有设备都是0 and to get maximum range... 并获得最大范围......

    SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    Sensor proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
    proximitySensor.getMaximumRange();

According documentation : 根据文件:

Some proximity sensors only support a binary near or far measurement.
In this case, the sensor should report its maximum range value in the far state
and a lesser value in the near state

So proximity sensor could even had no min and max values. 因此,接近传感器甚至可以没有最小值和最大值。 Then you should just compare its value with proximitySensor.getMaximumRange() . 然后你应该将它的值与proximitySensor.getMaximumRange()进行比较。

Each proximity sensor is different. 每个接近传感器都不同。 You have to monitor the values in onSensorChanged() and remember min and max value. 您必须监视onSensorChanged()中的值并记住最小值和最大值。 It's working fine. 它工作正常。 Here is my code for checking near state. 这是我检查近状态的代码。

    private float proximityCalibratedMax = Float.MIN_VALUE;

    ...

    public void onSensorChanged(SensorEvent event) {

            if(event.values[0] > proximityCalibratedMax){
                proximityCalibratedMax = event.values[0];
            }

            if (event.sensor.getType() == Sensor.TYPE_PROXIMITY && proximityCalibratedMax != Float.MIN_VALUE) {
                if (event.values[0] < proximityCalibratedMax) {
                    prevNear = true;
                    sensorTimestamp = new Date().getTime();

                } else {
                    long currentTimestamp = new Date().getTime();
                    prevNear = false;
                    openAutifyCarMode();

                }
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM