【问题标题】:ResearchKit Walking Task Not Returning Heart Rate DataResearchKit 步行任务未返回心率数据
【发布时间】:2016-04-07 14:47:26
【问题描述】:

我正在做我的第一个 ResearchKit 项目。我正在尝试通过苹果的 HealthKit 获取心率数据。我正在手机上测试该程序,并且我有带有健康数据的 Apple Watch,它应该可用。步行任务成功启动和完成,我能够解析结果文件。但我发现结果文件只包含物理传感器数据(加速度计和陀螺仪),而不包含任何健康数据。

让我有点担心的是,当步行任务开始时,我在控制台输出上看到了这两个警告:

ORKSample[511:80256] [ResearchKit][Warning] __82-[ORKTaskViewController requestHealthStoreAccessWithReadTypes:writeTypes:handler:]_block_invoke Health access: error=(null)
2016-04-07 16:31:28.097 ORKSample[511:80630] [ResearchKit][Warning] __59-[ORKTaskViewController requestPedometerAccessWithHandler:]_block_invoke Pedometer access: error=(null)

看来_block_invole Health access_block_invoke Pedometer access 都不好。

这是我用来授权健康数据的代码:

import ResearchKit
import HealthKit

class HealthDataStep: ORKInstructionStep {
    // MARK: Properties

    let healthDataItemsToRead: Set<HKObjectType> = [
        HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth)!,
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)!,
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)!,
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!,
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!
    ]

    let healthDataItemsToWrite: Set<HKSampleType> = [
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)!,
        HKObjectType.workoutType(),
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!,
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!
    ]

    // MARK: Initialization

    override init(identifier: String) {
        super.init(identifier: identifier)

        title = NSLocalizedString("Health Data", comment: "")
        text = NSLocalizedString("On the next screen, you will be prompted to grant access to read and write some of your general and health information, such as height, weight, and steps taken so you don't have to enter it again.", comment: "")
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: Convenience

    func getHealthAuthorization(completion: (success: Bool, error: NSError?) -> Void) {
        guard HKHealthStore.isHealthDataAvailable() else {
            let error = NSError(domain: "com.example.apple-samplecode.ORKSample", code: 2, userInfo: [NSLocalizedDescriptionKey: "Health data is not available on this device."])

            completion(success: false, error:error)

            return
        }

        // Get authorization to access the data
        HKHealthStore().requestAuthorizationToShareTypes(healthDataItemsToWrite, readTypes: healthDataItemsToRead) { (success, error) -> Void in
            completion(success:success, error:error)
        }
    }
}

我用这段代码来实现步行任务,很简单:

public var TimedWalkTask: ORKOrderedTask {
    return ORKOrderedTask.fitnessCheckTaskWithIdentifier("WalkTask",intendedUseDescription: nil,walkDuration: 15 as NSTimeInterval, restDuration: 15 as NSTimeInterval,options: .None)
}

只是想知道是否有人知道我是否遗漏了什么。该程序似乎运行正确并返回结果,但结果不包含我正在寻找的健康数据。

仅供参考,我附上了我的应用在 iphone 设置中的健康权限的屏幕截图:

【问题讨论】:

  • Apple Watch 不会一直监测心率,您需要将手机与 BTLE 心率监测器配对。

标签: ios swift researchkit


【解决方案1】:

Yuan 是正确的——样本不会从 Apple Watch 实时同步到手机,因此您不会在当前任务中获取它们。您可能仍然可以在研究中执行此操作,但您需要使用历史 HealthKit 查询来获取与先前任务相关的样本,并在以后处理数据时将它们关联起来。

此外,除非在 Apple Watch 上进行了锻炼,否则只会不经常收集心率样本。如果您想要更高频率的心率样本,您将需要一个 Watch 应用程序,作为您研究的一部分,开始锻炼课程,或者您需要让用户在 Watch 上使用锻炼应用程序开始锻炼,持续时间为任务。

【讨论】:

    猜你喜欢
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 2013-04-29
    相关资源
    最近更新 更多