0
workoutSessionでデータを取得できました。watchkit
のアプリ拡張でHKAnchoredObjectQuery
です。そして私はheartRateを取得し、私のiPhoneにデータを表示したい。だから私はHKSampleQuery
を使ってSimulator(時計とiPhone)を手に入れます。しかし、私はiPhoneと時計を使ってテストします。私のiPhoneはheartRateを1回だけ得ることができました。iPhoneがApple Watchから心拍数データを取得できないのはなぜですか?時計拡張機能はできますか?
//コード
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
guard let sampleType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate) else { return }
// 1. Build the Predicate
let past = NSDate.distantPast() as NSDate
let now = NSDate()
let mostRecentPredicate = HKQuery.predicateForSamplesWithStartDate(past, endDate:now, options: .None)
// 2. Build the sort descriptor to return the samples in descending order
let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: false)
// 3. we want to limit the number of samples returned by the query to just 1 (the most recent)
// 4. Build samples query
let sampleQuery = HKSampleQuery(sampleType: sampleType, predicate: mostRecentPredicate, limit: Int(HKObjectQueryNoLimit), sortDescriptors: [sortDescriptor])
{ (sampleQuery, HKSample, error) -> Void in
// Get the samples
guard let heartRateSamples = HKSample as? [HKQuantitySample] else {return}
if(heartRateSamples.count > 0){
let count : Double = Double(heartRateSamples.count)
var sum = 0.0;
for quantitySample in heartRateSamples
{
let value = quantitySample.quantity.doubleValueForUnit(self.heartRateUnit);
sum += value;
}
let avg = sum/count;
self.HeartRateSum.text = String(sum)
self.Count.text = String(count)
self.SilentHeartRate.text = String(avg)
}
}
// 5. Execute the Query
self.healthStore.executeQuery(sampleQuery)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
sleep(15)
while(true){
self.getRecentHeartRate()
}
});
}
func getRecentHeartRate() ->Void{
guard let sampleType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate) else { return }
let past = NSDate.distantPast() as NSDate
let now = NSDate()
let limit = 1
let mostRecentPredicate = HKQuery.predicateForSamplesWithStartDate(past, endDate:now, options: .None)
// 2. Build the sort descriptor to return the samples in descending order
let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: false)
let sampleQuery = HKSampleQuery(sampleType: sampleType, predicate: mostRecentPredicate, limit: limit, sortDescriptors: [sortDescriptor])
{ (sampleQuery, HKSample, error) -> Void in
// Get the samples
guard let heartRateSample = HKSample as? [HKQuantitySample] else {return}
guard let recentHeartRate = heartRateSample.first else{return}
let value = recentHeartRate.quantity.doubleValueForUnit(self.heartRateUnit)
dispatch_async(dispatch_get_main_queue()) {
self.heartRate.text = String(UInt16(value))
}
}
self.healthStore.executeQuery(sampleQuery)
}
おかげで多くのことを得るためにこれを試すことができます。私は最近の心臓部をIphoneに表示するように更新したいと思っています。心拍数の統計データを収集するのは安定しています。しかし、私はなぜプログラムがシミュレータしかし、iPhoneでうまく動作するのか分かりません。 – Maxwell
このコードを試してみましたか? –
もちろん。プログラムに入ると、私は一度hearRateを得ることができました。プログラムの実行中にユーザーのheartRateを更新してデータを表示したい。今度は毎回データを取得した後にsleep(1)というコードをテストします。それは今働く。しかし、それはまだ不安定です。理由は私のコードがあまりにも悪いと思います。メモリリークなどの問題があります。 – Maxwell