Rainer Schlonvoigt
08/04/2021, 9:10 AMclass Peripheral {}
class PeripheralFinder {
interface Listener {
fun onPeripheralFound(peripheral: Peripheral)
fun onError(error: Throwable)
}
fun findPeripheral(listener: Listener) {}
}
suspend fun findPeripheralAsync(): Peripheral {
val peripheralFinder = PeripheralFinder()
return withTimeout(5_000) {
suspendCancellableCoroutine { continuation ->
peripheralFinder.findPeripheral(listener = object : PeripheralFinder.Listener {
override fun onPeripheralFound(peripheral: Peripheral) {
continuation.resume(peripheral)
}
override fun onError(error: Throwable) {
continuation.resumeWithException(error)
}
})
}
}
}