目录

workout (Scenario-based Workout Services)

This module provides scenario-based workout services.

Since: 5.0.0(12)

Modules to Import

import { healthService } from '@kit.HealthServiceKit';

This module is a submodule of healthService and needs to be used in healthService.workout mode.

ActivityReport

Real-time Activity rings data.

Atomic service API: This API can be used in atomic services since API version 5.0.0(12).

System capability: SystemCapability.Health.HealthService

Since: 5.0.0(12)

Model restriction: This API can be used only in the stage model.

Name Type Read-Only Optional Description
steps number No No Step count.
stepsGoal number No Yes Step count goal. If this parameter is not set, the default goal displayed in the Huawei Health app cannot be read.
activeCalories number No No

Active calories.

Unit: cal

activeCaloriesGoal number No Yes

Activity calorie goal. The default value is the default goal displayed in the Huawei Health app.

Unit: cal

exercise number No No

Workout duration.

Unit: min

exerciseGoal number No Yes

Workout duration goal. The default value is the default goal displayed in the Huawei Health app.

Unit: min

activeHours number No No Number of active hours. If you are physically active for more than one minute during an hour, this hour will be counted as an active hour.
activeHoursGoal number No Yes Target number of active hours. If you are physically active for more than one minute during an hour, this hour will be counted as an active hour. The default value is the default target displayed in the Huawei Health app.

ConfigType

type ConfigType = number | string | boolean

Linkage configuration item type.

System capability: SystemCapability.Health.HealthService

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Type Description
number The value can be any number.
string The value can be any string.
boolean The value can be true or false. The specific meaning varies depending on the actual situation.

DeviceState

Linkage device status.

System capability: SystemCapability.Health.HealthService

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Name Type Read-Only Optional Description
deviceId string No No Device ID.
state number No No Device status.
deviceName string No Yes Device name. If this parameter is not set, it is empty by default.

Goal

Linkage workout goal.

System capability: SystemCapability.Health.HealthService

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Name Type Read-Only Optional Description
type number No No Goal type. For details about the value, please refer to TargetType.
value number No No Goal value.

LinkageType

Linkage type.

System capability: SystemCapability.Health.HealthService

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Name Value Description
COURSE_LINK 0 Course linkage.
ACTIVITY_LINK 1 Workout linkage.

StartCode

Result code when the linking service is enabled.

System capability: SystemCapability.Health.HealthService

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Name Value Description
SUCCESS 0 The linking service is enabled successfully.
WORKOUT_WORKING 1 The linking service is started.
NO_SUPPORTED_DEVICE 2 No device supports the linking service.
DEVICE_BUSY 3 The linkage device is busy.

StartResult

Result of enabling the linking service.

System capability: SystemCapability.Health.HealthService

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Name Type Read-Only Optional Description
startCode StartCode No No Result code when the linking service is enabled.
deviceState DeviceState[] No No Linkage device status.

SportInfo

Workout status information.

System capability: SystemCapability.Health.HealthService

Since: 6.1.1(24)

Model restriction: This API can be used only in the stage model.

Name Type Read-Only Optional Description
sportType number No No Workout type. For details, please refer to the subdata type ID in Workout Record Type Constants.
sportState SportState No No Workout status.

SportState

Workout status type.

System capability: SystemCapability.Health.HealthService

Since: 6.1.1(24)

Model restriction: This API can be used only in the stage model.

Name Value Description
IDLE 0 Initialized.
READY 1 Ready.
RUNNING 2 During the workout.
PAUSED 3 Paused.
STOPPED 4 Stopped.

TargetType

Linkage goal type.

System capability: SystemCapability.Health.HealthService

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Name Value Description
NONE 0 No goal.
DISTANCE 1 Distance.
CALORIE 2 Calories.
TIME 3 Duration.
SKIPPING_TIMES 4 Jump times.

WorkoutConfig

Linkage configuration item.

System capability: SystemCapability.Health.HealthService

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Name Type Read-Only Optional Description
linkageType LinkageType No No Linkage type.
sportType number No No Workout type. For details, please refer to the subdata type ID in Workout Record Type Constants.
activityGoals Goal[] No Yes Linkage workout goal. If this parameter is not set, it is empty by default.
extensionConfig Record<string, ConfigType> No Yes Extended configuration item. If this parameter is not set, it is empty by default.

workout.config

config(workoutConfig: WorkoutConfig): Promise<void>

Configures linkage. This API uses a promise to return the result.

This API can be used to develop wearables since API 19 Release.

Before calling this API, call init to perform initialization.

System capability: SystemCapability.Health.HealthService

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Parameters

Parameter Type Mandatory Description
workoutConfig WorkoutConfig Yes Linkage configuration item.

Returns

Type Description
Promise<void> Promise that returns no value.

Error codes:

For details about the error codes, please refer to Health Service Kit ArkTS API Error Codes and Universal Error Codes.

ID Error Message
201 Permission verification failed.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.
1009104003 Illegal command. Called when workout not in stopped or idle state.
1009104999 System internal error.

Example:

import { healthService, healthStore } from '@kit.HealthServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  let workoutOptions: healthService.workout.WorkoutConfig = {
    linkageType: healthService.workout.LinkageType.COURSE_LINK,
    sportType: healthStore.exerciseSequenceHelper.running.EXERCISE_TYPE.id
  };
  await healthService.workout.config(workoutOptions);
  hilog.info(0x0000, 'testTag', 'Succeeded in configuring workout');
} catch (err) {
  hilog.error(0x0000, 'testTag', `Failed to configure workout. Code: ${err.code}, message: ${err.message}`);
}

workout.start

start(): Promise<StartResult>

Enables linkage. This API uses a promise to return the result.

This API can be used to develop wearables since API 19 Release.

Before calling this API, call init to perform initialization.

System capability: SystemCapability.Health.HealthService

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Returns

Type Description
Promise<StartResult> Promise used to return the result of enabling the linking service.

Error codes:

For details about the error codes, please refer to Health Service Kit ArkTS API Error Codes.

ID Error Message
201 Permission verification failed.
1009104001 Sport service busy. Workout is already started by other application.
1009104002 Unsupported sport type.
1009104003 Illegal command. Called when workout in sporting, paused or stopped state.
1009104004 Permission verification error. Application has no permission, such as Motion Permission.
1009104999 System internal error.

Example:

import { healthService } from '@kit.HealthServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  await healthService.workout.start();
  hilog.info(0x0000, 'testTag', 'Succeeded in starting workout');
} catch (err) {
  hilog.error(0x0000, 'testTag', `Failed to start workout. Code: ${err.code}, message: ${err.message}`);
}

workout.pause

pause(): Promise<void>

Pauses linkage. This API uses a promise to return the result.

This API can be used to develop wearables since API 19 Release.

Before calling this API, call init to perform initialization.

System capability: SystemCapability.Health.HealthService

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Returns

Type Description
Promise<void> Promise that returns no value.

Error codes:

For details about the error codes, please refer to Health Service Kit ArkTS API Error Codes.

ID Error Message
201 Permission verification failed.
1009104003 Illegal command. Called when workout in ready, paused or stopped state.
1009104999 System internal error.

Example:

import { healthService } from '@kit.HealthServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  await healthService.workout.pause();
  hilog.info(0x0000, 'testTag', 'Succeeded in pausing workout');
} catch (err) {
  hilog.error(0x0000, 'testTag', `Failed to pause workout. Code: ${err.code}, message: ${err.message}`);
}

workout.resume

resume(): Promise<void>

Resumes linkage. This API uses a promise to return the result.

This API can be used to develop wearables since API 19 Release.

Before calling this API, call init to perform initialization.

System capability: SystemCapability.Health.HealthService

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Returns

Type Description
Promise<void> Promise that returns no value.

Error codes:

For details about the error codes, please refer to Health Service Kit ArkTS API Error Codes.

ID Error Message
201 Permission verification failed.
1009104003 Illegal command. Called when workout in ready, sporting or stopped state.
1009104999 System internal error.

Example:

import { healthService } from '@kit.HealthServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  await healthService.workout.resume();
  hilog.info(0x0000, 'testTag', 'Succeeded in resuming workout');
} catch (err) {
  hilog.error(0x0000, 'testTag', `Failed to resume workout. Code: ${err.code}, message: ${err.message}`);
}

workout.stop

stop(): Promise<void>

Stops linkage. This API uses a promise to return the result.

This API can be used to develop wearables since API 19 Release.

Before calling this API, call init to perform initialization.

System capability: SystemCapability.Health.HealthService

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Returns

Type Description
Promise<void> Promise that returns no value.

Error codes:

For details about the error codes, please refer to Health Service Kit ArkTS API Error Codes.

ID Error Message
201 Permission verification failed.
1009104003 Illegal command. Called when workout is not started.
1009104999 System internal error.

Example:

import { healthService } from '@kit.HealthServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  await healthService.workout.stop();
  hilog.info(0x0000, 'testTag', 'Succeeded in stopping workout');
} catch (err) {
  hilog.error(0x0000, 'testTag', `Failed to stop workout. Code: ${err.code}, message: ${err.message}`);
}

workout.onData

onData(dataType: healthStore.DataType, listener: Callback<SampleReal[]>): Promise<void>

Registers a listener for specified linkage fitness data. You are advised to subscribe to a maximum of three types of data. This API uses a promise to return the result.

This API can be used to develop wearables since API 19 Release.

Before calling this API, call init to perform initialization.

System capability: SystemCapability.Health.HealthService

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Parameters

Parameter Type Mandatory Description
dataType healthStore.DataType Yes Linkage workout data type.
listener Callback<SampleReal[]> Yes Callback used to return the linkage workout data.

Returns

Type Description
Promise<void> Promise that returns no value.

Error codes:

For details about the error codes, please refer to Health Service Kit ArkTS API Error Codes and Universal Error Codes.

ID Error Message
201 Permission verification failed.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types.

Example:

import { healthService, healthStore } from '@kit.HealthServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  const callback: Callback<healthService.SampleReal[]> = (sampleReals) => {
    hilog.info(0x0000, 'testTag', `Succeeded in receiving data. The sampleReals size is ${sampleReals.length}`);
  };
  const realTimeMotionDataType: healthStore.DataType = {
    id: 50004
  };
  await healthService.workout.onData(realTimeMotionDataType, callback);
} catch (err) {
  hilog.error(0x0000, 'testTag', `Failed to onData. Code: ${err.code}, message: ${err.message}`);
}

workout.onData

onData(dataType: undefined, listener: Callback<SampleReal[]>): Promise<void>

Registers a listener for all linkage workout data. This API uses a promise to return the result.

This API can be used to develop wearables since API 19 Release.

Before calling this API, call init to perform initialization.

System capability: SystemCapability.Health.HealthService

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Parameters

Parameter Type Mandatory Description
dataType undefined Yes All linkage workout data types for which the listener is registered.
listener Callback<SampleReal[]> Yes Callback used to return the linkage workout data.

Returns

Type Description
Promise<void> Promise that returns no value.

Error codes:

For details about the error codes, please refer to Health Service Kit ArkTS API Error Codes and Universal Error Codes.

ID Error Message
201 Permission verification failed.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.

Example:

import { healthService } from '@kit.HealthServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  const callback: Callback<healthService.SampleReal[]> = (sampleReals) => {
    hilog.info(0x0000, 'testTag', `Succeeded in receiving data. The sampleReals size is ${sampleReals.length}`);
  };
  await healthService.workout.onData(undefined, callback);
} catch (err) {
  hilog.error(0x0000, 'testTag', `Failed to onData. Code: ${err.code}, message: ${err.message}`);
}

workout.offData

offData(dataType: healthStore.DataType, listener: Callback<SampleReal[]>): Promise<void>

Cancels the listener for the specified linkage workout data. This API uses a promise to return the result.

This API can be used to develop wearables since API 19 Release.

Before calling this API, call init to perform initialization.

System capability: SystemCapability.Health.HealthService

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Parameters

Parameter Type Mandatory Description
dataType healthStore.DataType Yes Linkage workout data type.
listener Callback<SampleReal[]> Yes Callback used to return the linkage workout data.

Returns

Type Description
Promise<void> Promise that returns no value.

Error codes:

For details about the error codes, please refer to Health Service Kit ArkTS API Error Codes and Universal Error Codes.

ID Error Message
201 Permission verification failed.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3.Parameter verification failed.

Example:

import { healthService, healthStore } from '@kit.HealthServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  const callback: Callback<healthService.SampleReal[]> = (sampleReals) => {
    hilog.info(0x0000, 'testTag', `Succeeded in receiving data. The sampleReals size is ${sampleReals.length}`);
  };
  await healthService.workout.offData(healthStore.healthDataTypes.WORKOUT, callback);
} catch (err) {
  hilog.error(0x0000, 'testTag', `Failed to offData. Code: ${err.code}, message: ${err.message}`);
}

workout.offData

offData(dataType: undefined, listener: Callback<SampleReal[]>): Promise<void>

Cancels the listener for all linkage workout data. This API uses a promise to return the result.

This API can be used to develop wearables since API 19 Release.

Before calling this API, call init to perform initialization.

System capability: SystemCapability.Health.HealthService

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Parameters

Parameter Type Mandatory Description
dataType undefined Yes Types of all linkage workout data for which the listener is canceled.
listener Callback<SampleReal[]> Yes Callback used to return the linkage workout data.

Returns

Type Description
Promise<void> Promise that returns no value.

Error codes:

For details about the error codes, please refer to Health Service Kit ArkTS API Error Codes and Universal Error Codes.

ID Error Message
201 Permission verification failed.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Parameter verification failed.

Example:

import { healthService } from '@kit.HealthServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  const callback: Callback<healthService.SampleReal[]> = (sampleReals) => {
    hilog.info(0x0000, 'testTag', `Succeeded in receiving data. The sampleReals size is ${sampleReals.length}`);
  };
  await healthService.workout.offData(undefined, callback);
} catch (err) {
  hilog.error(0x0000, 'testTag', `Failed to offData. Code: ${err.code}, message: ${err.message}`);
}

workout.onEvent("*")

onEvent(event: "*", listener: Callback<SampleEvent>): Promise<void>

Registers a listener for linkage device events. This API uses a promise to return the result.

Before calling this API, call init to perform initialization.

System capability: SystemCapability.Health.HealthService

Device behavior differences: This API is supported on phones and tablets. On wearables, it returns error code 401.

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Parameters

Parameter Type Mandatory Description
event string Yes Linkage device event type. This field has a fixed value of *****. This event is triggered when the workout status of the linkage device is changed.
listener Callback<SampleEvent> Yes Callback used to return the linkage device event.

Returns

Type Description
Promise<void> Promise that returns no value.

Error codes:

For details about the error codes, please refer to Health Service Kit ArkTS API Error Codes and Universal Error Codes.

ID Error Message
201 Permission verification failed.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.

Example:

import { healthService } from '@kit.HealthServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  const callback: Callback<healthService.SampleEvent> = (event) => {
    hilog.info(0x0000, 'testTag', `Succeeded in receiving event. Event data: ${event.eventData}, event id: ${event.eventId}`);
  };
  await healthService.workout.onEvent('*', callback);
} catch (err) {
  hilog.error(0x0000, 'testTag', `Failed to onEvent. Code: ${err.code}, message: ${err.message}`);
}

workout.offEvent("*")

offEvent(event: "*", listener: Callback<SampleEvent>): Promise<void>

Unregisters a listener for linkage device events. This API uses a promise to return the result.

Before calling this API, call init to perform initialization.

System capability: SystemCapability.Health.HealthService

Device behavior differences: This API is supported on phones and tablets. On wearables, it returns error code 401.

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Parameters

Parameter Type Mandatory Description
event string Yes Linkage device event type. This field has a fixed value of *****. This event is triggered when the workout status of the linkage device is changed.
listener Callback<SampleEvent> Yes Callback used to return the linkage device event.

Returns

Type Description
Promise<void> Promise that returns no value.

Error codes:

For details about the error codes, please refer to Health Service Kit ArkTS API Error Codes and Universal Error Codes.

ID Error Message
201 Permission verification failed.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Parameter verification failed.

Example:

import { healthService } from '@kit.HealthServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  const callback: Callback<healthService.SampleEvent> = (event) => {
    hilog.info(0x0000, 'testTag', `Succeeded in receiving event. Event data: ${event.eventData}, event id: ${event.eventId}`);
  };
  await healthService.workout.offEvent('*', callback);
} catch (err) {
  hilog.error(0x0000, 'testTag', `Failed to offEvent. Code: ${err.code}, message: ${err.message}`);
}

workout.readActivityReport

readActivityReport(): Promise<ActivityReport>

Reads real-time Activity rings data. This API requires the DAILY_ACTIVITIES data type permission and uses a promise to return the result.

This API can be used to develop wearables since API 19 Release.

Before calling this API, call init to perform initialization.

Atomic service API: This API can be used in atomic services since API version 5.0.0(12).

System capability: SystemCapability.Health.HealthService

Since: 5.0.0(12)

Model restriction: This API can be used only in the stage model.

Returns

Type Description
Promise<ActivityReport> Promise used to return real-time Activity rings data.

Error codes:

For details about the error codes, please refer to Health Service Kit ArkTS API Error Codes.

ID Error Message
201 Permission denied.
14500101 Service exception.

Example:

import { healthService } from '@kit.HealthServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  const result: healthService.workout.ActivityReport = await healthService.workout.readActivityReport();
  
  hilog.info(0x0000, 'testTag', 'Succeeded in reading ActivityReport');
  Object.keys(result).forEach(key => {
    hilog.info(0x0000, 'testTag', `the ${key} is ${result[key]}`);
  });
} catch (err) {
  hilog.error(0x0000, 'testTag', `Failed to read ActivityReport. Code: ${err.code}, message: ${err.message}`);
}

workout.sendData

sendData(sampleReal: SampleReal[]): Promise<void>

Sends the fused workout data to the linkage device. This API uses a promise to return the result.

Before calling this API, call init to perform initialization.

System capability: SystemCapability.Health.HealthService

Device behavior differences: This API is supported on phones and tablets. On wearables, it returns error code 401.

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Parameters

Parameter Type Mandatory Description
sampleReal SampleReal[] Yes Fused workout data.

Returns

Type Description
Promise<void> Promise that returns no value.

Error codes:

For details about the error codes, please refer to Health Service Kit ArkTS API Error Codes and Universal Error Codes.

ID Error Message
201 Permission verification failed.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types.

Example:

import { healthService } from '@kit.HealthServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  const sampleReal: healthService.SampleReal = {
    dataType: { id: 50004 },
    time: 1695740400000, // 2023-09-26 23:00:00,
    fields: {
      hr: 90
    }
  };
  await healthService.workout.sendData([sampleReal]);
  hilog.info(0x0000, 'testTag', 'Succeeded in sending data.');
} catch (err) {
  hilog.error(0x0000, 'testTag', `Failed to send data. Code: ${err.code}, message: ${err.message}`);
}

workout.sendEvent

sendEvent(event: SampleEvent): Promise<void>

Sends the control event to the linkage device. This API uses a promise to return the result.

Before calling this API, call init to perform initialization.

System capability: SystemCapability.Health.HealthService

Device behavior differences: This API is supported on phones and tablets. On wearables, it returns error code 401.

Since: 5.1.0(18)

Model restriction: This API can be used only in the stage model.

Parameters

Parameter Type Mandatory Description
event SampleEvent Yes Linkage control event.

Returns

Type Description
Promise<void> Promise that returns no value.

Error codes:

For details about the error codes, please refer to Health Service Kit ArkTS API Error Codes and Universal Error Codes.

ID Error Message
201 Permission verification failed.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.

Example:

import { healthService } from '@kit.HealthServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  const sampleEvent: healthService.SampleEvent = {
    eventId: 800400002,
    eventLevel: 0,
    eventData: 'start'
  };
  await healthService.workout.sendEvent(sampleEvent);
  hilog.info(0x0000, 'testTag', 'Succeeded in sending event.');
} catch (err) {
  hilog.error(0x0000, 'testTag', `Failed to send event. Code: ${err.code}, message: ${err.message}`);
}

workout.getCurrentSportInfo

getCurrentSportInfo(): Promise<SportInfo>

Obtains the workout status. This API uses a promise to return the result.

Before calling this API, call init to perform initialization.

System capability: SystemCapability.Health.HealthService

Since: 6.1.1(24)

Model restriction: This API can be used only in the stage model.

Returns

Type Description
Promise<SportInfo> Promise used to return the workout status.

Error codes:

For details about the error codes, please refer to Health Service Kit ArkTS API Error Codes.

ID Error Message
201 Permission verification failed. Require workout management permission.Refer to the development documentation for permission request.

Example:

import { healthService } from '@kit.HealthServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  await healthService.workout.getCurrentSportInfo();
  hilog.info(0x0000, 'testTag', 'Succeeded in getting current sportInfo');
} catch (err) {
  hilog.error(0x0000, 'testTag', `Failed to getting current sportInfo'. Code: ${err.code}, message: ${err.message}`);
}