【发布时间】:2020-03-01 15:20:39
【问题描述】:
我有来自使用蓝牙血压、血糖的第三方设备的数据,我想将这些数据发送到苹果健康,即将数据写入苹果健康,任何人都可以提供使用 swift 5 的示例。
【问题讨论】:
我有来自使用蓝牙血压、血糖的第三方设备的数据,我想将这些数据发送到苹果健康,即将数据写入苹果健康,任何人都可以提供使用 swift 5 的示例。
【问题讨论】:
您收集 BLE 数据,提取值,然后在 HealthKitStore 中使用简单的保存功能。 (一定要在授权时先提供写权限!)。
这是一个血糖的示例函数。
func writeBloodGlucose(value: Double, measurementDate: Date) throws {
let healthStore = HKHealthStore()
let bloodSugar = HKQuantitySample(
type: HKObjectType.quantityType(forIdentifier: .bloodGlucose)!,
quantity: HKQuantity(
unit: HKUnit.gramUnit(with: .milli).unitDivided(by: HKUnit.liter()),
doubleValue: value
),
start: measurementDate,
end: measurementDate,
device: HKDevice(
name: "name",
manufacturer: "manufacturer",
model: "model",
hardwareVersion: "hardwareVersion",
firmwareVersion: "firmwareVersion",
softwareVersion: "softwareVersion",
localIdentifier: "localIdentifier",
udiDeviceIdentifier: "udiDeviceIdentifier"
),
metadata: nil)
healthStore.save(bloodSugar) { (success, error) in
print(success)
print(error)
}
}
如果您愿意,我已经开发了一个 CocoaPod,它可以简化 AppleHealth 的写入/读取数据。这是链接:https://cocoapods.org/pods/HealthKitReporter
【讨论】: