This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add event handling support to KeepAliveGatt (#74)
- Loading branch information
Showing
7 changed files
with
634 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2020 JUUL Labs, Inc. | ||
*/ | ||
|
||
package com.juul.able.keepalive | ||
|
||
import com.juul.able.gatt.GattIo | ||
|
||
sealed class Event { | ||
|
||
/** Triggered upon a connection being successfully established. */ | ||
data class Connected(val gatt: GattIo) : Event() | ||
|
||
/** | ||
* Triggered either immediately after an established connection has dropped or after a failed | ||
* connection attempt. | ||
* | ||
* The [connectionAttempt] property represents which connection attempt iteration over the | ||
* lifespan of the [KeepAliveGatt]. The value begins at 1 and increase by 1 for each iteration. | ||
* | ||
* @param wasConnected is `true` if event follows an established connection, or `false` if previous connection attempt failed. | ||
* @param connectionAttempt is the number of connection attempts since creation of [KeepAliveGatt]. | ||
*/ | ||
data class Disconnected( | ||
val wasConnected: Boolean, | ||
val connectionAttempt: Int | ||
) : Event() | ||
|
||
/** | ||
* Triggered when the connection request was rejected by the operating system (e.g. bluetooth | ||
* hardware unavailable). [KeepAliveGatt] will not attempt to reconnect until | ||
* [connect][KeepAliveGatt.connect] is called again. | ||
*/ | ||
data class Rejected(val cause: Throwable) : Event() | ||
} | ||
|
||
suspend fun Event.onConnected(action: suspend GattIo.() -> Unit) { | ||
if (this is Event.Connected) action.invoke(gatt) | ||
} | ||
|
||
suspend fun Event.onDisconnected(action: suspend (Event.Disconnected) -> Unit) { | ||
if (this is Event.Disconnected) action.invoke(this) | ||
} | ||
|
||
suspend fun Event.onRejected(action: suspend (Event.Rejected) -> Unit) { | ||
if (this is Event.Rejected) action.invoke(this) | ||
} |
Oops, something went wrong.