logo
down
shadow

How to save an array of HKQuantitySamples (heart rate) to a workout


How to save an array of HKQuantitySamples (heart rate) to a workout

By : Parviz.Mammadov
Date : November 18 2020, 03:01 PM
Any of those help Apple's docs sample code saves an average heart rate HKQuantitySample to a workout via add method, however for a given workout I am trying to save all heart rate values taken during the workout, i.e. [HKQuantitySample] how can I do this? Below is my code adding the first value just to test, but i want to add them all? , You already have heartRateValues as [HKQuantitySample] so just do:
code :
private func addSamples(toWorkout workout: HKWorkout, from startDate: Date, to endDate: Date) {
    // Create energy and distance samples
    let totalEnergyBurnedSample = HKQuantitySample(type: HKQuantityType.activeEnergyBurned(),
                                                   quantity: totalEnergyBurnedQuantity(),
                                                   start: startDate,
                                                   end: endDate)

    let totalDistanceSample = HKQuantitySample(type: HKQuantityType.distanceWalkingRunning(),
                                               quantity: totalDistanceQuantity(),
                                               start: startDate,
                                               end: endDate)

    let samples = [HKQuantitySample]()
    samples.append(totalEnergyBurnedSample)
    samples.append(totalDistanceSample)
    samples.append(contentsOf: heartRateValues)

    // Add samples to workout
    healthStore.add(samples, to: workout) { (success: Bool, error: Error?) in
        guard success else {
            print("Adding workout subsamples failed with error: \(String(describing: error))")
            return
        }


        }
    }


Share : facebook icon twitter icon
Bluetooth low energy specification Heart rate profile vs heart rate service

Bluetooth low energy specification Heart rate profile vs heart rate service


By : sandusky
Date : March 29 2020, 07:55 AM
I wish did fix the issue. The Heart Rate Profile defines an application profile to let a client detect the device as a heart rate sensor. This profile must include at least the GAP service and the Heart Rate Service for compliance
For battery, the monitoring of a battery level is not an application by itself (or it wouldn't be very useful as an application), but it is relevant to specify a Battery Service for monitoring the battery level within an application.
How to get heart rate in watchOS 3+ without using a workout session

How to get heart rate in watchOS 3+ without using a workout session


By : user2785474
Date : March 29 2020, 07:55 AM
Hope that helps Since you are having a companion iPhone app, you may query heart rate updates from there.
You just need to have an iPhone with paired Apple watch (which is obvious) The Default Apple Watch Heart Rate monitor app updates the HealthKit data immediately only when it is in the foreground.
code :
import UIKit
import HealthKit

class ViewController: UIViewController {
@IBOutlet weak var heartRateLabel: UILabel!
@IBOutlet weak var timeStampLabel: UILabel!

var hkStore: HKHealthStore?
let heartRateUnit: HKUnit = HKUnit.count().unitDivided(by: .minute())
var healthStore: HKHealthStore?
override func viewDidLoad() {
    super.viewDidLoad()
    healthStore = HKHealthStore()
    let sampleTypes = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!

    healthStore?.requestAuthorization(toShare: [sampleTypes], read: [sampleTypes], completion: { (success, error) in
        if (error != nil) {
            print(error!.localizedDescription)
        }
    })

    getSamples()
}
func getSamples() {
    let heartrate = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)
    let sort = [
        NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
    ]
    let sampleQuery = HKSampleQuery(sampleType: heartrate!, predicate: nil, limit: 1, sortDescriptors: sort, resultsHandler: { [unowned self] (query, results, error) in
        if let results = results as? [HKQuantitySample]
        {
            let sample = results[0] as HKQuantitySample
            let value = sample.quantity.doubleValue(for: self.heartRateUnit)
            let rate = results[0]
            print(value, rate)
            self.updateHeartRate(samples: results)
        }
    })
    healthStore?.execute(sampleQuery)
}

func updateHeartRate(samples: [HKSample]?) {
    guard let heartRateSamples = samples as? [HKQuantitySample] else {return}
    DispatchQueue.main.async {
        guard let sample = heartRateSamples.first else{return}
        let value = sample.quantity.doubleValue(for: self.heartRateUnit)
        self.heartRateLabel.text = String(UInt16(value))
        let date = sample.startDate
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
        self.timeStampLabel.text = dateFormatter.string(from: date)
     }
 }
}
Apple's heart rate monitoring example and byte order of bluetooth heart rate measurement characteristics

Apple's heart rate monitoring example and byte order of bluetooth heart rate measurement characteristics


By : Thomas
Date : March 29 2020, 07:55 AM
hope this fix your issue On the heart rate measurement characteristics: ,
Am I reading this document correctly?
iOS HealthKit how to save Heart Rate (bpm) values? Swift

iOS HealthKit how to save Heart Rate (bpm) values? Swift


By : Joyee
Date : March 29 2020, 07:55 AM
will help you How to use : HKUnit , Swift : Heart Rate (bpm) save into healthkit store
How to access Android Heart Rate Sensor RAW DATA? (reflected light, not the heart beat)

How to access Android Heart Rate Sensor RAW DATA? (reflected light, not the heart beat)


By : Javajef
Date : March 29 2020, 07:55 AM
hope this fix your issue You can use Google Fit's Sensor API to get the raw heartbeat data, if Google Fit is an option. See Google Fit Guide for details.
shadow
Privacy Policy - Terms - Contact Us © voile276.org