目录

管理运动健康采样数据

应用集成Health Service Kit后,通过调用HMSHealthKit中的接口来操作用户的运动数据和健康数据,如下:

插入用户的运动健康数据

插入的运动健康数据的采样点数据的开始时间和结束时间均需大于2014年1月1号对应的UNIX时间戳。

  1. 构造一个数据采集器。
  2. 构造一个步数增量采样点,设置采样点的开始时间、结束时间、增量步数、元数据等。
  3. 根据数据采集器和采样点,创建一个采样集,也可以重复执行步骤2,添加多个样本点数据到采样集。
  4. 调用insert接口, 将采样集插入到Health Service Kit中,需要设置成功或失败的Listener,用来返回数据插入成功或失败结果。

    插入用户的运动健康数据到Health Service Kit示例代码如下:
    import {HMSHealthKit, HMSHealthKitDataType, HMSHealthKitDeviceType, DeviceInfo, DataCollector, Value, SamplePoint, SampleSet} from '@hw-hmscore/hms-js-health'
    // 步骤1:构造一个数据采集器
    // 根据设备生产商、设备模型编号、设备uuid、设备类型构造设备信息
    let deviceInfo = new DeviceInfo("manufacturer", "modelName", "uuid", HMSHealthKitDeviceType.TYPE_PHONE);
    // 根据设备信息、应用包名、数据类型构造数据采集器
    let dataCollector = new DataCollector(deviceInfo, "com.health.demo", HMSHealthKitDataType.DT_CONTINUOUS_STEPS_DELTA);
    // 步骤2:构造一个步数增量采样点,设置采样点的开始时间、结束时间、增量步数、元数据等。
    // 根据原子采样明细数据类型,构造对应数据类型的字段值
    let value = new Value("steps_delta", "200");
    const values = []
    values.push(value);
    // 针对步数数据类型,如果写入的步数数据类型需要在运动健康App上展示,需要设置metadata,如果不需要在运动健康App展示metadata可以写成空字符串或者不设置。
    let metadata = {
        "motion_type": "RUN"
    }
    // 根据字段值数组、开始时间、结束时间和元数据创建samplePoint
    let samplePoint = new SamplePoint(values, "1624774020000", "1624774320000", JSON.stringify(metadata));
    const samplePoints = []
    samplePoints.push(samplePoint);
    // 步骤3:根据数据采集器和采样点,创建一个采样集
    let sampleSet = new SampleSet(dataCollector, samplePoints);
    // 步骤4:调用insert接口,根据异步回调处理插入数据成功或者失败
    HMSHealthKit.insert(sampleSet).then((result) => {
        // 调用成功
        console.info("insert success: " + JSON.stringify(result))
    }).catch((error) => {
        // 调用失败
        console.error("insert Error: " + JSON.stringify(error));
    })

删除用户的运动健康数据

  1. 构造数据类型列表。
  2. 构造数据采集器。
  3. 构造删除请求体。
  4. 调用delete接口, 从Health Service Kit中删除用户的运动健康数据,需要设置成功或失败的Listener,用来返回数据删除成功或失败结果。

    删除Health Service Kit中用户的运动健康数据示例代码如下:
    import {HMSHealthKit, HMSHealthKitDataType, HMSHealthKitDeviceType, DeviceInfo, DataCollector, DeleteOptions} from '@hw-hmscore/hms-js-health'
    // 步骤1:构造数据类型列表
    const dataTypes = []
    // 构造数据类型列表
    dataTypes.push(HMSHealthKitDataType.DT_CONTINUOUS_STEPS_DELTA);
    
    // 步骤2:构造数据采集器
    // 根据设备生产商、设备模型编号、设备uuid、设备类型构造设备信息
    let deviceInfo = new DeviceInfo("manufacturer", "modelName", "uuid", HMSHealthKitDeviceType.TYPE_PHONE);
    // 根据设备信息、应用包名、数据类型构造数据采集器
    let dataCollector = new DataCollector(deviceInfo, "com.health.demo", HMSHealthKitDataType.DT_CONTINUOUS_STEPS_DELTA);
    const dataCollectors = [];
    // 设置将要删除数据的数据源
    dataCollectors.push(dataCollector);
    // 步骤3:构造删除请求体
    // 设置将要删除的数据类型列表、将要删除的数据源列表、删除起止时间、是否删除所有数据、是否删除所有运动记录数据
    let deleteOptions = new DeleteOptions(dataTypes, dataCollectors, "1624774020000", "1624774320000", "false", "false");
    // 步骤4:调用delete接口,根据异步回调处理删除数据成功或者失败
    // 调用delete接口
    HMSHealthKit.delete(deleteOptions).then((result) => {
        // 调用成功
        console.info("delete success: " + JSON.stringify(result))
    }).catch((error) => {
        // 调用失败
        console.error("delete error: " + JSON.stringify(error));
    })

修改用户的运动健康数据

  1. 构造一个数据采集器。
  2. 构造一个步数增量采样点,设置采样点的开始时间、结束时间、增量步数。
  3. 根据数据采集器和采样点,创建一个采样集,也可以重复执行步骤2,添加多个样本点数据到采样集。
  4. 调用update接口, 修改Health Service Kit中用户的运动健康数据,若指定修改的数据不存在,则直接插入该数据。调用接口需要设置成功或失败的Listener,用来返回数据修改成功或失败结果。

    修改Health Service Kit中用户的运动健康数据示例代码如下:
    import {HMSHealthKit, HMSHealthKitDataType, HMSHealthKitDeviceType, DeviceInfo, DataCollector, Value, SamplePoint, SampleSet, UpdateOptions} from '@hw-hmscore/hms-js-health'
    // 步骤1:构造一个数据采集器
    // 根据设备生产商、设备模型编号、设备uuid、设备类型构造设备信息
    let deviceInfo = new DeviceInfo("manufacturer", "modelName", "uuid", HMSHealthKitDeviceType.TYPE_PHONE);
    // 根据设备信息、应用包名、数据类型构造数据采集器
    let dataCollector = new DataCollector(deviceInfo, "com.health.demo", HMSHealthKitDataType.DT_CONTINUOUS_STEPS_DELTA);
    // 步骤2:构造一个步数增量采样点,设置采样点的开始时间、结束时间、增量步数
    // 根据原子采样明细数据类型,构造对应数据类型的字段值
    let value = new Value("steps_delta", "100");
    const values = []
    values.push(value);
    // 根据字段值数组、开始时间、结束时间和元数据创建samplePoint
    let samplePoint = new SamplePoint(values, "1624774020000", "1624774320000", "here is customized text");
    // 步骤3:根据数据采集器和采样点,创建一个采样集
    // 根据数据源和samplePoints创建需要插入的数据采样集
    const samplePoints = []
    samplePoints.push(samplePoint);
    let sampleSet = new SampleSet(dataCollector, samplePoints);
    // 基于将要修改的数据采样集、开始时间、结束时间创建updateOption。
    let updateOption = new UpdateOptions(sampleSet, "1624774020000", "1624774320000")
    // 步骤4-5:调用update接口,根据异步回调处理更新数据成功或者失败
    // 调用update接口
    HMSHealthKit.update(updateOption).then((result) => {
        // 调用成功
        console.info("update success: " + JSON.stringify(result));
    }).catch((error) => {
        // 调用失败
        console.error("update error: " + JSON.stringify(error));
    })

查询用户的运动健康数据

  1. 调用查询运动健康数据接口,接口参数中的开始时间和结束时间均需大于2014年1月1号对应的UNIX时间戳。
  2. 调用查询运动健康数据接口,接口参数中的开始时间和结束时间之间的时间跨度不允许超过10天。
  1. 构造数据类型列表。
  2. 构造查询请求体。
  3. 调用read接口, 从Health Service Kit中查询用户的运动健康数据,需要设置成功或失败的Listener,用来返回数据查询成功或失败结果。

    查询用户的运动健康数据示例代码如下:
    import {HMSHealthKit, HMSHealthKitDataType, ReadOptions} from '@hw-hmscore/hms-js-health'
    // 步骤1:构造数据类型列表
    const dataTypes = []
    // 构造将要查询的数据类型列表
    dataTypes.push(HMSHealthKitDataType.DT_CONTINUOUS_STEPS_DELTA);
    // 步骤2:构造查询请求体
    // 设置起止时间、将要读取的数据类型列表、将要读取的数据源、读取分页大小、是否设置读取前进行一次Health Service Kit数据同步。
    let readOption = new ReadOptions("1624774020000", "1624774320000", dataTypes, null, null, "false")
    
    // 步骤3:调用read接口,根据异步回调处理查看数据成功或者失败
    HMSHealthKit.read(readOption).then((result) => {
        // 调用成功
        console.info("read success: " + JSON.stringify(result));
    }).catch((error) => {
        // 调用失败
        console.error("read error: " + JSON.stringify(error));
    })

查询用户的运动健康数据当日统计值

调用readTodaySummation接口, 从Health Service Kit中查询用户的运动健康数据当日统计值,需要设置成功或失败的Listener,用来返回数据查询成功或失败结果。

查询用户的运动健康数据当日统计值示例代码如下:
import {HMSHealthKit, HMSHealthKitDataType} from '@hw-hmscore/hms-js-health'
// 调用readTodaySummation接口,查询今日步数数据,根据异步回调处理查看数据成功或者失败
HMSHealthKit.readTodaySummation(HMSHealthKitDataType.DT_CONTINUOUS_STEPS_DELTA).then((result) => {
    // 调用成功
    console.info("readTodaySummation success: " + JSON.stringify(result))
}).catch((error) => {
    // 调用失败
    console.error("readTodaySummation error: " + JSON.stringify(error));
})

查询用户的运动健康数据多日统计值

调用readDailySummation接口,从Health Service Kit中查询用户的运动健康数据多日统计值,需要设置成功或失败的Listener,用来返回数据查询成功或失败结果。

查询用户的运动健康数据多日统计值示例代码如下:
import {HMSHealthKit, HMSHealthKitDataType} from '@hw-hmscore/hms-js-health'
// 调用readDailySummation接口,读取2021年11月1号到7号的每日步数数据,根据异步回调处理查看数据成功或者失败
HMSHealthKit.readDailySummation(HMSHealthKitDataType.DT_CONTINUOUS_STEPS_DELTA, "20211101" , "20211107").then((result) => {
    // 调用成功
    console.info("readDailySummation success: " + JSON.stringify(result))
}).catch((error) => {
    // 调用失败
    console.error("readDailySummation error: " + JSON.stringify(error));
})

查询用户的运动健康数据最新数据点

  1. 构造数据类型列表,支持的数据类型参见readLatest的入参说明。
  2. 调用readLatest接口,从Health Service Kit中查询用户的运动健康数据最新数据点,需要设置成功或失败的Listener,用来返回数据查询成功或失败结果。

    查询用户的运动健康数据最新数据点示例代码如下:
    import {HMSHealthKit, HMSHealthKitDataType} from '@hw-hmscore/hms-js-health'
    // 步骤1:构造数据类型列表
    const dataTypes = []
    // 添加身高数据类型
    dataTypes.push(HMSHealthKitDataType.DT_INSTANTANEOUS_HEIGHT)
    // 添加体重数据类型
    dataTypes.push(HMSHealthKitDataType.DT_INSTANTANEOUS_BODY_WEIGHT)
    // 添加心率数据类型
    dataTypes.push(HMSHealthKitDataType.DT_INSTANTANEOUS_HEART_RATE)
    // 步骤2:调用readLatest接口,根据异步回调处理查看数据成功或者失败
    // 调用readLatest接口,读取身高、体重、心率的最新数据采样点。
    HMSHealthKit.readLatest(dataTypes).then((result) => {
        // 调用成功
        console.info("read success: " + JSON.stringify(result))
    }).catch((error) => {
        // 调用失败
        console.error("read Error: " + JSON.stringify(error));
    })

清空端云用户数据

调用clearAll接口, 从Health Service Kit中清空用户写入Health Service Kit数据,需要设置成功或失败的Listener,用来返回数据清除成功或失败结果。

清除用户的运动健康数据示例代码如下:

import {HMSHealthKit} from '@hw-hmscore/hms-js-health'
// 调用clearAll接口,根据异步回调处理清除数据成功或者失败
// 调用clearAll接口
HMSHealthKit.clearAll().then((result) => {
    // 调用成功
    console.info("clearAll success: " + JSON.stringify(result))
}).catch((error) => {
    // 调用失败
    console.error("clearAll error: " + JSON.stringify(error));
})