Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

KeepAliveGatt EventHandler #74

Merged
merged 35 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
716dea5
KeepAliveGatt now takes in a onEventAction, allowing the consumer of …
sdonn3 Jul 9, 2020
72baad9
merge changes from travis' PR
sdonn3 Jul 9, 2020
aaa739d
Some changes to the README to reflect changes in KeepAliveGatt
sdonn3 Jul 9, 2020
785c709
Changes from PR
sdonn3 Jul 10, 2020
934c824
Add API changes
sdonn3 Jul 10, 2020
fe650b1
test format
sdonn3 Jul 10, 2020
7598997
Merge branch 'develop' into steve/esc-965_onDisconnect
sdonn3 Jul 10, 2020
21f6320
fix test
sdonn3 Jul 10, 2020
0adefb9
Merge remote-tracking branch 'origin/steve/esc-965_onDisconnect' into…
sdonn3 Jul 10, 2020
1e1aa35
changes
sdonn3 Jul 13, 2020
acd9291
PR feedback requests
sdonn3 Jul 14, 2020
76b07d1
connectionAttempt fix
sdonn3 Jul 14, 2020
29109b5
Code clean up
twyatt Jul 15, 2020
2368bd6
Drop Disconnected.Info and put properties directly on Disconnected event
twyatt Jul 15, 2020
2ae24d4
API dump
twyatt Jul 15, 2020
c04b2cd
Kotlinter
twyatt Jul 15, 2020
c2e9421
Add test that assumes connection rejection should yield Disconnected …
twyatt Jul 15, 2020
4ef5191
Code clean up and add Rejected event
twyatt Jul 15, 2020
8c8b54c
Add documentation re: states and events
twyatt Jul 15, 2020
803cdad
Simplify example in documentation
twyatt Jul 15, 2020
b1f4654
API dump
twyatt Jul 15, 2020
d26fafd
Kotlinter
twyatt Jul 15, 2020
0b80726
Reduce size of state and event flow diagram
twyatt Jul 15, 2020
521af43
Merge branch 'develop' into steve/esc-965_onDisconnect
twyatt Jul 15, 2020
6421846
Fix reconnect test
twyatt Jul 15, 2020
05ba36e
Settle on disconnected state on exception in onConnected
twyatt Jul 16, 2020
bec6b43
Fix which scope is cancelled due to onConnected exception
twyatt Jul 16, 2020
fe24455
Remove test that is no longer valid
twyatt Jul 16, 2020
560802d
Remove nested scopes and launch in `establishConnection`
twyatt Jul 16, 2020
70357d7
Minor documentation updates
twyatt Jul 16, 2020
f3e0e91
couple new tests
sdonn3 Jul 16, 2020
cb9b61d
Unwrap special exception for Disconnected event
twyatt Jul 16, 2020
c0e38ee
Test clean up
twyatt Jul 16, 2020
045f845
Update state and event flow diagram
twyatt Jul 16, 2020
a89e741
Properly propagate failure through Disconnected state
twyatt Jul 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions keep-alive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ fun CoroutineScope.keepAliveGatt(
| `androidContext` | The Android `Context` for establishing Bluetooth Low-Energy connections. |
| `bluetoothDevice` | `BluetoothDevice` to maintain a connection with. |
| `disconnectTimeoutMillis` | Duration (in milliseconds) to wait for connection to gracefully spin down (after `disconnect`) before forcefully closing. |
| `onEventAction ` | Actions to perform upon either connection or disconnection. `onConnect` is performed after connection, and `onDisconnect` |
| | is performed after a disconnection, either post connection-state or after a failure to connect. |
| `onEvent` | Actions to perform on various events within the KeepAliveGatt's lifecycle. (onConnected and onDisconnected) |

For example, to create a `KeepAliveGatt` as a child of Android's `viewModelScope`:

Expand All @@ -34,10 +33,30 @@ class ExampleViewModel(application: Application) : AndroidViewModel(application)
private val gatt = viewModelScope.keepAliveGatt(
application,
bluetoothAdapter.getRemoteDevice(MAC_ADDRESS),
disconnectTimeoutMillis = 5_000L // 5 seconds
disconnectTimeoutMillis = 5_000L, // 5 seconds
onEvent = { event ->
event.onConnected {
// Actions to perform on initial connect *and* subsequent reconnects:
discoverServicesOrThrow()
}
event.onDisconnected { disconnectInfo ->
// Actions to perform on disconnect, or after a failed connection attempt
if (disconnectInfo.wasConnected) {
/*
True if this disconnection event follows a successful connection, false
if it represents a failed connection attempt.
*/
} else if (disconnectInfo.connectionAttempt > 1) {
/*
Value tracking the current connection attempt in the life span of the
keepAliveGatt. Starts at 1 and increments by 1 for each connection attempt,
whether successful or not.
*/
}
}
}
) {
// Actions to perform on initial connect *and* subsequent reconnects:
discoverServicesOrThrow()

}

fun connect() {
Expand Down
5 changes: 1 addition & 4 deletions keep-alive/src/main/java/KeepAliveGatt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,7 @@ class KeepAliveGatt internal constructor(
val successfullyConnected = spawnConnection()
onEvent?.invoke(
Event.Disconnected(
DisconnectInfo(
successfullyConnected,
connectionAttempts++
)
DisconnectInfo(successfullyConnected, connectionAttempts++)
)
)
}
Expand Down
4 changes: 2 additions & 2 deletions keep-alive/src/test/java/KeepAliveGattTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ class KeepAliveGattTest {
fun `When connection is dropped after being connected, onDisconnect's value reflects that`() =
runBlocking {
val bluetoothDevice = mockBluetoothDevice()
val gatt1 = mockk<Gatt> {
val gatt = mockk<Gatt> {
every { onCharacteristicChanged } returns flow {
delay(2_000)
throw Exception("Test losing connection")
Expand All @@ -414,7 +414,7 @@ class KeepAliveGattTest {
mockkStatic(BLUETOOTH_DEVICE_CLASS) {
coEvery {
bluetoothDevice.connectGatt(any())
} returns ConnectGattResult.Success(gatt1)
} returns ConnectGattResult.Success(gatt)

val job = Job()
var disconnectInfo: DisconnectInfo? = null
Expand Down