问题
I'm testing the proximity sensor with this code:
final SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
final Sensor proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
sensorManager.registerListener(this, proximitySensor, SensorManager.SENSOR_DELAY_FASTEST);
And I'm getting only 5.0 value when my hand is far away from the sensor and 0.0 when it's completely closed the sensor. (I'm testing on Nexus S, 4.1) Can I get the values between 0 and 5?
回答1:
On many devices, the proximity sensor is essentially boolean - 'near' (0), or 'far' (5), but this is dependent on the actual proximity sensor used.
I don't have a Nexus S, so I can't verify that device, but I'm guessing that it's similar to my Samsung GS3, which reports the following information: Name: GP2A Proximity Sensor Vendor: Sharp Version: 1 Power: 0.75 Maximum Range: 5.0 Resolution: 5.0
As you can see on the GS3, the resolution is the same as the maximum range, so 0 and 5 are the only values that I'm going to get no matter what I do.
You can check out your own device by using an app like 'Android Sensor Box', or by using the Sensor API's getMaximumRange() and getResolution() methods.
回答2:
Also, please note that every manufacturer report different values for the proximity sensor near and far state. On top of that getMaximumRange() for some smartphones returns way higher value than the value reported in onSensorChanged. So you cannot rely on any of the values reported by the phone. The only solution I've come up with, is to monitor the values reported in onSensorChanged, and remember the highest value, and if the value is lower than the highest, then it means the proximity sensor is near an object.
Here are some examples:
Motorola Defy: lowest-3, highest-100, getMaximumRange-2.1474836E9
THL T200: lowest-0, highest-1, getMaximumRange-1
Samsung S5 mini: lowest-0, highest-5, getMaximumRange-4096.0
Nexus S, Sony Z2: lowest-0, highest-5, getMaximumRange-5
来源:https://stackoverflow.com/questions/16876516/proximity-sensor-accuracy