Post

Replies

Boosts

Views

Activity

Reply to HKLiveWorkoutBuilder only reporting heart rate - No other measurements
Ziqiao, As per your email for following up on any additional questions, HealthKit only collects .respiratoryRate during sleep. However, there must be a way in which the developer can record this by specifying that the user is asleep. The reason why I ask is because I am writing a health and wellness app for my PhD where I am using music therapy for relaxation and utilizing machine learning to detect whether or not the soundscape being played is having any positive effect on the participant. Respiratory rate would be a very useful biosensing metric for my research. Is there a way in which I can retrieve the .respiratoryRate in code on the Apple Watch as the user is "asleep"? Sincerely, Arron
Sep ’24
Reply to HKLiveWorkoutBuilder only reporting heart rate - No other measurements
@DTS Engineer (Ziqiao), Thank you for your quick response on my question. I've used an HKSampleQuery as you've suggested which retrieves the HRV. However when running the query, no results are returned for respiratory rate or body temperature. The following code shows a function with various calls (please comment/uncomment as needed): func fetchData() { let bodyTemperatureType = HKQuantityType.quantityType(forIdentifier: .respiratoryRate)! let mostRecentPredicate = HKQuery.predicateForSamples(withStart: Date.distantPast, end: Date(), options: .strictEndDate) let sampleQuery = HKSampleQuery(sampleType: bodyTemperatureType, predicate: mostRecentPredicate, limit: 1, sortDescriptors: [NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)]) { (query, samples, error) in guard let samples = samples as? [HKQuantitySample], let sample = samples.first else { print("No data available or error: \(String(describing: error))") return } // Doesn't work! let respiratoryRate = sample.quantity.doubleValue(for: HKUnit(from: "count/min")) // Doesn't work! //let bodyTemperature = sample.quantity.doubleValue(for: HKUnit.degreeCelsius()) // This works //let hrv = sample.quantity.doubleValue(for: HKUnit.secondUnit(with: .milli)) //print("Body Temperature: \(hrv)°C") //print("HRV: \(hrv)") print("Resp rate: \(respiratoryRate)") } // Execute the query healthStore.execute(sampleQuery) } I see no documentation from Apple why only HRV can be retrieved from an HKSampleQuery but not body temperature or respiratory rate. Any ideas why this is?
Sep ’24