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..9c123cb9 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 @@ -1991,7 +1991,7 @@ class BDBleApiImpl private constructor(context: Context, features: Set if (status is FirmwareUpdateStatus.FinalizingFwUpdate) { - waitDeviceSessionToOpen(identifier, factoryResetMaxWaitTimeSeconds, if (isDeviceSensor) 0L else 60L) + waitDeviceSessionWithPftpToOpen(identifier, factoryResetMaxWaitTimeSeconds, if (isDeviceSensor) 0L else 60L) .andThen(Flowable.just(FirmwareUpdateStatus.FwUpdateCompletedSuccessfully(firmwareUpdateResponse.version))) } else { Flowable.just(status) @@ -2403,12 +2403,12 @@ class BDBleApiImpl private constructor(context: Context, features: Set - BleLogger.e(TAG, "Timeout reached while waiting for device session to open, deviceId: $deviceId") + BleLogger.e(TAG, "waitDeviceSessionWithPftpToOpen(): error $error") emitter.onError(error) }) emitter.setCancellable { @@ -2436,6 +2441,22 @@ class BDBleApiImpl private constructor(context: Context, features: Set Completable { - BleLogger.trace("Wait for device session to open, timeoutSeconds: \(timeoutSeconds), waitForDeviceDownSeconds: \(waitForDeviceDownSeconds)") + private func waitDeviceSessionWithPftpToOpen(deviceId: String, timeoutSeconds: Int, waitForDeviceDownSeconds: Int = 0) -> Completable { + BleLogger.trace("waitDeviceSessionWithPftpToOpen(): timeoutSeconds \(timeoutSeconds), waitForDeviceDownSeconds \(waitForDeviceDownSeconds)") let pollIntervalSeconds = 5 return Completable.create { emitter in @@ -3123,15 +3122,20 @@ extension PolarBleApiImpl: PolarBleApi { } .timeout(RxTimeInterval.seconds(timeoutSeconds), scheduler: MainScheduler.instance) .subscribe(onNext: { _ in - if self.deviceSessionState == BleDeviceSession.DeviceSessionState.sessionOpen { - BleLogger.trace("Session opened, deviceId: \(deviceId)") + let isSessionOpen = self.deviceSessionState == BleDeviceSession.DeviceSessionState.sessionOpen + let isPftpClientReady = self.isPftpClientReady(deviceId) + + BleLogger.trace("waitDeviceSessionWithPftpToOpen(): isSessionOpen \(isSessionOpen), isFTPReady \(isPftpClientReady)") + + if isSessionOpen && isPftpClientReady { + BleLogger.trace("waitDeviceSessionWithPftpToOpen(): completed") disposable?.dispose() emitter(.completed) } else { - BleLogger.trace("Waiting for device session to open, deviceId: \(deviceId), current state: \(String(describing: self.deviceSessionState))") + BleLogger.trace("waitDeviceSessionWithPftpToOpen(): current state \(String(describing: self.deviceSessionState))") } }, onError: { error in - BleLogger.trace("Timeout reached while waiting for device session to open, deviceId: \(deviceId)") + BleLogger.error("waitDeviceSessionWithPftpToOpen(): error \(error)") emitter(.error(error)) }) return Disposables.create { @@ -3139,6 +3143,18 @@ extension PolarBleApiImpl: PolarBleApi { } } } + + private func isPftpClientReady(_ identifier: String) -> Bool { + do { + let session = try self.sessionFtpClientReady(identifier) + guard let client = session.fetchGattClient(BlePsFtpClient.PSFTP_SERVICE) as? BlePsFtpClient else { + return false + } + return true + } catch _ { + return false + } + } private func processAccData(