• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Vibrator
2
3> **NOTE**
4>
5> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
6
7
8## Modules to Import
9
10```js
11import vibrator from '@ohos.vibrator';
12```
13
14
15## vibrator.vibrate
16
17vibrate(duration: number): Promise<void>
18
19Triggers vibration with a specific duration. This API uses a promise to return the execution result.
20
21**Required permissions**: ohos.permission.VIBRATE
22
23**System capability**: SystemCapability.Sensors.MiscDevice
24
25
26**Parameters**
27| Name    | Type   | Mandatory | Description         |
28| -------- | ------ | ---- | ------------ |
29| duration | number | Yes  | Vibration duration. |
30
31**Return value**
32| Type                | Description        |
33| ------------------- | ----------- |
34| Promise<void> | Promise used to indicate whether the vibration is triggered successfully. |
35
36
37**Example**
38  ```js
39  vibrator.vibrate(1000).then(()=>{
40      console.log("Promise returned to indicate a successful vibration.");
41  }, (error)=>{
42      console.log("error.code"+error.code+"error.message"+error.message);
43  });
44  ```
45
46
47## vibrator.vibrate
48
49vibrate(duration: number, callback?: AsyncCallback<void>): void
50
51Triggers vibration with a specific duration. This API uses an asynchronous callback to return the execution result.
52
53**Required permissions**: ohos.permission.VIBRATE
54
55**System capability**: SystemCapability.Sensors.MiscDevice
56
57**Parameters**
58| Name    | Type                      | Mandatory | Description                    |
59| -------- | ------------------------- | ---- | ----------------------- |
60| duration | number                   | Yes  | Vibration duration.          |
61| callback | AsyncCallback<void> | No  | Callback used to indicate whether the vibration is triggered successfully. |
62
63**Example**
64  ```js
65  vibrator.vibrate(1000,function(error){
66      if(error){
67          console.log("error.code"+error.code+"error.message"+error.message);
68      }else{
69          console.log("Callback returned to indicate a successful vibration.");
70      }
71  })
72  ```
73
74
75## vibrator.vibrate
76
77vibrate(effectId: EffectId): Promise<void>
78
79Triggers vibration with a specific effect. This API uses a promise to return the execution result.
80
81**Required permissions**: ohos.permission.VIBRATE
82
83**System capability**: SystemCapability.Sensors.MiscDevice
84
85**Parameters**
86| Name    | Type                  | Mandatory | Description          |
87| -------- | --------------------- | ---- | ------------- |
88| effectId | [EffectId](#effectid) | Yes  | Vibration effect. |
89
90**Return value**
91| Type                | Description        |
92| ------------------- | ----------- |
93| Promise<void> | Promise used to indicate whether the vibration is triggered successfully. |
94
95**Example**
96  ```js
97  vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(()=>{
98      console.log("Promise returned to indicate a successful vibration.");
99  }, (error)=>{
100      console.log("error.code"+error.code+"error.message"+error.message);
101  });
102  ```
103
104
105## vibrator.vibrate
106
107vibrate(effectId: EffectId, callback?: AsyncCallback<void>): void
108
109Triggers vibration with a specific effect. This API uses an asynchronous callback to return the execution result.
110
111**Required permissions**: ohos.permission.VIBRATE
112
113**System capability**: SystemCapability.Sensors.MiscDevice
114
115**Parameters**
116| Name    | Type                      | Mandatory | Description                    |
117| -------- | ------------------------- | ---- | ----------------------- |
118| effectId | [EffectId](#effectid)    | Yes  | Vibration effect.         |
119| callback | AsyncCallback<void> | No  | Callback used to indicate whether the vibration is triggered successfully. |
120
121**Example**
122  ```js
123  vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function(error){
124      if(error){
125          console.log("error.code"+error.code+"error.message"+error.message);
126      }else{
127          console.log("Callback returned to indicate a successful vibration.");
128      }
129  })
130  ```
131
132
133## vibrator.stop
134
135stop(stopMode: VibratorStopMode): Promise<void>
136
137Stops the vibration based on the specified **stopMode**. This API uses a promise to return the execution result. If the specified **stopMode** is different from the mode used to trigger the vibration, this API fails to be called.
138
139**Required permissions**: ohos.permission.VIBRATE
140
141**System capability**: SystemCapability.Sensors.MiscDevice
142
143**Parameters**
144| Name     | Type                                  | Mandatory | Description |
145| -------- | ------------------------------------- | ---- | --------------- |
146| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes  | Vibration mode to stop. |
147
148**Return value**
149| Type                | Description |
150| ------------------- | ----------- |
151| Promise<void> | Promise used to indicate whether the vibration is stopped successfully. |
152
153**Example**
154  ```js
155  vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(()=>{
156      console.log("Promise returned to indicate a successful vibration.");
157  }, (error)=>{
158      console.log("error.code"+error.code+"error.message"+error.message);
159  });
160  ```
161
162
163## vibrator.stop
164
165stop(stopMode: VibratorStopMode, callback?: AsyncCallback<void>): void;
166
167Stops the vibration based on the specified **stopMode**. This API uses an asynchronous callback to return the execution result. If the specified **stopMode** is different from the mode used to trigger the vibration, this API fails to be called.
168
169**Required permissions**: ohos.permission.VIBRATE
170
171**System capability**: SystemCapability.Sensors.MiscDevice
172
173**Parameters**
174| Name | Type | Mandatory | Description |
175| -------- | ------------------------------------- | ---- | ----------------------- |
176| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes | Vibration mode to stop. |
177| callback | AsyncCallback<void> | No | Callback used to indicate whether the vibration is stopped successfully. |
178
179**Example**
180  ```js
181  vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, function(error){
182      if(error){
183          console.log("error.code"+error.code+"error.message"+error.message);
184      }else{
185          console.log("Callback returned to indicate a successful stop.");
186      }
187  })
188  ```
189
190
191## EffectId
192
193Describes the vibration effect.
194
195**System capability**: SystemCapability.Sensors.MiscDevice
196
197| Name               | Default Value        | Description                                                     |
198| ------------------ | -------------------- | --------------------------------------------------------------- |
199| EFFECT_CLOCK_TIMER | "haptic.clock.timer" | Vibration effect of the vibrator when a user adjusts the timer. |
200
201
202## VibratorStopMode
203
204Describes the vibration mode to stop.
205
206**System capability**: SystemCapability.Sensors.MiscDevice
207
208| Name | Default Value | Description |
209| ------------------------- | -------- | ---------------------------------------- |
210| VIBRATOR_STOP_MODE_TIME  | "time"  | The vibration to stop is in **duration** mode. This vibration is triggered with the parameter **duration** of the **number** type. |
211| VIBRATOR_STOP_MODE_PRESET | "preset" | The vibration to stop is in **EffectId** mode. This vibration is triggered with the parameter **effectId** of the **EffectId** type. |
212