【鸿蒙next教程】车载组件:smartMobilityCommon(智慧出行场景)
本文详细介绍了HarmonyOS 5.0智慧出行(smartMobilityCommon)开发接口,包括事件监听、状态管理等核心功能。
往期鸿蒙5.0全套实战文章必看:(文中附带鸿蒙5.0全栈学习资料)
smartMobilityCommon(智慧出行场景)
作为分布式业务公共功能提供给应用,实现监听分布式业务(HiCar、超级桌面等)的状态,以及实现事件监听的能力。
起始版本:5.0.0(12)
导入模块
import { smartMobilityCommon } from '@kit.CarKit';
SmartMobilityEvent
公共事件。
系统能力:SystemCapability.CarService.DistributedEngine
起始版本:5.0.0(12)
|
名称 |
类型 |
只读 |
可选 |
说明 |
|---|---|---|---|---|
|
eventName |
string |
否 |
否 |
事件名,由业务方自定义。 |
|
type |
否 |
否 |
业务类型。 |
|
|
data |
Record<string, Object> |
否 |
否 |
事件详细信息,内容由业务和应用双方约定。 |
SmartMobilityInfo
该类为智慧出行的状态信息,定义了当前的业务类型、连接状态、业务数据。
系统能力:SystemCapability.CarService.DistributedEngine
起始版本:5.0.0(12)
|
名称 |
类型 |
只读 |
可选 |
说明 |
|---|---|---|---|---|
|
status |
否 |
否 |
业务连接状态。 |
|
|
type |
否 |
否 |
业务类型。 |
|
|
data |
Record<string, Object> |
否 |
否 |
连接详细信息,内容由业务和应用双方约定。 |
SmartMobilityStatus
业务连接状态枚举值。
系统能力:SystemCapability.CarService.DistributedEngine
起始版本:5.0.0(12)
|
名称 |
值 |
说明 |
|---|---|---|
|
IDLE |
0 |
业务处于空闲态。 说明 业务未启动时,默认为该值。 |
|
RUNNING |
1 |
业务处于运行中。 |
SmartMobilityType
业务类型枚举值。
系统能力:SystemCapability.CarService.DistributedEngine
起始版本:5.0.0(12)
|
名称 |
值 |
说明 |
|---|---|---|
|
HICAR |
0 |
HiCar。 |
|
SUPER_LAUNCHER |
1 |
超级桌面。 |
|
CAR_HOP |
2 |
流转。 |
getSmartMobilityAwareness
getSmartMobilityAwareness(): SmartMobilityAwareness
用于获取智慧出行管理类。
系统能力:SystemCapability.CarService.DistributedEngine
权限描述:ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE
起始版本:5.0.0(12)
返回值:
|
类型 |
说明 |
|---|---|
|
智慧出行管理类。 |
示例:
import { smartMobilityCommon } from '@kit.CarKit';
let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
错误码:
|
错误码ID |
错误信息 |
|---|---|
|
201 |
Permission verify failed. |
SmartMobilityAwareness
智慧出行管理类,用于调用智慧出行接口。
系统能力:SystemCapability.CarService.DistributedEngine
起始版本:5.0.0(12)
on(type: 'smartMobilityEvent')
on(type: 'smartMobilityEvent', smartMobilityTypes: SmartMobilityType[],callback: Callback<SmartMobilityEvent>): void
应用注册事件监听。
系统能力:SystemCapability.CarService.DistributedEngine
权限描述:ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE
起始版本:5.0.0(12)
参数:
|
参数名 |
类型 |
必填 |
说明 |
|---|---|---|---|
|
type |
string |
是 |
订阅类型,该位置为常量值: 'smartMobilityEvent',表示订阅事件监听 |
|
smartMobilityTypes |
是 |
业务类型数组,支持同时订阅多个业务。 |
|
|
callback |
Callback<SmartMobilityEvent> |
是 |
出行业务事件回调函数。 |
错误码:
|
错误码ID |
错误信息 |
|---|---|
|
201 |
Permission verify failed. |
|
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. |
示例:
import { smartMobilityCommon } from '@kit.CarKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
// Get single instance of SmartMobilityAwareness.
let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
// 业务类型
let types: smartMobilityCommon.SmartMobilityType[] = [smartMobilityCommon.SmartMobilityType.CAR_HOP];
// 出行业务事件回调函数
const callBack = (event: smartMobilityCommon.SmartMobilityEvent) => {
hilog.info(0x0000, 'Received smart mobility event: ', JSON.stringify(event));
};
// 注册出行业务事件监听
awareness.on('smartMobilityEvent', types, callBack);
off(type: 'smartMobilityEvent')
off(type: 'smartMobilityEvent', smartMobilityTypes: SmartMobilityType[], callback?: Callback<SmartMobilityEvent>): void
应用取消事件监听。
系统能力:SystemCapability.CarService.DistributedEngine
权限描述:ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE
起始版本:5.0.0(12)
参数:
|
参数名 |
类型 |
必填 |
说明 |
|---|---|---|---|
|
type |
string |
是 |
订阅类型,该位置为常量值: 'smartMobilityEvent',表示取消事件监听。 |
|
smartMobilityTypes |
是 |
业务类型数组,支持同时取消多个业务监听。 |
|
|
callback |
Callback<SmartMobilityEvent> |
否 |
出行业务事件回调函数,不传入该参数,表示取消对应业务的所有监听。 |
错误码:
|
错误码ID |
错误信息 |
|---|---|
|
201 |
Permission verify failed. |
|
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. |
示例:
import { smartMobilityCommon } from '@kit.CarKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
// Get single instance of SmartMobilityAwareness.
let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
// 业务类型
let types: smartMobilityCommon.SmartMobilityType[] = [smartMobilityCommon.SmartMobilityType.CAR_HOP];
// 出行业务事件回调函数
const callBack = (event: smartMobilityCommon.SmartMobilityEvent) => {
hilog.info(0x0000, 'Received smart mobility event: ', JSON.stringify(event));
};
// 解注册出行业务事件监听
awareness.off('smartMobilityEvent', types, callBack);
getSmartMobilityEvent
getSmartMobilityEvent(type: SmartMobilityType, eventName: string): SmartMobilityEvent
应用获取指定事件的信息。
系统能力:SystemCapability.CarService.DistributedEngine
权限描述:ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE
起始版本:5.0.0(12)
参数:
|
参数名 |
类型 |
必填 |
说明 |
|---|---|---|---|
|
type |
是 |
业务类型。 |
|
|
eventName |
string |
是 |
事件名。 |
返回值:
|
类型 |
说明 |
|---|---|
|
公共事件信息。 |
错误码:
|
错误码ID |
错误信息 |
|---|---|
|
201 |
Permission verify failed. |
|
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. |
示例:
import { smartMobilityCommon } from '@kit.CarKit';
// Get single instance of SmartMobilityAwareness.
let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
// 业务类型
let type = smartMobilityCommon.SmartMobilityType.CAR_HOP;
// 事件名称
let eventName: string = 'CAR_HOP_EVENT';
// 获取公共事件信息
let event: smartMobilityCommon.SmartMobilityEvent = awareness.getSmartMobilityEvent(type, eventName);
on(type: 'smartMobilityStatus')
on(type: 'smartMobilityStatus', smartMobilityTypes: SmartMobilityType[], callback: Callback<SmartMobilityInfo>): void
注册智慧出行连接状态的监听。
系统能力:SystemCapability.CarService.DistributedEngine
权限描述:ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE
起始版本:5.0.0(12)
参数:
|
参数名 |
类型 |
必填 |
说明 |
|---|---|---|---|
|
type |
string |
是 |
订阅类型,该位置为常量值: 'smartMobilityStatus',表示订阅业务连接状态监听。 |
|
smartMobilityTypes |
是 |
业务类型数组,支持同时订阅多个业务。 |
|
|
callback |
Callback<SmartMobilityInfo> |
是 |
出行连接状态回调函数。 |
错误码:
|
错误码ID |
错误信息 |
|---|---|
|
201 |
Permission verify failed. |
|
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. |
示例:
import { smartMobilityCommon } from '@kit.CarKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
// Get single instance of SmartMobilityAwareness.
let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
// 业务类型
let types: smartMobilityCommon.SmartMobilityType[] = [smartMobilityCommon.SmartMobilityType.HICAR];
// 出行连接状态回调函数
const callBack = (info: smartMobilityCommon.SmartMobilityInfo) => {
hilog.info(0x0000, 'Received smart mobility info: ', JSON.stringify(info));
};
// 注册智慧出行连接状态的监听
awareness.on('smartMobilityStatus', types, callBack);
off(type: 'smartMobilityStatus')
off(type: 'smartMobilityStatus', smartMobilityTypes: SmartMobilityType[], callback?: Callback<SmartMobilityInfo>): void
取消注册智慧出行连接状态的监听。
系统能力:SystemCapability.CarService.DistributedEngine
权限描述:ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE
起始版本:5.0.0(12)
参数:
|
参数名 |
类型 |
必填 |
说明 |
|---|---|---|---|
|
type |
string |
是 |
订阅类型,该位置为常量值: 'smartMobilityStatus',表示取消业务连接状态监听。 |
|
smartMobilityTypes |
是 |
业务类型数组,支持同时取消多个业务监听。 |
|
|
callback |
Callback<SmartMobilityInfo> |
否 |
出行业务状态回调函数,不传入该参数,表示取消对应业务的所有监听。 |
错误码:
|
错误码ID |
错误信息 |
|---|---|
|
201 |
Permission verify failed. |
|
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. |
示例:
import { smartMobilityCommon } from '@kit.CarKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
// Get single instance of SmartMobilityAwareness.
let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
// 业务类型
let types: smartMobilityCommon.SmartMobilityType[] = [smartMobilityCommon.SmartMobilityType.HICAR];
// 出行连接状态回调函数
const callBack = (info: smartMobilityCommon.SmartMobilityInfo) => {
hilog.info(0x0000, 'Received smart mobility info: ', JSON.stringify(info));
};
// 取消注册智慧出行连接状态的监听
awareness.off('smartMobilityStatus', types);
getSmartMobilityStatus
getSmartMobilityStatus(type: SmartMobilityType): SmartMobilityInfo
获取智慧出行连接状态。
系统能力:SystemCapability.CarService.DistributedEngine
权限描述:ohos.permission.ACCESS_CAR_DISTRIBUTED_ENGINE
起始版本:5.0.0(12)
参数:
|
参数名 |
类型 |
必填 |
说明 |
|---|---|---|---|
|
type |
是 |
业务类型。 |
返回值:
|
类型 |
说明 |
|---|---|
|
业务状态信息。 |
错误码:
|
错误码ID |
错误信息 |
|---|---|
|
201 |
Permission verify failed. |
|
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. |
示例:
import { smartMobilityCommon } from '@kit.CarKit';
let awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
// 业务类型
let type = smartMobilityCommon.SmartMobilityType.HICAR;
// 获取业务状态信息
let ret = awareness.getSmartMobilityStatus(type);
更多推荐



所有评论(0)