NikitaAbrosimov
NikitaAbrosimov

Reputation: 127

Get heartRateVariabilitySDNN during LiveWorkoutSession at WatchOS

I've a made a simple watchOS app to run a running session and monitor heart rate variability(sdnn). Running on a simulator I succeed to get other p-s like distance, heart rate or calories, but not sdnn.

Here is my way to setup a WorkoutManager:

    func workoutBuilderDidCollectEvent(_ workoutBuilder: HKLiveWorkoutBuilder) {
    //
    }
    
    func workoutBuilder(_ workoutBuilder: HKLiveWorkoutBuilder, didCollectDataOf collectedTypes: Set<HKSampleType>) {
        for type in collectedTypes{
            print(type)
            guard let quantityType = type as? HKQuantityType else {return}
            let statistics = workoutBuilder.statistics(for: quantityType)
          
            updateForStatistics(statistics)
        }
    }
} 

And this piece is fetching data in realtime:

func updateForStatistics(_ statistics: HKStatistics?) {
        guard let statistics = statistics else { return }

        DispatchQueue.main.async {
            switch statistics.quantityType {
            case HKQuantityType.quantityType(forIdentifier: .heartRate):
                let heartRateUnit = HKUnit.count().unitDivided(by: HKUnit.minute())
                self.heartRate = statistics.mostRecentQuantity()?.doubleValue(for: heartRateUnit) ?? 0
                self.averageHeartRate = statistics.averageQuantity()?.doubleValue(for: heartRateUnit) ?? 0
            case HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned):
                let energyUnit = HKUnit.kilocalorie()
                self.activeEnergy = statistics.sumQuantity()?.doubleValue(for: energyUnit) ?? 0
            case HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning), HKQuantityType.quantityType(forIdentifier: .distanceCycling):
                let meterUnit = HKUnit.meter()
                self.distance = statistics.sumQuantity()?.doubleValue(for: meterUnit) ?? 0
            case HKQuantityType.quantityType(forIdentifier: .heartRateVariabilitySDNN):
                let sdnnUnit = HKUnit.count()
                self.sdnn = statistics.mostRecentQuantity()?.doubleValue(for: sdnnUnit) ?? 0
            default:
                return
            }
        }
    }

As mentioned, all other p-s are emulating by WatchOS excluding sdnn - here I always getting no data.

Also, I know how to write my own sdnn values through the HKQuantitySample, but need the specific ones tracked by OS during the session. Or some workaround to force OS to save this for me.

Any ideas, please?

Upvotes: 0

Views: 279

Answers (2)

strama
strama

Reputation: 1

Unfortunately, You cannot access heartRateVariabilitySDNN parameter because it is not measured during workout. However, you can manually trigger measurement of HRV by using Breathe exercise in Mindfulness app

Upvotes: 0

Felipe Veiga
Felipe Veiga

Reputation: 1

The ssdUnit should be HKUnit(from: "ms")

Upvotes: 0

Related Questions