目录

Motion Awareness Development

When to Use

An application can call the motion module to identify user actions, for example, whether the user is operating the device screen with the left hand or the right hand.

For detailed API introduction, refer to @ohos.multimodalAwareness.motion (Motion Awareness).

Operating hand status query is supported since API version 15. Holding hand status query is supported since API version 20.

Operating Hand Status Query

[h2]Available APIs

API Description
on(type:'operatingHandChanged',callback:Callback<OperatingHandStatus>):void; Subscribes to operating hand change events. This API uses a callback to return the result.
off(type: 'operatingHandChanged', callback?: Callback<OperatingHandStatus>): void; Unsubscribes from operating hand change events.
getRecentOperatingHandStatus(): OperatingHandStatus; Obtains the latest operating hand status.

[h2]Required Permissions

To use the motion module to obtain the operating hand, you need to request the ohos.permission.ACTIVITY_MOTION or ohos.permission.DETECT_GESTURE permission. For details, see Declaring Permissions.

"requestPermissions":[
    {
      "name" : "ohos.permission.ACTIVITY_MOTION",
      // Fill in reason and usedScene as needed.
      "reason" : "", // Used for app listing verification.
      "usedScene" : {
         "abilities" : [
            "" // Name of the permission to use.
         ],
         "when" : "" // Timing of the call.
      },
    },
    {
      "name" : "ohos.permission.DETECT_GESTURE"
    }
  ]

[h2]Constraints

[h2]How to Develop

  1. Import the related modules.

    import { motion } from '@kit.MultimodalAwarenessKit';
    import { BusinessError } from '@kit.BasicServicesKit';
  2. Define a callback to receive operating hand change results.

    let callback:Callback<motion.OperatingHandStatus> = (data:motion.OperatingHandStatus) => {
      console.info('callback succeeded' + data);
    };
  3. Subscribe to operating hand change results.

    try {
      motion.on('operatingHandChanged', callback);
      console.info('on succeeded');
      // ...
    } catch (err) {
      let error = err as BusinessError;
      console.error('Failed on and err code is ' + error.code);
      // ...
    }
  4. Unsubscribe from operating hand change events.

    try {
      motion.off('operatingHandChanged');
      console.info('off succeeded');
      // ...
    } catch (err) {
      let error = err as BusinessError;
      console.error('Failed off and err code is ' + error.code);
      // ...
    }
  5. Obtain the latest operating hand status.

    try {
      let data:motion.OperatingHandStatus = motion.getRecentOperatingHandStatus();
      console.info('get succeeded' + data);
      // ...
    } catch (err) {
      let error = err as BusinessError;
      console.error('Failed get and err code is ' + error.code);
      // ...
    }

Holding Hand Status Query

[h2]Available APIs

API Description
on(type:'holdingHandChanged',callback:Callback<HoldingHandStatus>): void; Enables listening for holding hand status changes. This API uses a callback to return the result.
off(type: 'holdingHandChanged', callback?: Callback<HoldingHandStatus>): void; Disables listening for holding hand status changes.

[h2]Required Permissions

To use the motion module to obtain the holding hand, you need to request the ohos.permission.DETECT_GESTURE permission. For details, see Declaring Permissions.

"requestPermissions":[
    {
      "name" : "ohos.permission.DETECT_GESTURE"
    }
  ]

[h2]Constraints

[h2]How to Develop

  1. Import the related modules.

    import { motion } from '@kit.MultimodalAwarenessKit';
    import { BusinessError } from '@kit.BasicServicesKit';
  2. Define a callback to receive the hand holding status change.

    let callback:Callback<motion.HoldingHandStatus> = (data:motion.HoldingHandStatus) => {
      console.info('callback succeeded' + data);
    };
  3. Enable listening for holding hand status changes.

    try {
      motion.on('holdingHandChanged', callback);
      console.info('on succeeded');
      // ...
    } catch (err) {
      let error = err as BusinessError;
      console.error('Failed on and err code is ' + error.code);
      // ...
    }
  4. Disable listening for holding hand status changes.

    try {
      motion.off('holdingHandChanged');
      console.info('off succeeded');
      // ...
    } catch (err) {
      let error = err as BusinessError;
      console.error('Failed off and err code is ' + error.code);
      // ...
    }