Skip to content

Commit

Permalink
Fix doFirstTimeUse on Android so that it completes when all underlyin…
Browse files Browse the repository at this point in the history
…g operations are completed
  • Loading branch information
korzonkiee committed Dec 6, 2024
1 parent 734c1f1 commit d83a4bb
Showing 1 changed file with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1520,8 +1520,10 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
}

override fun doFirstTimeUse(identifier: String, ftuConfig: PolarFirstTimeUseConfig): Completable {
return Completable.create { emitter ->
return Completable.defer {
try {
BleLogger.d(TAG, "doFirstTimeUse() device: $identifier")

val session = sessionPsFtpClientReady(identifier)
val client = session.fetchClient(BlePsFtpUtils.RFC77_PFTP_SERVICE) as BlePsFtpClient?
?: throw PolarServiceNotAvailable()
Expand All @@ -1537,7 +1539,7 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
}

val ftuInputStream = ByteArrayInputStream(ftuData)
val disposable = client.write(ftuBuilder.build().toByteArray(), ftuInputStream)
return@defer client.write(ftuBuilder.build().toByteArray(), ftuInputStream)
.concatWith(
Completable.defer {
try {
Expand Down Expand Up @@ -1573,21 +1575,21 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
)
} catch (error: Throwable) {
BleLogger.e(TAG, "writeUserIdentifier() error: $error")
emitter.onError(error)
Completable.complete()
Completable.error(error)
}
}
)
.ignoreElements()
.doOnComplete {
BleLogger.d(TAG, "doFirstTimeUse() completed")
sendTerminateAndStopSyncNotifications(client)
}
.subscribe(
{ emitter.onComplete() },
{ error -> emitter.onError(error) }
)
.doOnError { error ->
BleLogger.e(TAG, "doFirstTimeUse() error: $error")
}
} catch (error: Throwable) {
BleLogger.e(TAG, "doConfig() error: $error")
emitter.onError(error)
BleLogger.e(TAG, "doFirstTimeUse() error: $error")
return@defer Completable.error(error)
}
}
}
Expand Down

0 comments on commit d83a4bb

Please sign in to comment.