Search code examples
androidandroid-sensors

android.sensor.pick_up_gesture not working


I'm writing some app, that would require to check when the device is lifted. I know that the usual solution is to listen to SCREEN_ON or something similar, but would really appreciate some input on the problem with SENSOR_STRING_TYPE_PICK_UP_GESTURE.

On android developer, I foudn nothing regarding this sensor, it is not even listed: (https://developer.android.com/guide/topics/sensors/sensors_overview#sensors-intro)

However, on the Android Open Source Project I found some rather promising info: https://source.android.com/docs/core/interaction/sensors/sensor-types#interaction_composite_sensors

Pick up gesture Underlying physical sensors: Undefined (anything low power)

Reporting-mode: One-shot

Low-power

Implement only the wake-up version of this sensor.

getDefaultSensor(SENSOR_TYPE_PICK_UP_GESTURE) returns a wake-up sensor

A pick-up gesture sensor triggers when the device is picked up regardless of wherever it was before (desk, pocket, bag).

Each sensor event reports 1 in sensors_event_t.data[0]. Though, it does not work. Do I need a different SDK or what?

My problem is, that the line given on the site shows an error. In my code, the first line works perfectly fine, can set up listeners and such, while the second will produce an error during build, and shows up red in android developer.

sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.getDefaultSensor(SENSOR_TYPE_PICK_UP_GESTURE);

Im building with

minSdk 27
targetSdk 32

Solution

  • Managed to solve it by changing the targetSdk to 30. (though the constant was still missing, a quick read through the source files gave me the idea to just use the int values, which worked)

    sensorManager.getDefaultSensor(1);
    sensorManager.getDefaultSensor(25);