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 55e39b6
Showing 1 changed file with 16 additions and 11 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(identifier: $identifier): started")

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 All @@ -1554,6 +1556,8 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
baos.toByteArray()
}

BleLogger.d(TAG, "doFirstTimeUse(identifier: $identifier): write user identifier")

val userIdInputStream = ByteArrayInputStream(userIdData)
client.write(userIdBuilder.build().toByteArray(), userIdInputStream)
.ignoreElements()
Expand All @@ -1568,26 +1572,27 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
throw IllegalArgumentException("Invalid deviceTime format: ${ftuConfig.deviceTime}", e)
}
}
BleLogger.d(TAG, "doFirstTimeUse(identifier: $identifier): set local time")
setLocalTime(identifier, calendar)
}
)
} catch (error: Throwable) {
BleLogger.e(TAG, "writeUserIdentifier() error: $error")
emitter.onError(error)
Completable.complete()
BleLogger.e(TAG, "doFirstTimeUse(identifier: $identifier): write user identifier error: $error")
Completable.error(error)
}
}
)
.ignoreElements()
.doOnComplete {
BleLogger.d(TAG, "doFirstTimeUse(identifier: $identifier): completed")
sendTerminateAndStopSyncNotifications(client)
}
.subscribe(
{ emitter.onComplete() },
{ error -> emitter.onError(error) }
)
.doOnError { error ->
BleLogger.e(TAG, "doFirstTimeUse(identifier: $identifier): error $error")
}
} catch (error: Throwable) {
BleLogger.e(TAG, "doConfig() error: $error")
emitter.onError(error)
BleLogger.e(TAG, "doFirstTimeUse(identifier: $identifier): error $error")
return@defer Completable.error(error)
}
}
}
Expand Down

0 comments on commit 55e39b6

Please sign in to comment.