如果有一些 HealthKit 或其他数据源我可以查询以了解 Apple Watch 是否在给定的时间间隔内佩戴/与手腕接触,我很感兴趣。目前我依靠 HealthKit 查询 HeartRate,如果我在某个窗口内没有得到心率读数,那么手表很可能不在手腕上或正在充电。
有没有更好的方法来检测 Apple Watch 是否戴在手腕上?
这种方法的问题在于它的描述性不强——如果用户在最后一分钟戴上手表并进行了测量,那么这个逻辑会将整个时间段视为手表“开启”。有更好的吗?
// 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
}