2016-02-29 94 views
0

我試圖從傳感器實時獲取心率值。下面的代碼激活鍛鍊會話(我可以在手錶上看到它),HR傳感器亮起,所以它顯然工作,但我不能訪問值updateHeartRate()方法總是返回「沒有HR檢測」。有誰知道如何解決這個問題?訪問Apple Watch中的心率傳感器 - HealthKit不返回值

let healthStore = HKHealthStore() 
var workoutActive = false 
var workoutSession : HKWorkoutSession? 
let heartRateUnit = HKUnit(fromString: "count/min") 
var anchor = HKQueryAnchor(fromValue: 
Int(HKAnchoredObjectQueryNoAnchor)) 

func workoutSession(workoutSession: HKWorkoutSession, didChangeToState toState: HKWorkoutSessionState, fromState: HKWorkoutSessionState, date: NSDate) { 
    switch toState { 
    case .Running: 
     workoutDidStart(date) 
    case .Ended: 
     workoutDidEnd(date) 
    default: 
     print("Unexpected state \(toState)") 
    } 
} 

func workoutSession(workoutSession: HKWorkoutSession, didFailWithError error: NSError) { 
    // Do nothing for now 
    NSLog("Workout error: \(error.userInfo)") 
} 

func workoutDidStart(date : NSDate) { 
    print("Workout did start") 
    if let query = createHeartRateStreamingQuery(date) { 
     print("Using query") 
     healthStore.executeQuery(query) 

    } else { 
     print("cannot start") 
    } 
} 

func workoutDidEnd(date : NSDate) { 
    if let query = createHeartRateStreamingQuery(date) { 
     healthStore.stopQuery(query) 
    } else { 
    } 
} 

func startBtnTapped() { 
    if (self.workoutActive) { 
     //finish the current workout 
     self.workoutActive = false 
     print("Finishing Workout") 

     if let workout = self.workoutSession { 
      healthStore.endWorkoutSession(workout) 
     } 
    } else { 
     //start a new workout 
     self.workoutActive = true 
     startWorkout() 

     print("Starting Workout") 
    } 

} 

func startWorkout() { 
    self.workoutSession = HKWorkoutSession(activityType: HKWorkoutActivityType.CrossTraining, locationType: HKWorkoutSessionLocationType.Indoor) 
    self.workoutSession?.delegate = self 
    healthStore.startWorkoutSession(self.workoutSession!) 
} 

func createHeartRateStreamingQuery(workoutStartDate: NSDate) -> HKQuery? { 

    let predicate = HKQuery.predicateForSamplesWithStartDate(workoutStartDate, endDate: nil, options: HKQueryOptions.None) 
    print("Entered queury") 
    guard let quantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate) else { return nil } 

    let heartRateQuery = HKAnchoredObjectQuery(type: quantityType, predicate: nil, anchor: anchor, limit: Int(HKObjectQueryNoLimit)) { (query, sampleObjects, deletedObjects, newAnchor, error) -> Void in 
     self.updateHeartRate(sampleObjects) 
     print("Query is configured") 
    } 

    heartRateQuery.updateHandler = {(query, samples, deleteObjects, newAnchor, error) -> Void in 
     self.updateHeartRate(samples) 
     print("Updating sample") 
     print(samples) 
    } 
    return heartRateQuery 
} 


func updateHeartRate(samples: [HKSample]?) { 
    print("Entered updateHR") 
    guard let heartRateSamples = samples as? [HKQuantitySample] else { 

     print("No HR detected") 

     return 
    } 

    dispatch_async(dispatch_get_main_queue()) { 
     print("Enter async") 
     guard let sample = heartRateSamples.first else{return} 
     let value = sample.quantity.doubleValueForUnit(self.heartRateUnit) 
     print(String(UInt16(value))) 


     // retrieve source from sample 
     print(sample.sourceRevision.source.name) 

    } 
} 

回答

0

您是否要求授權從HealthKit讀取心率採樣? 如果不是,您的查詢很可能會收到描述問題的錯誤。嘗試在初始結果處理程序和更新處理程序塊中檢查錯誤。

相關問題