And*_*cia 13 xcode objective-c watchkit
我们可以直接从苹果手表获取心率吗?我知道这是一个重复的问题,但没有人在5个月内问过这个问题.我知道你可以从健康应用程序访问它,但我不确定它将是"实时"的.
cas*_*las 36
心率原始数据信息现在可用于Watchkit for watchOS 2.0.
WatchOS 2包括对其他现有框架的许多增强,例如HealthKit,允许访问实时访问心率和健康信息的健康传感器.
您可以在以下会话中查看此信息,总共30分钟演示.如果您不想观看整个会话,那么您可以直接跳转到Healthkit API25-28分钟之间的功能:
WatchKit for WWOS 2015中的watchOS 2.0 Session
这是源代码实现链接
该
HKWorkout班是一个具体子HKSample类.HealthKit使用锻炼来跟踪各种活动.锻炼对象不仅存储关于活动的摘要信息(例如,持续时间,总距离和燃烧的总能量),它还充当其他样本的容器.您可以将任意数量的样本与锻炼相关联.通过这种方式,您可以添加与锻炼相关的详细信息.
在该给定链接中,代码的以下部分定义了heartRate的采样率
NSMutableArray *samples = [NSMutableArray array];
HKQuantity *heartRateForInterval =
[HKQuantity quantityWithUnit:[HKUnit unitFromString:@"count/min"]
doubleValue:95.0];
HKQuantitySample *heartRateForIntervalSample =
[HKQuantitySample quantitySampleWithType:heartRateType
quantity:heartRateForInterval
startDate:intervals[0]
endDate:intervals[1]];
[samples addObject:heartRateForIntervalSample];
Run Code Online (Sandbox Code Playgroud)
他们在那里说:
您需要根据锻炼类型和应用程序的需求微调相关样本的确切长度.使用5分钟的间隔可以最大限度地减少存储锻炼所需的记忆量,同时仍能提供长时间锻炼过程中强度变化的一般感觉.使用5秒间隔提供了更加详细的锻炼视图,但需要更多的内存和处理.
sho*_*oan 11
在探索HealthKit和WatchKit Extension之后,我的发现如下:
要在查询后获取HealthKit的心率数据,需要定期触发.
func getSamples()
{
let heartrate =HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)
let sort = [
NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
]
let heartRateUnit = HKUnit(fromString: "count/min")
let sampleQuery = HKSampleQuery(sampleType: heartrate!, predicate: nil, limit: 1, sortDescriptors: sort, resultsHandler: { [unowned self] (query, results, error) in
if let results = results as? [HKQuantitySample]
{
let sample = results[0] as HKQuantitySample
let value = sample.quantity.doubleValueForUnit(self.heartRateUnit)
print (value)
let rate = results[0]
print(results[0])
print(query)
self.updateHeartRate(results)
}
})
healthStore?.executeQuery(sampleQuery)
}
func updateHeartRate(samples: [HKSample]?)
{
guard let heartRateSamples = samples as? [HKQuantitySample] else {return}
dispatch_async(dispatch_get_main_queue()) {
guard let sample = heartRateSamples.first else{return}
let value = sample.quantity.doubleValueForUnit(self.heartRateUnit)
self.heartRateLabel.text = String(UInt16(value))
let date = sample.startDate
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
self.timeStampLabel.text = dateFormatter.stringFromDate(date)
}
}
Run Code Online (Sandbox Code Playgroud)如果有人获得更多信息,请更新我.
快乐的编码.
Ste*_*son 10
没有直接的方法可以访问Apple Watch上的任何传感器.您将不得不依赖HealthKit的访问权限.
一位苹果布道者说这个
目前无法创建心脏监护应用程序.数据无法保证实时发送到iPhone,因此您无法及时确定发生了什么.
请参阅https://devforums.apple.com/message/1098855#1098855
您可以通过开始锻炼并从healthkit查询心率数据来获取心率数据.
要求预先阅读锻炼数据.
HKHealthStore *healthStore = [[HKHealthStore alloc] init];
HKQuantityType *type = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
HKQuantityType *type2 = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning];
HKQuantityType *type3 = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned];
[healthStore requestAuthorizationToShareTypes:nil readTypes:[NSSet setWithObjects:type, type2, type3, nil] completion:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@"health data request success");
}else{
NSLog(@"error %@", error);
}
}];
Run Code Online (Sandbox Code Playgroud)在iPhone上的AppDelegate中,请回复此请求
-(void)applicationShouldRequestHealthAuthorization:(UIApplication *)application{
[healthStore handleAuthorizationForExtensionWithCompletion:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@"phone recieved health kit request");
}
}];
}
Run Code Online (Sandbox Code Playgroud)然后执行Healthkit Delegate:
-(void)workoutSession:(HKWorkoutSession *)workoutSession didFailWithError:(NSError *)error{
NSLog(@"session error %@", error);
}
-(void)workoutSession:(HKWorkoutSession *)workoutSession didChangeToState:(HKWorkoutSessionState)toState fromState:(HKWorkoutSessionState)fromState date:(NSDate *)date{
dispatch_async(dispatch_get_main_queue(), ^{
switch (toState) {
case HKWorkoutSessionStateRunning:
//When workout state is running, we will excute updateHeartbeat
[self updateHeartbeat:date];
NSLog(@"started workout");
break;
default:
break;
}
});
}
Run Code Online (Sandbox Code Playgroud)现在是时候写了 **[self updateHeartbeat:date]**
-(void)updateHeartbeat:(NSDate *)startDate{
//first, create a predicate and set the endDate and option to nil/none
NSPredicate *Predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:nil options:HKQueryOptionNone];
//Then we create a sample type which is HKQuantityTypeIdentifierHeartRate
HKSampleType *object = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
//ok, now, create a HKAnchoredObjectQuery with all the mess that we just created.
heartQuery = [[HKAnchoredObjectQuery alloc] initWithType:object predicate:Predicate anchor:0 limit:0 resultsHandler:^(HKAnchoredObjectQuery *query, NSArray<HKSample *> *sampleObjects, NSArray<HKDeletedObject *> *deletedObjects, HKQueryAnchor *newAnchor, NSError *error) {
if (!error && sampleObjects.count > 0) {
HKQuantitySample *sample = (HKQuantitySample *)[sampleObjects objectAtIndex:0];
HKQuantity *quantity = sample.quantity;
NSLog(@"%f", [quantity doubleValueForUnit:[HKUnit unitFromString:@"count/min"]]);
}else{
NSLog(@"query %@", error);
}
}];
//wait, it's not over yet, this is the update handler
[heartQuery setUpdateHandler:^(HKAnchoredObjectQuery *query, NSArray<HKSample *> *SampleArray, NSArray<HKDeletedObject *> *deletedObjects, HKQueryAnchor *Anchor, NSError *error) {
if (!error && SampleArray.count > 0) {
HKQuantitySample *sample = (HKQuantitySample *)[SampleArray objectAtIndex:0];
HKQuantity *quantity = sample.quantity;
NSLog(@"%f", [quantity doubleValueForUnit:[HKUnit unitFromString:@"count/min"]]);
}else{
NSLog(@"query %@", error);
}
}];
//now excute query and wait for the result showing up in the log. Yeah!
[healthStore executeQuery:heartQuery];
}
Run Code Online (Sandbox Code Playgroud)您还可以获得Healthkit的功能.如果您有任何疑问,请在下面发表评论.
| 归档时间: |
|
| 查看次数: |
13633 次 |
| 最近记录: |