observationExceptionHandler
Registers an ObservationExceptionHandler for the Peripheral. When registered, observation failures are passed to the ObservationExceptionHandler instead of through observation flows. Any exceptions in the ObservationExceptionHandler will propagate through (and terminate) the associated observation flow.
Some failures are due to connection drops before the connection state has propagated from the system, the ObservationExceptionHandler can be useful for ignoring failures that precursor a connection drop:
Peripheral(advertisement) {
observationExceptionHandler { cause ->
// Only propagate failure if we don't see a disconnect within a second.
withTimeoutOrNull(1_000L) {
state.first { it is Disconnected }
} ?: throw IllegalStateException("Observation failure occurred.", cause)
println("Ignored failure associated with disconnect: $cause")
}
}
Content copied to clipboard