sampleSensor

suspend fun sampleSensor(context: Context, sensorType: Int, durationMs: Int? = null): SensorSampleResult

Registers a listener on the default sensor of sensorType and collects readings.

When durationMs is null, returns as soon as the first sample arrives (waiting at most SINGLE_READING_TIMEOUT_MS). Otherwise collects for durationMs (clamped 1-5000) measured by a timer — not by incoming events — so on-change sensors (light, proximity) that only report when their value changes still complete on time with whatever was collected. If a timed window elapses with no samples, waits an extra WINDOW_GRACE_MS for a first one.

The listener is unregistered on every path, including coroutine cancellation.

Parameters

context
sensorType

a Sensor.TYPE_* constant.

durationMs

sampling window in ms, or null for a single reading.