HKQuantityTypeIdentifier watchOS calculate Heart rate while watch is not wrist

I am developing an app to calculate the Heart rate using SwiftUI. Code is measuring hear rate accurately. But when I took my watch off form my wrist it still showing the heart rate. I have check the apple heart rate app which stop showing the HR as I took of my watch form wrist.

Code Block swift
let healthKitTypes: Set = [ HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!]
healthStore.requestAuthorization(toShare: healthKitTypes, read: healthKitTypes) { _, _ in }
let devicePredicate = HKQuery.predicateForObjects(from: [HKDevice.local()])
let updateHandler: (HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, Error?) -> Void = { query, samples, deletedObjects, queryAnchor, error in
guard let samples = samples as? [HKQuantitySample] else { return } self.process(samples, type: quantityTypeIdentifier)
}
let query = HKAnchoredObjectQuery(type: HKObjectType.quantityType(forIdentifier: quantityTypeIdentifier)!, predicate: devicePredicate, anchor: nil, limit: HKObjectQueryNoLimit, resultsHandler: updateHandler) query.updateHandler = updateHandler
healthStore.execute(query)


How Can I achieve this in my application.

Replies

May have a look here for some preliminary answer:
https://stackoverflow.com/questions/55077716/ios-watchos5-how-to-detect-programmatically-if-apple-watch-was-on-the-wrist-w

May be you could also look at heart rate history and if nothing changed in the last 3 or 4 frames, that could mean the watch is no more on wrist, and thus ignore the measures ?