The refresh rate for reading the heart rate, water depth, etc. from the Apple Watch seems to be every 5 seconds. Is there any way to configure this to 1 second? I'm trying to read the water depth, temperature, and heart rate on a 1-second interval. I used a workout session class I found online:
import Foundation
import HealthKit
class WorkoutSession: NSObject, HKLiveWorkoutBuilderDelegate, HKWorkoutSessionDelegate, ObservableObject{
@Published var energyBurnedStatistics: HKStatistics?
@Published var heartRateStatistics: HKStatistics?
@Published var oxygenSaturationStatistics: HKStatistics?
@Published var elapsedTimeStatistics: HKStatistics?
@Published var underwaterDepthStatistics: HKStatistics?
@Published var waterTempStatistics: HKStatistics?
@Published var energyBurned: Double?
@Published var bpm: Double?
@Published var oxygenSaturation: Double?
@Published var status = HKWorkoutSessionState.notStarted
@Published var elapsedTime: TimeInterval?
@Published var workoutData: HKWorkout?
@Published var underwaterDepth: Double?
@Published var waterTemp: Double?
@Published var session: HKWorkoutSession?
@Published var builder: HKLiveWorkoutBuilder?
var healthStore = HKHealthStore()
func workoutBuilder(_ workoutBuilder: HKLiveWorkoutBuilder, didCollectDataOf collectedTypes: Set<HKSampleType>){
self.heartRateStatistics = workoutBuilder.statistics(for: .init(.heartRate))
self.energyBurned = workoutBuilder.statistics(for: .init(.activeEnergyBurned))?.sumQuantity()?.doubleValue(for: .kilocalorie())
self.energyBurnedStatistics = workoutBuilder.statistics(for: .init(.activeEnergyBurned))
self.underwaterDepthStatistics = workoutBuilder.statistics(for: .init(.underwaterDepth))
self.underwaterDepth = workoutBuilder.statistics(for: .init(.underwaterDepth))?.sumQuantity()?.doubleValue(for: .meter())
self.waterTempStatistics = workoutBuilder.statistics(for: .init(.waterTemperature))
self.waterTemp = workoutBuilder.statistics(for: .init(.waterTemperature))?.sumQuantity()?.doubleValue(for: .degreeFahrenheit())
self.elapsedTime = workoutBuilder.elapsedTime
self.bpm = calculateBPM()
}
func workoutBuilderDidCollectEvent(_ workoutBuilder: HKLiveWorkoutBuilder){
}
func workoutSession(_ workoutSession: HKWorkoutSession, didChangeTo toState: HKWorkoutSessionState, from fromState: HKWorkoutSessionState, date: Date){
}
func workoutSession(_ workoutSession: HKWorkoutSession, didFailWithError error: Error){
}
func setUpSession(){
let typesToShare: Set = [HKQuantityType.workoutType()]
let typesToRead: Set = [
HKQuantityType.quantityType(forIdentifier: .heartRate)!,
HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)!
]
healthStore.requestAuthorization(toShare: typesToShare, read: typesToRead){success, error in
}
}
func startWorkoutSession(){
let configuration = HKWorkoutConfiguration()
configuration.activityType = .other
configuration.locationType = .outdoor
do {
session = try HKWorkoutSession(healthStore: healthStore, configuration: configuration)
builder = session!.associatedWorkoutBuilder()
builder!.dataSource = HKLiveWorkoutDataSource(healthStore: healthStore, workoutConfiguration: configuration)
session!.delegate = self
builder!.delegate = self
session!.startActivity(with: Date())
builder!.beginCollection(withStart: Date()){ success, error in
if(!success){
print("Unable to start collection")
}
self.status = .running
}
} catch {
print("Unable to start workout session, the error: \(error)")
}
}
func endWorkoutSession(){
guard let session = session else {
print("Session is nil. Unable to end workout.")
return
}
guard let builder = builder else {
print("Builder is nil. Unable to end workout.")
return
}
session.end()
builder.endCollection(withEnd: Date()) { success, error in
if (!success){
print("Unable to end collection.")
return
}
builder.finishWorkout {workout, error in
if workout == nil{
print("Unable to read workout")
return
}
self.status = .ended
self.workoutData = workout
}
}
}
func resumeWorkoutSession(){
guard let session = session else {
print("Session is nil. Unable to end workout.")
return
}
session.resume()
self.status = .running
}
func pauseWorkoutSession(){
guard let session = session else {
print("Session is nil. Unable to end workout.")
return
}
session.pause()
self.status = .paused
}
}
extension WorkoutSession {
private func calculateBPM() -> Double?{
let countUnit: HKUnit = .count()
let minuteUnit: HKUnit = .minute()
let beatsPerMinute: HKUnit = countUnit.unitDivided(by: minuteUnit)
return self.heartRateStatistics?.mostRecentQuantity()?.doubleValue(for: beatsPerMinute)
}
}
Post
Replies
Boosts
Views
Activity
How can the water depth measurement be accessed from the new Apple Watch Ultra?