From 5ddc048ea13f693ac206e29d3926cb4e1a0947c7 Mon Sep 17 00:00:00 2001 From: Maciej Korzeniewski Date: Fri, 6 Dec 2024 11:02:03 +0200 Subject: [PATCH] Rename waitDeviceSessionToOpen to waitDeviceSessionWithFtpToOpen and update it so that it waits for FTP client to be ready #496 --- .../java/com/polar/sdk/impl/BDBleApiImpl.kt | 39 ++++++++++++++----- .../sdk/impl/PolarBleApiImpl.swift | 36 ++++++++++++----- 2 files changed, 56 insertions(+), 19 deletions(-) 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..4a3439b6 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) + waitDeviceSessionWithFtpToOpen(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, "waitDeviceSessionWithFtpToOpen(): 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 waitDeviceSessionWithFtpToOpen(deviceId: String, timeoutSeconds: Int, waitForDeviceDownSeconds: Int = 0) -> Completable { + BleLogger.trace("waitDeviceSessionWithFtpToOpen(): 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 isFtpClientReady = self.isFtpClientReady(deviceId) + + BleLogger.trace("waitDeviceSessionWithFtpToOpen(): isSessionOpen \(isSessionOpen), isFTPReady \(isFtpClientReady)") + + if isSessionOpen && isFtpClientReady { + BleLogger.trace("waitDeviceSessionWithFtpToOpen(): completed") disposable?.dispose() emitter(.completed) } else { - BleLogger.trace("Waiting for device session to open, deviceId: \(deviceId), current state: \(String(describing: self.deviceSessionState))") + BleLogger.trace("waitDeviceSessionWithFtpToOpen(): current state \(String(describing: self.deviceSessionState))") } }, onError: { error in - BleLogger.trace("Timeout reached while waiting for device session to open, deviceId: \(deviceId)") + BleLogger.error("waitDeviceSessionWithFtpToOpen(): error \(error)") emitter(.error(error)) }) return Disposables.create { @@ -3139,6 +3143,18 @@ extension PolarBleApiImpl: PolarBleApi { } } } + + private func isFtpClientReady(_ 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(