harmony 鸿蒙Vibrator Development

  • 2025-06-06
  • 浏览 (4)

Vibrator Development

When to Use

You can set different vibration effects for your device, for example, you can set vibration effects with different intensities and durations for buttons on the device.

For details about the APIs, see Vibrator.

Available APIs

Name Description
startVibration(effect: VibrateTime, attribute: VibrateAttribute): Promise<void> Starts vibration with the specified effect and attribute. This API uses a promise to return the result.
startVibration(effect: VibrateTime, attribute: VibrateAttribute, callback: AsyncCallback<void>): void Starts vibration with the specified effect and attribute. This API uses an asynchronous callback to return the result.
stopVibration(): Promise<void> Stops vibration in all modes. This API uses a promise to return the result.
stopVibration(callback: AsyncCallback<void>): void Stops vibration in all modes. This API uses an asynchronous callback to return the result.

How to Develop

  1. Before using the vibrator on a device, you must declare the ohos.permission.VIBRATE permission. For details, see Declaring Permissions.

  2. Configure the vibrator to vibrate with the specified duration and attribute.

    import { vibrator } from '@kit.SensorServiceKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    
    
    try {
      // Start vibration.
      vibrator.startVibration({
        type: 'time',
        duration: 1000,
      }, {
        id: 0,
        usage: 'alarm'
      }, (error: BusinessError) => {
        if (error) {
          console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
          return;
        }
        console.info('Succeed in starting vibration');
      });
    } catch (err) {
      let e: BusinessError = err as BusinessError;
      console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
    }
    
  3. Stop vibration.

    import { vibrator } from '@kit.SensorServiceKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    
    
    try {
      // Stop vibration in all modes.
      vibrator.stopVibration((error: BusinessError) => {
        if (error) {
          console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
          return;
        }
        console.info('Succeed in stopping vibration');
      })
    } catch (error) {
      let e: BusinessError = error as BusinessError;
      console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
    }
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Sensor Service Kit

harmony 鸿蒙Sensor Development

harmony 鸿蒙Sensor Development (C/C++)

harmony 鸿蒙Sensor Development (ArkTS)

harmony 鸿蒙Sensor Overview

harmony 鸿蒙Sensor Overview

harmony 鸿蒙Introduction to Sensor Service Kit

harmony 鸿蒙Vibrator Development (C/C++)

harmony 鸿蒙Vibrator Development (ArkTS)

harmony 鸿蒙Vibrator Overview

0  赞