Eric Boggs
11/23/2021, 5:18 PMEric Boggs
11/23/2021, 5:23 PMtravis
11/23/2021, 5:26 PMEric Boggs
11/23/2021, 7:51 PMscanScope.launch {
try {
withTimeout(10000) {
scanner
.advertisements
.catch { cause -> Timber.e(cause) }
.filter { it.isHub }
.onCompletion { cause ->
if (cause == null) {
connect()
} else {
// We've failed
}
}
.first { advertisement ->
// This is the advertisement we want, so set it
true
}
}
} catch (e: Exception) {
// We've failed
Timber.e(e)
}
}Eric Boggs
11/23/2021, 7:52 PMtravis
11/24/2021, 6:07 AMscanScope.launch {
val found = withTimeoutOrNull(10_000L) {
scanner
.advertisements
.first { it.isHub }
}
if (found == null) {
// todo: log that scan timed out
return
}
// you may opt to put the follow code in another Coroutine, depends on what the lifecycle is for various BLE operations you're doing:
val peripheral = peripheralScope.peripheral(found)
try {
peripheral.connect()
} catch (e: Exception) {
// todo: log that connection failed
}
// todo: peripheral is in a connected state here
}travis
11/24/2021, 6:12 AMAbortFlowException is normal for the flow completing due to first finding a match.
first is terminal (ends the flow normally) and returns the item that matched.
So the return of the entire flow change, in this case, will be the Advertisement, which you can use to create a Peripheral and connect to.Eric Boggs
11/24/2021, 3:34 PM