Skip to content

Commit

Permalink
allow refreshWatchMetadata() retries, reconnect on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
crc-32 committed Jul 4, 2024
1 parent df37df4 commit 87033db
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ class ConnectionLooper @Inject constructor(
_watchPresenceState.value = null
}

fun tryReconnect() {
try {
lastConnectedWatch?.let {
connectToWatch(it)
}
} catch (e: SecurityException) {
Timber.e(e, "Failed to trigger reconnect due to permissions issue")
}
}

@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
fun connectToWatch(macAddress: String) {
coroutineScope.launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,29 @@ class SystemHandler @Inject constructor(
}

private suspend fun refreshWatchMetadata() {
try {
withTimeout(5000) {
val watchInfo = systemService.requestWatchVersion()
//FIXME: Possible race condition here
ConnectionStateManager.connectionState.value.watchOrNull?.metadata?.value = watchInfo
watchMetadataStore.lastConnectedWatchMetadata.value = watchInfo
val watchModel = systemService.requestWatchModel()
watchMetadataStore.lastConnectedWatchModel.value = watchModel
var retries = 0
while (retries < 3) {
try {
withTimeout(3000) {
val watchInfo = systemService.requestWatchVersion()
//FIXME: Possible race condition here
ConnectionStateManager.connectionState.value.watchOrNull?.metadata?.value = watchInfo
watchMetadataStore.lastConnectedWatchMetadata.value = watchInfo
val watchModel = systemService.requestWatchModel()
watchMetadataStore.lastConnectedWatchModel.value = watchModel
}
break
} catch (e: TimeoutCancellationException) {
Timber.e(e, "Failed to get watch metadata, retrying")
retries++
} catch (e: Exception) {
Timber.e(e, "Failed to get watch metadata")
break
}
} catch (e: Exception) {
Timber.e(e, "Failed to get watch metadata")
}
if (retries >= 3) {
Timber.e("Failed to get watch metadata after 3 retries, giving up and reconnecting")
connectionLooper.tryReconnect()
}
}

Expand Down

0 comments on commit 87033db

Please sign in to comment.