• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Vibrator Development
2
3
4## When to Use
5
6You can set different vibration effects as needed, for example, customizing the vibration intensity, frequency, and duration for button touches, alarm clocks, and incoming calls.
7
8For details about the APIs, see [Vibrator](../reference/apis/js-apis-vibrator.md).
9
10
11## Available APIs
12
13| Module         | API                                                      | Description                                                        |
14| ------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
15| 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.|
16| 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.|
17| ohos.vibrator | stopVibration(stopMode: VibratorStopMode): Promise<void> | Stops vibration in the specified mode. This API uses a promise to return the result.                                |
18| 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.                                |
19
20
21## How to Develop
22
231. 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).
24
252. Start vibration with the specified effect and attribute.
26
27   ```js
28   import vibrator from '@ohos.vibrator';
29   try {
30       vibrator.startVibration({
31           type: 'time',
32           duration: 1000,
33       }, {
34           id: 0,
35           usage: 'alarm'
36       }, (error) => {
37           if (error) {
38               console.error('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
39               return;
40           }
41           console.log('Callback returned to indicate a successful vibration.');
42       });
43   } catch (err) {
44       console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
45   }
46   ```
47
483. Stop vibration in the specified mode.
49
50   ```js
51   import vibrator from '@ohos.vibrator';
52   try {
53     // Stop vibration in VIBRATOR_STOP_MODE_TIME mode.
54     vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, function (error) {
55         if (error) {
56             console.log('error.code' + error.code + 'error.message' + error.message);
57             return;
58         }
59         console.log('Callback returned to indicate successful.');
60     })
61   } catch (err) {
62     console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
63   }
64   ```
65