iOS WatchOS5 - how to detect programmatically if Apple Watch was on the wrist (worn) at specific time interval?

与世无争的帅哥 提交于 2020-01-24 19:25:47

问题


I'm interested if there is some HealthKit or other data source I can query to know if the Apple Watch was worn/in contact with the wrist at a given time interval. Currently I'm relying on HealthKit query for HeartRate and it appears that if I get no heart rate readings within a certain window, then the watch was most likely off the wrist or charging.

Is there a better way to detect if the Apple Watch was worn on the wrist?

The problem with this method is that it is not very descriptive - if the user put on the watch at the last minute and got a measurement, this logic would consider the entire period as having the watch "On". Is there something better?

// obtain heartRateSamples from HealthKit and filter them
let hrFilterStart = startDate.addingTimeInterval(startSecondsOffset)
let hrFilterEnd = hrFilterStart.addingTimeInterval(Double(30 * 60) )

let heartRateDuringTimeSlice =  heartRateSamples.filter{ sample -> Bool in
    let fallsBetween = (hrFilterStart ... hrFilterEnd).contains(sample.startDate)
    return fallsBetween
}

if heartRateDuringTimeSlice.count == 0 {
    //watch is not on the wrist - probably charging, ignore this interval
}

回答1:


HealthKit does not expose any information that you can use to reliably determine whether the Apple Watch was on-wrist. Using the presence of heart rate or other automatically collected samples will work well enough for most users, but keep in mind that there are situations where heart rate samples might not be collected at a consistent frequency even when the watch is on-wrist.



来源:https://stackoverflow.com/questions/55077716/ios-watchos5-how-to-detect-programmatically-if-apple-watch-was-on-the-wrist-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!