The carAwareness (vehicle awareness) module provides camera-based environment and motion awareness capabilities for in-vehicle apps.
For detailed API description, refer to @ohos.multimodalAwareness.carAwareness (Vehicle Awareness).
Air gesture awareness obtains the coordinates and motion events of the user's hand on the screen. It applies to scenarios such as contactless control of the in-vehicle center console and gesture interaction in interactive games.
| API | Description |
|---|---|
| onSpatialMotion(callback: Callback<SpatialMotionInfo>): void; | Enables air gesture awareness and subscribes to air gesture recognition results. |
| offSpatialMotion(callback?: Callback<SpatialMotionInfo>): void; | Disables air gesture awareness and unsubscribes from air gesture recognition results. |
| getAllCapabilityList(): Promise<Capability[]>; | Obtains the list of all vehicle awareness capabilities supported by the current device. |
To use air gesture awareness, the following permission is required: ohos.permission.vehicle.MMA_SPATIALACTION.
For details about how to request the permission, see Declaring Permissions.
"requestPermissions": [
{
"name": "ohos.permission.vehicle.MMA_SPATIALACTION",
// Fill in reason and usedScene as needed.
"reason" : "", // Used for app release verification.
"usedScene" : {
"abilities" : [
"" // Name of the permission to use.
],
"when" : "" // Time to call.
},
}
] SpatialMotionInfo
| Name | Type | Description |
|---|---|---|
| timestamp | number | Timestamp of the recognition result. Unit: Milliseconds |
| pointX | number | X-axis coordinate of the hand on the screen. |
| pointY | number | Y-axis coordinate of the hand on the screen. |
| event | number | Gesture event type: -1: Invalid 0: Ready 1: Move 2: Tap |
Import the required modules.
import { carAwareness } from '@kit.MultimodalAwarenessKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit'; Check whether the device supports the air gesture capability.
async function querySpatialMotionCapability(): Promise<boolean> {
try {
const capabilityList = await carAwareness.getAllCapabilityList();
const isSupported = capabilityList.includes(carAwareness.Capability.SPATIAL_MOTION);
hilog.info(DOMAIN, TAG, 'Spatial motion supported: %{public}s', isSupported);
return isSupported;
} catch (error) {
const err: BusinessError = error as BusinessError;
hilog.error(DOMAIN, TAG, 'Failed to get capability list, error code: %{public}d', err.code);
return false;
}
} Register the gesture awareness callback to process the recognition result.
function subscribeSpatialMotion(onUpdate: (data: string) => void): void {
try {
carAwareness.onSpatialMotion((motionInfo: carAwareness.SpatialMotionInfo) => {
const eventType: string = getSpatialMotionEventType(motionInfo.event);
const dataStr: string = `Event: ${eventType}, Position: (${motionInfo.pointX}, ${motionInfo.pointY})`;
hilog.info(DOMAIN, TAG, 'Spatial motion event: %{public}d', motionInfo.event);
hilog.info(DOMAIN, TAG, 'Hand coordinate: (%{public}d, %{public}d)', motionInfo.pointX, motionInfo.pointY);
onUpdate(dataStr);
});
hilog.info(DOMAIN, TAG, 'Succeeded in subscribing spatial motion.');
} catch (error) {
const err: BusinessError = error as BusinessError;
hilog.error(DOMAIN, TAG, 'Failed to subscribe spatial motion, error code: %{public}d', err.code);
}
} Unsubscribe from gesture awareness.
function unsubscribeSpatialMotion(): void {
try {
carAwareness.offSpatialMotion();
hilog.info(DOMAIN, TAG, 'Succeeded in unsubscribing spatial motion.');
} catch (error) {
const err: BusinessError = error as BusinessError;
hilog.error(DOMAIN, TAG, 'Failed to unsubscribe spatial motion, error code: %{public}d', err.code);
}
} Real-time weather awareness obtains the weather conditions of the vehicle's current environment. It applies to scenarios such as adjusting in-vehicle service recommendations based on the weather and automatically enabling wipers or defogging.
| Name | Description |
|---|---|
| onRealTimeWeather(callback: Callback<RealTimeWeatherInfo>): void; | Enables real-time weather awareness and subscribes to real-time weather recognition results. |
| offRealTimeWeather(callback?: Callback<RealTimeWeatherInfo>): void; | Disables real-time weather awareness and unsubscribes from real-time weather recognition results. |
To use real-time weather awareness, the following permission is required: ohos.permission.vehicle.MMA_WEATHER.
For details about how to request the permission, see Declaring Permissions.
"requestPermissions": [
{
"name": "ohos.permission.vehicle.MMA_WEATHER"
}
] RealTimeWeatherInfo
| Name | Type | Description |
|---|---|---|
| timestamp | number | Timestamp of the recognition result. Unit: Milliseconds. |
| weather | number | Weather status: -1: Invalid 0: Sunny/Cloudy 1: Light mist 2: Dense fog 3: Light snow 4: Heavy snow 5: Light rain 6: Heavy rain |
Import the required modules.
import { carAwareness } from '@kit.MultimodalAwarenessKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit'; Register the weather awareness callback.
function subscribeWeather(onUpdate: (data: string) => void): void {
try {
carAwareness.onRealTimeWeather((weatherInfo: carAwareness.RealTimeWeatherInfo) => {
const weatherType: string = getWeatherType(weatherInfo.weather);
hilog.info(DOMAIN, TAG, 'Weather code: %{public}d', weatherInfo.weather);
onUpdate(weatherType);
});
hilog.info(DOMAIN, TAG, 'Succeeded in subscribing realtime weather.');
} catch (error) {
const err: BusinessError = error as BusinessError;
hilog.error(DOMAIN, TAG, 'Failed to subscribe realtime weather, error code: %{public}d', err.code);
}
} Unsubscribe from weather awareness.
function unsubscribeWeather(): void {
try {
carAwareness.offRealTimeWeather();
hilog.info(DOMAIN, TAG, 'Succeeded in unsubscribing realtime weather.');
} catch (error) {
const err: BusinessError = error as BusinessError;
hilog.error(DOMAIN, TAG, 'Failed to unsubscribe realtime weather, error code: %{public}d', err.code);
}
} Refueling status awareness can identify the start and end states of vehicle refueling. It is suitable for automatically recording the refueling duration, matching and generating refueling orders, and supports atomic service invocation.
| API | Description |
|---|---|
| onRefueling(callback: Callback<RefuelingInfo>): void; | Enables refueling awareness and subscribes to refueling status awareness results. |
| offRefueling(callback?: Callback<RefuelingInfo>): void; | Disables refueling awareness and unsubscribes from refueling status awareness results. |
To use refueling status awareness, the following permission is required: ohos.permission.vehicle.MMA_ENERGYREFILL.
For details about how to request the permission, see Declaring Permissions.
"requestPermissions": [
{
"name": "ohos.permission.vehicle.MMA_ENERGYREFILL"
}
] RefuelingInfo
| Name | Type | Description |
|---|---|---|
| timestamp | number | Timestamp of the recognition result. Unit: Milliseconds. |
| status | number | Refueling status: -1: Invalid 0: Idle 1: Refueling started 2: Refueling ended |
Import the required modules.
import { carAwareness } from '@kit.MultimodalAwarenessKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit'; Register the refueling status callback.
function subscribeRefueling(onUpdate: (data: string) => void): void {
try {
carAwareness.onRefueling((refuelingInfo: carAwareness.RefuelingInfo) => {
const statusType: string = getRefuelingStatusType(refuelingInfo.status);
hilog.info(DOMAIN, TAG, 'Refueling status: %{public}d', refuelingInfo.status);
onUpdate(statusType);
});
hilog.info(DOMAIN, TAG, 'Succeeded in subscribing refueling status.');
} catch (error) {
const err: BusinessError = error as BusinessError;
hilog.error(DOMAIN, TAG, 'Failed to subscribe refueling status, error code: %{public}d', err.code);
}
} Unsubscribe from refueling status.
function unsubscribeRefueling(): void {
try {
carAwareness.offRefueling();
hilog.info(DOMAIN, TAG, 'Succeeded in unsubscribing refueling status.');
} catch (error) {
const err: BusinessError = error as BusinessError;
hilog.error(DOMAIN, TAG, 'Failed to unsubscribe refueling status, error code: %{public}d', err.code);
}
}