How to read the pressure sensor?

老子叫甜甜 提交于 2020-04-28 22:42:06

问题


Physical home button press can be detected easily on most of the devices:

...

public class ExampleAccessibilityService extends AccessibilityService {

    ...

    @Override
    protected boolean onKeyEvent(KeyEvent event) {    
        if (event.getKeyCode() == KEYCODE_HOME && event.getAction() == ACTION_DOWN)
            Log.d("Example", "The home key is pressed.");
        return super.onKeyEvent(event);
    }
}

But the code above doesn't work on some devices that have a pressure-sensitive virtual home button. I suppose these Samsung Galaxy devices are affected: S8, S8+, S9, S9+, Note10, Note10+ and Fold. Officially, it is considered a pressure sensor, not a button.

How to read this sensor?

The TYPE_PRESSURE sensor event is related to the barometer, it shows the ambient air pressure in hPa or mbar.

The getPressure() method returns "the size of the capacitive object rather than literal pressure".

I don't need info about the level of pressure, I just want to know if the pressure-sensitive virtual home button is pressed.


回答1:


Technically, it is not considered a sensor, so it can't be used, as far as I know.

for (Sensor s : ((SensorManager) getSystemService(SENSOR_SERVICE)).getSensorList(Sensor.TYPE_ALL))
        Log.d("Sensor", s.getName());

I've logged all the available sensors of a device having a pressure-sensitive virtual home button, but the button isn't on the list. I wonder, How to access sensors not listed by SensorManager?




回答2:


This virtual button is not for application usage and can only be accessed by that Samsung ROM. One can define the pressure required to trigger it. What's the point in reading it anyway? Your device does not have any pressure sensor, aka barometer. What you mean is the touchscreen, which is pressure sensitive - and so the only chance to read it might be the touchscreen driver.

These are the sources for their vendor binaries, for AOSP. Unless building a custom ROM, there probably is no way to bind custom functionality to the touchscreen or to expose the value to the SDK. These sources should also show, what the virtual button actually does, beside emulating a hardware button event.



来源:https://stackoverflow.com/questions/61047609/how-to-read-the-pressure-sensor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!