中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
无法获取计步数据,申请计步资质已经通过,也配置了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}`; } } }
微信扫码分享