Reputation: 1098
I am seeing proximity sensor detection behavior on physical Android devices that is so far off from what I am expecting that I must be missing some foundational huge thing. Here's what I see:
I wrote a simple app that displays the proximity sensor value.
activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/proxValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
MainActivity.java
:
public class MainActivity extends AppCompatActivity implements SensorEventListener {
TextView proxValue;
SensorManager sensorManager;
Sensor proximitySensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
proxValue = (TextView) findViewById(R.id.proxValue);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
proxValue.setText(String.valueOf(event.values[0]));
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
In the device emulator, when I move the virtual sensor's proximity slider around, it works as I would expect: the float value displayed on screen corresponds to the slider value.
However, when I run this on a physical device (devices tested listed below), I see two unexpected behaviors:
Edit: to clarify, the behavior I expected is that when my hand waves close to the front of the phone (without making physical contact, say hovering 1"), the proximity sensor is able to indicate that it's 1" away. Think, for example, a push-up tracker app that you lay on the ground and do push ups on top of, that uses the proximity sensor to determine that you've sufficiently lowered to count as a push up.</edit>
Meanwhile, the phone app's proximity sensor behaves correctly/as-expected: while in a phone call, if I place the phone near my head (without making physical contact), the screen will black out.
Related, I tried this proximity sensor test app with same problematic results on physical devices.
Android version 14/API 34
Devices tested:
Thanks in advance for any help.
Upvotes: 0
Views: 272