diff --git a/sources/Android/android-communications/library/src/sdk/java/com/polar/sdk/impl/BDBleApiImpl.kt b/sources/Android/android-communications/library/src/sdk/java/com/polar/sdk/impl/BDBleApiImpl.kt index 579b63b2..fba5192f 100644 --- a/sources/Android/android-communications/library/src/sdk/java/com/polar/sdk/impl/BDBleApiImpl.kt +++ b/sources/Android/android-communications/library/src/sdk/java/com/polar/sdk/impl/BDBleApiImpl.kt @@ -1990,9 +1990,7 @@ class BDBleApiImpl private constructor(context: Context, features: Set @@ -2013,18 +2011,22 @@ class BDBleApiImpl private constructor(context: Context, features: Set if (status is FirmwareUpdateStatus.FinalizingFwUpdate) { BleLogger.d(TAG, "Starting finalization of firmware update") - Completable.timer(rebootTriggeredWaitTimeSeconds, TimeUnit.SECONDS) - .andThen( - waitDeviceSessionToOpen(identifier, factoryResetMaxWaitTimeSeconds, if (isDeviceSensor) 0L else 120L) - .andThen( - Completable.fromCallable { - BleLogger.d(TAG, "Restoring backup to device after version ${firmwareUpdateResponse.version}") - sendInitializationAndStartSyncNotifications(client) - backupManager.restoreBackup(backup).subscribe() - } - ) - ) - .andThen(Flowable.just(FirmwareUpdateStatus.FinalizingFwUpdate())) + BleLogger.d(TAG, "Waiting for device session to open after reboot") + waitDeviceSessionToOpen(identifier, factoryResetMaxWaitTimeSeconds, waitForDeviceDownSeconds = 10L) + .andThen(Completable.defer { + BleLogger.d(TAG, "Performing factory reset while preserving pairing information") + return@defer doFactoryReset(identifier, true) + }) + .andThen(Completable.defer { + BleLogger.d(TAG, "Waiting for device session to open after factory reset") + return@defer waitDeviceSessionToOpen(identifier, factoryResetMaxWaitTimeSeconds, waitForDeviceDownSeconds = 10L) + }) + .andThen(Completable.defer { + BleLogger.d(TAG, "Restoring backup to device after version ${firmwareUpdateResponse.version}") + sendInitializationAndStartSyncNotifications(client) + return@defer backupManager.restoreBackup(backup) + }) + .andThen(Flowable.just(status)) } else { Flowable.just(status) }