vibrator-guidelines.md 2.9 KB
Newer Older
G
ge-yafang 已提交
1 2 3 4 5
# Vibrator Development


## When to Use

W
wusongqing 已提交
6
You can set different vibration effects as needed, for example, customizing the vibration intensity, frequency, and duration for button touches, alarm clocks, and incoming calls.
G
ge-yafang 已提交
7

W
wusongqing 已提交
8 9
For details about the APIs, see [Vibrator](../reference/apis/js-apis-vibrator.md).

G
ge-yafang 已提交
10 11 12

## Available APIs

13 14 15 16 17 18
| Module         | API                                                      | Description                                                        |
| ------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| ohos.vibrator | startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise<void> | Starts vibration with the specified effect and attribute. This API uses a promise to return the result.|
| ohos.vibrator | startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: AsyncCallback<void>): void | Starts vibration with the specified effect and attribute. This API uses an asynchronous callback to return the result.|
| ohos.vibrator | stopVibration(stopMode: VibratorStopMode): Promise<void> | Stops vibration in the specified mode. This API uses a promise to return the result.                                |
| ohos.vibrator | stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback<void>): void | Stops vibration in the specified mode. This API uses an asynchronous callback to return the result.                                |
G
ge-yafang 已提交
19 20 21 22


## How to Develop

G
Gloria 已提交
23
1. Before using the vibrator on a device, you must declare the **ohos.permission.VIBRATE** permission. For details about how to configure a permission, see [Declaring Permissions](../security/accesstoken-guidelines.md).
G
ge-yafang 已提交
24

25
2. Start vibration with the specified effect and attribute.
W
wusongqing 已提交
26

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
   ```js
   import vibrator from '@ohos.vibrator';
   try {
       vibrator.startVibration({
           type: 'time',
           duration: 1000,
       }, {
           id: 0,
           usage: 'alarm'
       }, (error) => {
           if (error) {
               console.error('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
               return;
           }
           console.log('Callback returned to indicate a successful vibration.');
       });
   } catch (err) {
       console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
   }
G
ge-yafang 已提交
46 47
   ```

48
3. Stop vibration in the specified mode.
W
wusongqing 已提交
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63
   ```js
   import vibrator from '@ohos.vibrator';
   try {
     // Stop vibration in VIBRATOR_STOP_MODE_TIME mode.
     vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, function (error) {
         if (error) {
             console.log('error.code' + error.code + 'error.message' + error.message);
             return;
         }
         console.log('Callback returned to indicate successful.');
     })
   } catch (err) {
     console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
   }
G
ge-yafang 已提交
64
   ```