更新记录
1.0.7(2025-06-19)
- 优化
1.0.6(2025-06-19)
- 修复获取体重兼容性问题
1.0.5(2025-06-19)
- 增加静息心率、呼吸率
平台兼容性
uni-app
Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
---|---|---|---|---|---|---|---|---|
√ | √ | - | - | - | - | × | - | × |
微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
---|---|---|---|---|---|---|---|---|---|---|
× | × | × | × | × | × | - | × | × | × | × |
uni-app x
Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
---|---|---|---|---|---|
- | - | × | - | × | × |
增删查手机系统健康数据如步数、跑步距离、身高、血糖、活动能量、心率、血压等等
集成步骤
- 拷贝demo里的nativeResources、Info.plist文件到项目根目录
- 苹果网站创建Identifiers后,点击进入到详情,勾选Capabilities下的HealthKit
- 集成插件,集成插件步骤请参考 https://www.cnblogs.com/wenrisheng/p/18323027
接口
import {
UTSHealth
} from "@/uni_modules/wrs-uts-health"
let health = new UTSHealth()
- 请求权限
let types = [
"stepCount",
"distanceWalkingRunning",
"heartRate",
"bloodPressureSystolic",
"bloodPressureDiastolic",
"bodyFatPercentage",
"bodyMass",
"bodyMassIndex",
"height",
"leanBodyMass",
"activeEnergyBurned",
"basalEnergyBurned",
"distanceCycling",
"sleepAnalysis",
"bodyTemperature",
"oxygenSaturation",
"bloodGlucose",
"walkingStepLength"
]
let params = {}
params.readTypes = types // 读权限
params.writeTypes = types // 写权限
health.requestAuthorization(params, (resp) => {
this.showMsg(JSON.stringify(resp))
let flag = resp.flag
if (!flag) {
console.log(JSON.stringify(resp))
}
})
- 获取健康数据
let sampleQuery = true // true: 查询详细数据 false:查询总和
let params = {}
params.type = "stepCount" // type类型,见下面type说明
params.sampleQuery = sampleQuery
// params.startDay = "2024-10-02 00:00:00" // 可选参数,开始时间
// params.endDay = "2024-10-02 23:59:59" // 可选参数,结束时间
health.executeQuery(params, (resp) => {
if(sampleQuery) {
// {"flag":true,"data":[{"uuid":"A4A071C8-0530-46A5-95D5-15DD03F7F761","endDate":1750070700,"type":"HKQuantityTypeIdentifierStepCount","sourceBundleIdentifier":"com.apple.Health","sourceName":"健康","startDate":1750070700,"unit":"count","quantity":50}]}
let dataArray = resp.data
if(dataArray) { // 数组
}
} else {
// {"sumQuantity":414349.9705362814,"flag":true}
let sumQuantity = resp.sumQuantity
if(sumQuantity) {
this.showMsg("步数:" + sumQuantity)
} else {
this.showMsg(JSON.stringify(resp))
}
}
})
返回数据字段说明
- uuid: 数据的ID
- startDate: 开始时间,时间戳
- endDate: 结束时间,时间戳
- sourceName: 数据来源名称
- sourceBundleIdentifier:来源标识
- quantity: 指标值
- unit:指标单位
- categoryType:指标类型
- 多指标项同时查询
let types = []
types.push({
sampleQuery: true,
type: "stepCount",
// startDay: "2025-06-15 00:00:00",
// endDay: "2025-06-16 23:59:59"
})
types.push({
sampleQuery: true,
type: "heartRate"
})
let params = {}
params.types = types
health.executeBatchQuery(params, (resp)=>{
// {
// "dataList": [{
// "type": "stepCount",
// "data": [{
// "uuid": "A4A071C8-0530-46A5-95D5-15DD03F7F761",
// "endDate": 1750070700,
// "count": 50,
// "type": "HKQuantityTypeIdentifierStepCount",
// "sourceBundleIdentifier": "com.apple.Health",
// "sourceName": "健康",
// "startDate": 1750070700,
// "unit": "count",
// "quantity": 50
// }]
// }, {
// "type": "heartRate",
// "data": [{
// "uuid": "DCCD2AB0-CD40-47E8-AA58-91FA2C53748D",
// "endDate": 1750071600,
// "count": 200,
// "type": "HKQuantityTypeIdentifierHeartRate",
// "sourceBundleIdentifier": "com.apple.Health",
// "sourceName": "健康",
// "startDate": 1750071600,
// "unit": "count/min",
// "quantity": 200
// }]
// }]
// }
this.showMsg(JSON.stringify(resp))
})
- 插入健康数据
let params = {}
params.dataArray = [
{
type: "stepCount", // type见下面说明
quantity: 100, // 步数
startDay:"2025-06-15 00:00:00",
endDay: "2025-06-15 00:00:00"
}
]
health.executeSave(params, (resp)=>{
this.showMsg(JSON.stringify(resp))
let flag = resp.flag
if (!flag) {
console.log("保存失败:" + JSON.stringify(resp))
}
})
- 删除健康数据
let params = {}
params.type = "stepCount" // type见下面说明
params.startDay = "2025-06-15 00:00:00"
params.endDay = "2025-06-16 23:59:59"
health.executeDelete(params, (resp)=>{
this.showMsg(JSON.stringify(resp))
let flag = resp.flag
if (!flag) {
console.log("删除失败:" + JSON.stringify(resp))
}
})
type类型
-
stepCount: 步数
-
distanceWalkingRunning: 步行+跑步距离(单位公里)
-
height: 身高
-
activeEnergyBurned: 活动能量
-
basalEnergyBurned: 静息能量
-
distanceCycling: 骑车距离
-
heartRate: 心率
-
restingHeartRate: 静息心率
-
respiratoryRate: 呼吸频率
-
bloodPressureSystolic: 收缩压
-
bloodPressureDiastolic: 舒张压
-
bodyFatPercentage:体脂率
-
bodyMass:体重
-
leanBodyMass:去脂体重
-
bodyMassIndex:身高体重指数BMI
-
activeEnergyBurned:活动能量
-
basalEnergyBurned: 静息能量
-
distanceCycling:骑车距离
-
sleepAnalysis: 睡眠,结束时间减去开始时间就是睡眠时长 睡眠类型返回的categoryType是指睡眠类型: 0: inBed 卧床时间 1: asleepUnspecified 睡眠 2: awake 清醒时间 3: asleepCore 核心睡眠 4: asleepDeep 深度睡眠 5: asleepREM 快速动眼睡眠
-
bodyTemperature:体温
-
oxygenSaturation:血氧
-
bloodGlucose:血糖
-
walkingStepLength: 步行步长
-
appleExerciseTime:苹果锻炼时间
-
vo2Max:最大摄氧量
如需更多数据请联系作者https://www.cnblogs.com/wenrisheng/p/18323027