【发布时间】:2019-03-09 13:14:53
【问题描述】:
如果有一些 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
}
【问题讨论】: