文档管理中心
您当前正在浏览HarmonyOS最新文档,覆盖已发布的所有API版本,可在API参考中筛选您使用的API版本。详细的版本配套关系请参考版本说明
指南与API参考API参考系统硬件Car Kit(车服务)ArkTS APIsmartMobilityCommon (智慧出行场景)

smartMobilityCommon (智慧出行场景)

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

作为分布式业务公共功能提供给应用,实现监听分布式业务(HiCar、超级桌面等)的状态,以及事件监听的能力。

起始版本: 5.0.0(12)

导入模块

收起
自动换行
深色代码主题
复制
  1. import { smartMobilityCommon } from '@kit.CarKit';

SmartMobilityEvent

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

公共事件。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.CarService.DistributedEngine

起始版本: 5.0.0(12)

展开
名称 类型 只读 可选 说明
eventName string 事件名,由业务方自定义。
type SmartMobilityType 业务类型。
data Record<string, Object> 事件详细信息,内容由智慧出行业务和应用双方约定。

SmartMobilityInfo

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

该类为智慧出行的状态信息,定义了当前的业务类型、连接状态、业务数据。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.CarService.DistributedEngine

起始版本: 5.0.0(12)

展开
名称 类型 只读 可选 说明
status SmartMobilityStatus 业务连接状态。
type SmartMobilityType 业务类型。
data Record<string, Object> 连接详细信息,内容由智慧出行业务和应用双方约定。

SmartMobilityStatus

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

业务连接状态枚举值。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.CarService.DistributedEngine

起始版本: 5.0.0(12)

展开
名称 说明
IDLE 0 业务处于空闲态,业务未启动时,默认为该值。
RUNNING 1 业务处于运行中。

SmartMobilityType

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

业务类型枚举值。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.CarService.DistributedEngine

起始版本: 5.0.0(12)

展开
名称 说明
HICAR 0 HiCar。
SUPER_LAUNCHER 1 超级桌面。
CAR_HOP 2 流转。

getSmartMobilityAwareness

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

getSmartMobilityAwareness(): SmartMobilityAwareness

用于获取智慧出行管理类。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.CarService.DistributedEngine

设备行为差异:对于6.0.0(22)及之前的版本,该接口在Phone中可正常调用,在其他设备类型中返回801错误码。在6.1.0(23)及之后版本,该接口在Phone、Tablet中均可正常调用,在其它设备中无效果。

需要权限: ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE

起始版本: 5.0.0(12)

返回值:

展开
类型 说明
SmartMobilityAwareness 智慧出行管理类。

示例:

收起
自动换行
深色代码主题
复制
  1. import { smartMobilityCommon } from '@kit.CarKit';
  2. let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();

错误码:

以下错误码的详细介绍请参见通用错误码

展开
错误码ID 错误信息
201 Permission denied.
801 Capability not supported.

SmartMobilityAwareness

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

智慧出行管理类,用于调用智慧出行接口。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.CarService.DistributedEngine

起始版本: 5.0.0(12)

on('smartMobilityEvent')

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

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

注册智慧出行业务的事件监听,例如导航流转完成后通知事件时,触发此回调执行。使用callback异步回调。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.CarService.DistributedEngine

设备行为差异:对于6.0.0(22)及之前的版本,该接口在Phone中可正常调用,在其他设备类型中返回801错误码。在6.1.0(23)及之后版本,该接口在Phone、Tablet中均可正常调用,在其它设备中无效果。

需要权限: ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE

起始版本: 5.0.0(12)

参数:

展开
参数名 类型 必填 说明
type string

订阅类型,该位置为常量值:

'smartMobilityEvent',表示订阅事件监听。

smartMobilityTypes SmartMobilityType[] 业务类型数组,支持同时订阅多个业务。
callback Callback<SmartMobilityEvent> 出行业务事件回调函数,返回智慧出行公共事件对象。

错误码:

以下错误码的详细介绍请参见通用错误码

展开
错误码ID 错误信息
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.

示例:

收起
自动换行
深色代码主题
复制
  1. import { smartMobilityCommon } from '@kit.CarKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. try {
  4. // 获取SmartMobilityAwareness实例
  5. let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
  6. // 业务类型
  7. let types: smartMobilityCommon.SmartMobilityType[] = [smartMobilityCommon.SmartMobilityType.CAR_HOP];
  8. // 出行业务事件回调函数
  9. const callBack = (event: smartMobilityCommon.SmartMobilityEvent) => {
  10. hilog.info(0x0000, 'testTag', 'Received smart mobility event: ', JSON.stringify(event));
  11. };
  12. // 注册出行业务事件监听
  13. awareness.on('smartMobilityEvent', types, callBack);
  14. } catch (e) {
  15. // 捕获接口调用异常时的错误码并做相应处理
  16. hilog.error(0x0000, 'testTag', `on smart mobility event error, error code: ${e?.code}`);
  17. }

off('smartMobilityEvent')

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

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

取消注册智慧出行业务的事件监听。使用callback异步回调。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.CarService.DistributedEngine

设备行为差异:对于6.0.0(22)及之前的版本,该接口在Phone中可正常调用,在其他设备类型中返回801错误码。在6.1.0(23)及之后版本,该接口在Phone、Tablet中均可正常调用,在其它设备中无效果。

需要权限: ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE

起始版本: 5.0.0(12)

参数:

展开
参数名 类型 必填 说明
type string

订阅类型,该位置为常量值:

'smartMobilityEvent',表示取消事件监听。

smartMobilityTypes SmartMobilityType[] 业务类型数组,支持同时取消多个业务监听。
callback Callback<SmartMobilityEvent> 需要取消注册的回调函数,需与订阅时传入的回调函数是同一个。若无此参数,则取消注册所有的回调函数。

错误码:

以下错误码的详细介绍请参见通用错误码

展开
错误码ID 错误信息
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.

示例:

收起
自动换行
深色代码主题
复制
  1. import { smartMobilityCommon } from '@kit.CarKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. try {
  4. // 获取SmartMobilityAwareness实例
  5. let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
  6. // 业务类型
  7. let types: smartMobilityCommon.SmartMobilityType[] = [smartMobilityCommon.SmartMobilityType.CAR_HOP];
  8. // 出行业务事件回调函数
  9. const callBack = (event: smartMobilityCommon.SmartMobilityEvent) => {
  10. hilog.info(0x0000, 'testTag', 'Received smart mobility event: ', JSON.stringify(event));
  11. };
  12. // 解注册出行业务事件监听
  13. awareness.off('smartMobilityEvent', types, callBack);
  14. } catch (e) {
  15. // 捕获接口调用异常时的错误码并做相应处理
  16. hilog.error(0x0000, 'testTag', `off smart mobility event error, error code: ${e?.code}`);
  17. }

getSmartMobilityEvent

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

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

应用获取指定事件的信息。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.CarService.DistributedEngine

设备行为差异:对于6.0.0(22)及之前的版本,该接口在Phone中可正常调用,在其他设备类型中返回801错误码。在6.1.0(23)及之后版本,该接口在Phone、Tablet中均可正常调用,在其它设备中无效果。

需要权限: ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE

起始版本: 5.0.0(12)

参数:

展开
参数名 类型 必填 说明
type SmartMobilityType 业务类型。
eventName string

事件名。取值有下面3种:

CAR_HOP_EVENT,流转事件。

HICAR_EVENT,HiCar事件。

SUPER_LAUNCHER_EVENT,超级桌面事件。

其他取值,则接口返回401错误码。

返回值:

展开
类型 说明
SmartMobilityEvent 公共事件信息。

错误码:

以下错误码的详细介绍请参见通用错误码

展开
错误码ID 错误信息
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.

示例:

收起
自动换行
深色代码主题
复制
  1. import { smartMobilityCommon } from '@kit.CarKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. try {
  4. // 获取SmartMobilityAwareness实例
  5. let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
  6. // 业务类型
  7. let type = smartMobilityCommon.SmartMobilityType.CAR_HOP;
  8. // 事件名称
  9. let eventName: string = 'CAR_HOP_EVENT';
  10. // 获取公共事件信息
  11. let event: smartMobilityCommon.SmartMobilityEvent = awareness.getSmartMobilityEvent(type, eventName);
  12. hilog.info(0x0000, 'testTag', `get smart mobility event succeed, event: ${JSON.stringify(event)}`);
  13. } catch (e) {
  14. // 捕获接口调用异常时的错误码并做相应处理
  15. hilog.error(0x0000, 'testTag', `get smart mobility event error, error code: ${e?.code}`);
  16. }

on('smartMobilityStatus')

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

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

注册智慧出行连接状态的监听。使用callback异步回调。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.CarService.DistributedEngine

设备行为差异:对于6.0.0(22)及之前的版本,该接口在Phone中可正常调用,在其他设备类型中返回801错误码。在6.1.0(23)及之后版本,该接口在Phone、Tablet中均可正常调用,在其它设备中无效果。

需要权限: ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE

起始版本: 5.0.0(12)

参数:

展开
参数名 类型 必填 说明
type string

订阅类型,该位置为常量值:

'smartMobilityStatus',表示订阅业务连接状态监听。

smartMobilityTypes SmartMobilityType[] 业务类型数组,支持同时订阅多个业务。
callback Callback<SmartMobilityInfo> 出行连接状态回调函数,返回智慧出行状态信息对象。

错误码:

以下错误码的详细介绍请参见通用错误码

展开
错误码ID 错误信息
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.

示例:

收起
自动换行
深色代码主题
复制
  1. import { smartMobilityCommon } from '@kit.CarKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. try {
  4. // 获取SmartMobilityAwareness实例
  5. let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
  6. // 业务类型
  7. let types: smartMobilityCommon.SmartMobilityType[] = [smartMobilityCommon.SmartMobilityType.HICAR];
  8. // 出行连接状态回调函数
  9. const callBack = (info: smartMobilityCommon.SmartMobilityInfo) => {
  10. hilog.info(0x0000, 'testTag', 'Received smart mobility info: ', JSON.stringify(info));
  11. };
  12. // 注册智慧出行连接状态的监听
  13. awareness.on('smartMobilityStatus', types, callBack);
  14. } catch (e) {
  15. // 捕获接口调用异常时的错误码并做相应处理
  16. hilog.error(0x0000, 'testTag', `on smart mobility status error, error code: ${e?.code}`);
  17. }

off('smartMobilityStatus')

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

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

取消注册智慧出行连接状态的监听。使用callback异步回调。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.CarService.DistributedEngine

设备行为差异:对于6.0.0(22)及之前的版本,该接口在Phone中可正常调用,在其他设备类型中返回801错误码。在6.1.0(23)及之后版本,该接口在Phone、Tablet中均可正常调用,在其它设备中无效果。

需要权限: ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE

起始版本: 5.0.0(12)

参数:

展开
参数名 类型 必填 说明
type string

订阅类型,该位置为常量值:

'smartMobilityStatus',表示取消业务连接状态监听。

smartMobilityTypes SmartMobilityType[] 业务类型数组,支持同时取消多个业务监听。
callback Callback<SmartMobilityInfo> 需要取消注册的回调函数,需与订阅时传入的回调函数是同一个。若无此参数,则取消注册所有的回调函数。

错误码:

以下错误码的详细介绍请参见通用错误码

展开
错误码ID 错误信息
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.

示例:

收起
自动换行
深色代码主题
复制
  1. import { smartMobilityCommon } from '@kit.CarKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. try {
  4. // 获取SmartMobilityAwareness实例
  5. let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
  6. // 业务类型
  7. let types: smartMobilityCommon.SmartMobilityType[] = [smartMobilityCommon.SmartMobilityType.HICAR];
  8. // 出行连接状态回调函数
  9. const callBack = (info: smartMobilityCommon.SmartMobilityInfo) => {
  10. hilog.info(0x0000, 'testTag', 'Received smart mobility info: ', JSON.stringify(info));
  11. };
  12. // 取消注册智慧出行连接状态的监听
  13. awareness.off('smartMobilityStatus', types, callBack);
  14. } catch (e) {
  15. // 捕获接口调用异常时的错误码并做相应处理
  16. hilog.error(0x0000, 'testTag', `off smart mobility status error, error code: ${e?.code}`);
  17. }

getSmartMobilityStatus

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

getSmartMobilityStatus(type: SmartMobilityType): SmartMobilityInfo

获取智慧出行连接状态。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.CarService.DistributedEngine

设备行为差异:对于6.0.0(22)及之前的版本,该接口在Phone中可正常调用,在其他设备类型中返回801错误码。在6.1.0(23)及之后版本,该接口在Phone、Tablet中均可正常调用,在其它设备中无效果。

需要权限: ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE

起始版本: 5.0.0(12)

参数:

展开
参数名 类型 必填 说明
type SmartMobilityType 业务类型。

返回值:

展开
类型 说明
SmartMobilityInfo 业务状态信息。

错误码:

以下错误码的详细介绍请参见通用错误码

展开
错误码ID 错误信息
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.

示例:

收起
自动换行
深色代码主题
复制
  1. import { smartMobilityCommon } from '@kit.CarKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. try {
  4. // 获取SmartMobilityAwareness实例
  5. let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
  6. // 业务类型
  7. let type = smartMobilityCommon.SmartMobilityType.HICAR;
  8. // 获取业务状态信息
  9. let ret = awareness.getSmartMobilityStatus(type);
  10. hilog.info(0x0000, 'testTag', `get smart mobility status succeed, status: ${JSON.stringify(ret)}`);
  11. } catch (e) {
  12. // 捕获接口调用异常时的错误码并做相应处理
  13. hilog.error(0x0000, 'testTag', `get smart mobility status error, errCode: ${e?.code}`);
  14. }