目录

Car Awareness Development

Overview

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 Development

[h2]When to Use

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.

[h2]Available APIs

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.

[h2]Required Permission

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.
    },
  }
]

[h2]Parameter Description

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

[h2]Constraints

[h2]How to Develop

  1. Import the required modules.

    import { carAwareness } from '@kit.MultimodalAwarenessKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
  2. 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;
      }
    }
  3. 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);
      }
    }
  4. 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 Development

[h2]When to Use

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.

[h2]Available APIs

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.

[h2]Required Permission

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"
  }
]

[h2]Parameter Description

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

[h2]Constraints

[h2]How to Develop

  1. Import the required modules.

    import { carAwareness } from '@kit.MultimodalAwarenessKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
  2. 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);
      }
    }
  3. 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 Development

[h2]When to Use

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.

[h2]Available APIs

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.

[h2]Required Permission

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"
  }
]

[h2]Parameter Description

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

[h2]Constraints

[h2]How to Develop

  1. Import the required modules.

    import { carAwareness } from '@kit.MultimodalAwarenessKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
  2. 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);
      }
    }
  3. 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);
      }
    }