使用healthKit的dailyActivities,无法获取某个时间段的计步数据

无法获取计步数据,申请计步资质已经通过,也配置了client_id,用户也授权了,但是返回某一个时间段的计步数据一直为空数组,这期间一定是有步数的。



下面是相关代码:

import { healthStore } from '@kit.HealthServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { common } from '@kit.AbilityKit';
import { AppUtil, DateUtil } from '@pura/harmony-utils'

export interface  SJDailyData{
  step:number
  calorie:number
  distance:number
  duration:number
  status:number
}

export class HealthServiceManager {

  static context:common.UIAbilityContext

  static  init(context:common.UIAbilityContext){
    healthStore.init(context)
    HealthServiceManager.context = context
    // HealthServiceManager.cancelAuthAll()
  }

  static async getStepsEasyAuth(start: Date, end: Date): Promise<SJDailyData | undefined> {
    let hasPermission = await HealthServiceManager.hasStepCountPermission()
    let searchAction = async () => {
      let dailyData = await HealthServiceManager.getDailyActivityForPeriod(start, end)
      return dailyData
    }
    if (hasPermission) {
      return await searchAction()
    } else {
      let authOK = await HealthServiceManager.auth(HealthServiceManager.context)
      if(authOK){
        return await searchAction()
      }else{
        // CommonUI.showToast("无法获取步数,请重试")
        return undefined
      }
    }
  }



  /**
   * 获取开始-结束时间的活动,包括步数,卡路里,距离,运动时长等
   *startTime: date.setHours(0, 0, 0, 0),
   * endTime: date.setHours(23, 59, 59, 999),
   * */
  public static  async getDailyActivityForPeriod(startDate: Date, endDate: Date): Promise<SJDailyData|undefined> {
    try {
      let startTime =   DateUtil.getFormatDate("2024-01-01 00:00:00").getTime() // startDate.getTime()
      let endTime = DateUtil.getFormatDate("2024-01-20 12:00:00").getTime()// endDate.getTime()
      const request: healthStore.SamplePointReadRequest = {
        samplePointDataType: healthStore.samplePointHelper.dailyActivities.DATA_TYPE,
        startTime:startTime,
        endTime:endTime,
      };
      let samplePoints = await healthStore.readData<healthStore.samplePointHelper.dailyActivities.Model>(request);
      let totalSteps = 0;
      let totalCalorie = 0;
      let totalDurations = 0;
      let totalStatus = 0;
      let totalDistance = 0;

      samplePoints.forEach((point) => {
        totalSteps += point.fields.step
        totalCalorie += point.fields.calorie
        totalDurations += point.fields.duration ?? 0
        totalStatus += point.fields.status ?? 0
        totalDistance += point.fields.distance
      });

      let res:SJDailyData = {
        step:totalSteps,
        calorie:totalCalorie,
        duration:totalDurations,
        status:totalStatus,
        distance:totalDistance
      }
      return res;
    } catch (err) {
      return ;
    }
  }


  public static async auth(context: common.UIAbilityContext): Promise<boolean> {
    try {
      let authorizationParameter: healthStore.AuthorizationRequest = {
        readDataTypes: [healthStore.samplePointHelper.dailyActivities.DATA_TYPE],
        writeDataTypes: []
      };
      let queryAuthorizationResponse = await healthStore.requestAuthorizations(context, authorizationParameter);
      let granted = queryAuthorizationResponse.readDataTypes.some(dataType => dataType.name === healthStore.samplePointHelper.dailyActivities.DATA_TYPE.name);
      return granted
    } catch (err) {
      return false
    }
  }

  public static async hasStepCountPermission(): Promise<boolean> {
    try {
      let parameter: healthStore.AuthorizationRequest = {
        readDataTypes: [healthStore.samplePointHelper.dailyActivities.DATA_TYPE],
        writeDataTypes: []
      };
      let queryAuthorizationResponse = await healthStore.getAuthorizations(parameter);
      let granted = queryAuthorizationResponse.readDataTypes.some(dataType => dataType.name === healthStore.samplePointHelper.dailyActivities.DATA_TYPE.name);
      return granted;
    } catch (err) {
      return false;
    }
  }

  public static async cancelAuthAll(): Promise<string> {
    try {
      await healthStore.cancelAuthorizations();
      hilog.info(0x0000, 'testTag', 'Succeeded in cancelling authorization.');
      return 'Succeeded in cancelling authorization.';
    } catch (err) {
      hilog.error(0x0000, 'testTag', `Failed to cancel authorization. Code: ${err.code}, message: ${err.message}`);
      return `Failed to cancel authorization. Code: ${err.code}, message: ${err.message}`;
    }
  }


}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
dailyActivities
healthKit
计步
2025-01-20 14:08:04
3.0w浏览
已于2025-2-5 08:47:07修改
收藏 0
回答 0


相关问题
HarmonyOS 传感器相关问题咨询?
558浏览 • 1回复 待解决
HarmonyOS 获取用户
758浏览 • 2回复 待解决
如何将某个时间转换成距现在时间
909浏览 • 1回复 待解决
HarmonyOS 怎么获取当天运动
987浏览 • 1回复 待解决
如何获取当前系统时间时间
1560浏览 • 1回复 待解决
HarmonyOS 如何获取当前时间时间
669浏览 • 1回复 待解决
HarmonyOS 设备获取每日
1164浏览 • 1回复 待解决
HarmonyOS 如何获取某个组件尺寸?
549浏览 • 1回复 待解决
ArkTS时间获取如何实现
5252浏览 • 1回复 已解决
HarmonyOS 怎么获取APP构建时间
389浏览 • 1回复 待解决
如何获取今天日期、时间戳?
1059浏览 • 1回复 待解决