We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
ReferencesSystemHardwareCar KitArkTS APIssmartMobilityCommon (Smart Mobility)

smartMobilityCommon (Smart Mobility)

Phone5.0.0(12)+Tablet5.0.0(12)+

smartMobilityCommon is provided as a common function for apps to listen to the status of distributed services (such as HiCar and Super Home Screen) and implement event listening.

Since: 5.0.0(12)

Modules to Import

import { smartMobilityCommon } from '@kit.CarKit';

SmartMobilityEvent

Phone5.0.0(12)+Tablet5.0.0(12)+

Common event.

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

System capability: SystemCapability.CarService.DistributedEngine

Since: 5.0.0(12)

Expand
Name Type Read-Only Optional Description
eventName string No No Event name, which is defined by the service party.
type SmartMobilityType No No Service type.
data Record<string, Object> No No Detailed event information, which is determined jointly by the smart mobility service and app parties.

SmartMobilityInfo

Phone5.0.0(12)+Tablet5.0.0(12)+

Smart mobility status information, including the current service type, connection status, and service data.

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

System capability: SystemCapability.CarService.DistributedEngine

Since: 5.0.0(12)

Expand
Name Type Read-Only Optional Description
status SmartMobilityStatus No No Service connection status.
type SmartMobilityType No No Service type.
data Record<string, Object> No No Detailed connection information, which is determined jointly by the smart mobility service and app parties.

SmartMobilityStatus

Phone5.0.0(12)+Tablet5.0.0(12)+

Enumerated values of the service connection status.

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

System capability: SystemCapability.CarService.DistributedEngine

Since: 5.0.0(12)

Expand
Name Value Description
IDLE 0 The service is in the idle state. This is the default value when the service is not started.
RUNNING 1 The service is running.

SmartMobilityType

Phone5.0.0(12)+Tablet5.0.0(12)+

Enumerated values of the service type.

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

System capability: SystemCapability.CarService.DistributedEngine

Since: 5.0.0(12)

Expand
Name Value Description
HICAR 0 HiCar.
SUPER_LAUNCHER 1 Super Home Screen.
CAR_HOP 2 Hop.

getSmartMobilityAwareness

Phone5.0.0(12)+Tablet5.0.0(12)+

getSmartMobilityAwareness(): SmartMobilityAwareness

Obtains SmartMobilityAwareness.

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

System capability: SystemCapability.CarService.DistributedEngine

Device behavior differences: For 6.0.0(22) and earlier versions, this API is supported on phones. On other device types, it returns error code 801. For 6.1.0(23) and later versions, this API is supported on both phones and tablets, but does not respond on other device types.

Required permission: ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE

Since: 5.0.0(12)

Returns:

Expand
Type Description
SmartMobilityAwareness Smart mobility management class.

Example:

import { smartMobilityCommon } from '@kit.CarKit';

let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();

Error codes:

For details about the error codes, please refer to Universal Error Codes.

Expand
ID Error Message
201 Permission denied.
801 Capability not supported.

SmartMobilityAwareness

Phone5.0.0(12)+Tablet5.0.0(12)+

Smart mobility management class, which is used to call smart mobility APIs.

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

System capability: SystemCapability.CarService.DistributedEngine

Since: 5.0.0(12)

on('smartMobilityEvent')

Phone5.0.0(12)+Tablet5.0.0(12)+

on(type: 'smartMobilityEvent', smartMobilityTypes: SmartMobilityType[],callback: Callback<SmartMobilityEvent>): void

Registers an event listener of the smart mobility service. For example, this callback is triggered when a navigation hop is complete. This API uses an asynchronous callback to return the result.

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

System capability: SystemCapability.CarService.DistributedEngine

Device behavior differences: For 6.0.0(22) and earlier versions, this API is supported on phones. On other device types, it returns error code 801. For 6.1.0(23) and later versions, this API is supported on both phones and tablets, but does not respond on other device types.

Required permission: ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE

Since: 5.0.0(12)

Parameters:

Expand
Parameter Type Mandatory Description
type string Yes

Subscription type. The value is a constant:

smartMobilityEvent, which indicates registering an event listener.

smartMobilityTypes SmartMobilityType[] Yes Service type array, which can contain multiple services.
callback Callback<SmartMobilityEvent> Yes Mobility service event callback function, which returns the common event object of smart mobility.

Error codes:

For details about the error codes, please refer to Universal Error Codes.

Expand
ID Error Message
201 Permission denied.
401

Parameter error. Possible causes:

1. Mandatory parameters are left unspecified;

2. Incorrect parameter type;

3. Parameter verification failed;

4. The size of specified type is greater than 255.

801 Capability not supported.

Example:

import { smartMobilityCommon } from '@kit.CarKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  // Obtain the SmartMobilityAwareness instance.
  let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
  // Service type.
  let types: smartMobilityCommon.SmartMobilityType[] = [smartMobilityCommon.SmartMobilityType.CAR_HOP];
  // Mobility service event callback function.
  const callBack = (event: smartMobilityCommon.SmartMobilityEvent) => {
    hilog.info(0x0000, 'testTag', 'Received smart mobility event: ', JSON.stringify(event));
  };
  // Register a mobility service event listener.
  awareness.on('smartMobilityEvent', types, callBack);
} catch (e) {
  // Obtain the error code for an API call exception and handle the exception.
  hilog.error(0x0000, 'testTag', `on smart mobility event error, error code: ${e?.code}`);
}

off('smartMobilityEvent')

Phone5.0.0(12)+Tablet5.0.0(12)+

off(type: 'smartMobilityEvent', smartMobilityTypes: SmartMobilityType[], callback?: Callback<SmartMobilityEvent>): void

Unregisters a smart mobility service event listener. This API uses an asynchronous callback to return the result.

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

System capability: SystemCapability.CarService.DistributedEngine

Device behavior differences: For 6.0.0(22) and earlier versions, this API is supported on phones. On other device types, it returns error code 801. For 6.1.0(23) and later versions, this API is supported on both phones and tablets, but does not respond on other device types.

Required permission: ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE

Since: 5.0.0(12)

Parameters:

Expand
Parameter Type Mandatory Description
type string Yes

Subscription type. The value is a constant:

smartMobilityEvent, which indicates unregistering an event listener.

smartMobilityTypes SmartMobilityType[] Yes Service type array, which can contain multiple services.
callback Callback<SmartMobilityEvent> No Callback function to be unregistered, which must be the same as that passed upon subscription. If this parameter is not passed, all callback functions will be unregistered.

Error codes:

For details about the error codes, please refer to Universal Error Codes.

Expand
ID Error Message
201 Permission denied.
401

Parameter error. Possible causes:

1. Mandatory parameters are left unspecified;

2. Incorrect parameter type;

3. Parameter verification failed;

4. The size of specified type is greater than 255.

801 Capability not supported.

Example:

import { smartMobilityCommon } from '@kit.CarKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  // Obtain the SmartMobilityAwareness instance.
  let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
  // Service type.
  let types: smartMobilityCommon.SmartMobilityType[] = [smartMobilityCommon.SmartMobilityType.CAR_HOP];
  // Mobility service event callback function.
  const callBack = (event: smartMobilityCommon.SmartMobilityEvent) => {
    hilog.info(0x0000, 'testTag', 'Received smart mobility event: ', JSON.stringify(event));
  };

  // Unregister the mobility service event listener.
  awareness.off('smartMobilityEvent', types, callBack);
} catch (e) {
  // Obtain the error code for an API call exception and handle the exception.
  hilog.error(0x0000, 'testTag', `off smart mobility event error, error code: ${e?.code}`);
}

getSmartMobilityEvent

Phone5.0.0(12)+Tablet5.0.0(12)+

getSmartMobilityEvent(type: SmartMobilityType, eventName: string): SmartMobilityEvent

Obtains the information about a specified event.

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

System capability: SystemCapability.CarService.DistributedEngine

Device behavior differences: For 6.0.0(22) and earlier versions, this API is supported on phones. On other device types, it returns error code 801. For 6.1.0(23) and later versions, this API is supported on both phones and tablets, but does not respond on other device types.

Required permission: ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE

Since: 5.0.0(12)

Parameters:

Expand
Parameter Type Mandatory Description
type SmartMobilityType Yes Service type.
eventName string Yes

Event name. The options are as follows:

CAR_HOP_EVENT: hop event

HICAR_EVENT: HiCar event

SUPER_LAUNCHER_EVENT: Super Home Screen event

For other values, the API returns error code 401.

Returns:

Expand
Type Description
SmartMobilityEvent Common event information.

Error codes:

For details about the error codes, please refer to Universal Error Codes.

Expand
ID Error Message
201 Permission denied.
401

Parameter error. Possible causes:

1. Mandatory parameters are left unspecified;

2. Incorrect parameter type;

3. Parameter verification failed;

4. The size of specified type is greater than 255.

801 Capability not supported.

Example:

import { smartMobilityCommon } from '@kit.CarKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  // Obtain the SmartMobilityAwareness instance.
  let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
  // Service type.
  let type = smartMobilityCommon.SmartMobilityType.CAR_HOP;
  // Event name.
  let eventName: string = 'CAR_HOP_EVENT';
  // Obtain the common event information.
  let event: smartMobilityCommon.SmartMobilityEvent = awareness.getSmartMobilityEvent(type, eventName);
  hilog.info(0x0000, 'testTag', `get smart mobility event succeed, event: ${JSON.stringify(event)}`);
} catch (e) {
  // Obtain the error code for an API call exception and handle the exception.
  hilog.error(0x0000, 'testTag', `get smart mobility event error, error code: ${e?.code}`);
}

on('smartMobilityStatus')

Phone5.0.0(12)+Tablet5.0.0(12)+

on(type: 'smartMobilityStatus', smartMobilityTypes: SmartMobilityType[], callback: Callback<SmartMobilityInfo>): void

Registers a smart mobility connection status listener. This API uses an asynchronous callback to return the result.

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

System capability: SystemCapability.CarService.DistributedEngine

Device behavior differences: For 6.0.0(22) and earlier versions, this API is supported on phones. On other device types, it returns error code 801. For 6.1.0(23) and later versions, this API is supported on both phones and tablets, but does not respond on other device types.

Required permission: ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE

Since: 5.0.0(12)

Parameters:

Expand
Parameter Type Mandatory Description
type string Yes

Subscription type. The value is a constant:

smartMobilityStatus, which indicates registering a service connection status listener.

smartMobilityTypes SmartMobilityType[] Yes Service type array, which can contain multiple services.
callback Callback<SmartMobilityInfo> Yes Mobility connection status callback function, which returns the status information object of smart mobility.

Error codes:

For details about the error codes, please refer to Universal Error Codes.

Expand
ID Error Message
201 Permission denied.
401

Parameter error. Possible causes:

1. Mandatory parameters are left unspecified;

2. Incorrect parameter type;

3. Parameter verification failed;

4. The size of specified type is greater than 255.

801 Capability not supported.

Example:

import { smartMobilityCommon } from '@kit.CarKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  // Obtain the SmartMobilityAwareness instance.
  let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
  // Service type.
  let types: smartMobilityCommon.SmartMobilityType[] = [smartMobilityCommon.SmartMobilityType.HICAR];
  // Mobility connection status callback function.
  const callBack = (info: smartMobilityCommon.SmartMobilityInfo) => {
    hilog.info(0x0000, 'testTag', 'Received smart mobility info: ', JSON.stringify(info));
  };
  // Register a smart mobility connection status listener.
  awareness.on('smartMobilityStatus', types, callBack);
} catch (e) {
  // Obtain the error code for an API call exception and handle the exception.
  hilog.error(0x0000, 'testTag', `on smart mobility status error, error code: ${e?.code}`);
}

off('smartMobilityStatus')

Phone5.0.0(12)+Tablet5.0.0(12)+

off(type: 'smartMobilityStatus', smartMobilityTypes: SmartMobilityType[], callback?: Callback<SmartMobilityInfo>): void

Unregisters a smart mobility connection status listener. This API uses an asynchronous callback to return the result.

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

System capability: SystemCapability.CarService.DistributedEngine

Device behavior differences: For 6.0.0(22) and earlier versions, this API is supported on phones. On other device types, it returns error code 801. For 6.1.0(23) and later versions, this API is supported on both phones and tablets, but does not respond on other device types.

Required permission: ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE

Since: 5.0.0(12)

Parameters:

Expand
Parameter Type Mandatory Description
type string Yes

Subscription type. The value is a constant:

smartMobilityStatus, which indicates unregistering a service connection status listener.

smartMobilityTypes SmartMobilityType[] Yes Service type array, which can contain multiple services.
callback Callback<SmartMobilityInfo> No Callback function to be unregistered, which must be the same as that passed upon subscription. If this parameter is not passed, all callback functions will be unregistered.

Error codes:

For details about the error codes, please refer to Universal Error Codes.

Expand
ID Error Message
201 Permission denied.
401

Parameter error. Possible causes:

1. Mandatory parameters are left unspecified;

2. Incorrect parameter type;

3. Parameter verification failed;

4. The size of specified type is greater than 255.

801 Capability not supported.

Example:

import { smartMobilityCommon } from '@kit.CarKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  // Obtain the SmartMobilityAwareness instance.
  let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
  // Service type.
  let types: smartMobilityCommon.SmartMobilityType[] = [smartMobilityCommon.SmartMobilityType.HICAR];
  // Mobility connection status callback function.
  const callBack = (info: smartMobilityCommon.SmartMobilityInfo) => {
    hilog.info(0x0000, 'testTag', 'Received smart mobility info: ', JSON.stringify(info));
  };
  // Unregister the smart mobility connection status listener.
  awareness.off('smartMobilityStatus', types, callBack);
} catch (e) {
  // Obtain the error code for an API call exception and handle the exception.
  hilog.error(0x0000, 'testTag', `off smart mobility status error, error code: ${e?.code}`);
}

getSmartMobilityStatus

Phone5.0.0(12)+Tablet5.0.0(12)+

getSmartMobilityStatus(type: SmartMobilityType): SmartMobilityInfo

Obtains the smart mobility connection status.

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

System capability: SystemCapability.CarService.DistributedEngine

Device behavior differences: For 6.0.0(22) and earlier versions, this API is supported on phones. On other device types, it returns error code 801. For 6.1.0(23) and later versions, this API is supported on both phones and tablets, but does not respond on other device types.

Required permission: ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE

Since: 5.0.0(12)

Parameters:

Expand
Parameter Type Mandatory Description
type SmartMobilityType Yes Service type.

Returns:

Expand
Type Description
SmartMobilityInfo Service connection status information.

Error codes:

For details about the error codes, please refer to Universal Error Codes.

Expand
ID Error Message
201 Permission denied.
401

Parameter error. Possible causes:

1. Mandatory parameters are left unspecified;

2. Incorrect parameter type;

3. Parameter verification failed;

4. The size of specified type is greater than 255.

801 Capability not supported.

Example:

import { smartMobilityCommon } from '@kit.CarKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

try {
  // Obtain the SmartMobilityAwareness instance.
  let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
  // Service type.
  let type = smartMobilityCommon.SmartMobilityType.HICAR;
  // Obtain the service status information.
  let ret = awareness.getSmartMobilityStatus(type);
  hilog.info(0x0000, 'testTag', `get smart mobility status succeed, status: ${JSON.stringify(ret)}`);
} catch (e) {
  // Obtain the error code for an API call exception and handle the exception.
  hilog.error(0x0000, 'testTag', `get smart mobility status error, errCode: ${e?.code}`);
}
Search in References
Enter a keyword.