简体   繁体   中英

How to process Android sensor events with Kotlin Coroutines?

I would like to handle sensor events on Android in a stream-oriented way, preferably with Kotlin's Coroutines.

For the sensor I know how to implement the callback interface SensorEventListener with public void onSensorChanged(SensorEvent event);

How could I send data into a kotlinx.coroutines.channels.Channel out of this?

You can simply have a coroutine put an event into the Channel<SensorEvent> for you as follows.

object SensorEventProcessor : SensorEventListener {

    private val scope = CoroutineScope(Dispatchers.Default)
    private val events = Channel<SensorEvent>(100) // Some backlog capacity

    override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
        // If you need to process this as well, it would be a good idea
        // to wrap the values from this as well as onSensorChanged() into
        // a custom SensorEvent class and then put it on a channel.
    }

    override fun onSensorChanged(event: SensorEvent?) {
        event?.let { offer(it) }
    }

    private fun offer(event: SensorEvent) = runBlocking { events.send(event) }

    private fun process() = scope.launch {
        events.consumeEach {
            // Do something
        }
    }
}

You can also use a channelFlow its still expiremental, but very promising.

    fun getAccelerometerData(): Flow<FloatArray> {
    if (accelerometer == null) {
        return emptyFlow()
    }
    return channelFlow {
        val listener = object : SensorEventListener {
            override fun onSensorChanged(event: SensorEvent?) {
                if (event !== null) {
                    channel.offer(event.values)
                }

            }

            override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
                //
            }
        }
        sensorManager.registerListener(listener, accelerometer, SensorManager.SENSOR_DELAY_FASTEST)

        awaitClose {
            sensorManager.unregisterListener(listener)
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM