1e41f4b71Sopenharmony_ci# @ohos.vibrator (Vibrator)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe **vibrator** module provides APIs for starting or stopping vibration.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **NOTE**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> 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.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci## Modules to Import
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci```ts
13e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
14e41f4b71Sopenharmony_ci```
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci## vibrator.startVibration<sup>9+</sup>
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_cistartVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: AsyncCallback&lt;void&gt;): void
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ciStarts vibration with the specified effect and attribute. This API uses an asynchronous callback to return the result.
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.VIBRATE
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci**Parameters**
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci| Name   | Type                                  | Mandatory| Description                                                        |
31e41f4b71Sopenharmony_ci| --------- | -------------------------------------- | ---- | :----------------------------------------------------------- |
32e41f4b71Sopenharmony_ci| effect    | [VibrateEffect](#vibrateeffect9)       | Yes  | Vibration effect. The options are as follows:<br>- [VibrateTime](#vibratetime9): vibration with the specified duration.<br>- [VibratePreset](#vibratepreset9): vibration with a preset effect.<br>- [VibrateFromFile](#vibratefromfile10): vibration according to a custom vibration configuration file.|
33e41f4b71Sopenharmony_ci| attribute | [VibrateAttribute](#vibrateattribute9) | Yes  | Vibration attribute.                                              |
34e41f4b71Sopenharmony_ci| callback  | AsyncCallback&lt;void&gt;              | Yes  | Callback used to return the result. If the vibration starts, **err** is **undefined**; otherwise, **err** is an error object.  |
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci**Error codes**
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ciFor details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md) and [Universal Error Codes](../errorcode-universal.md).
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
41e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
42e41f4b71Sopenharmony_ci| 201      | Permission denied.                                           |
43e41f4b71Sopenharmony_ci| 401      | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. |
44e41f4b71Sopenharmony_ci| 801      | Capability not supported.                                    |
45e41f4b71Sopenharmony_ci| 14600101 | Device operation failed.                                     |
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci**Example**
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ciTrigger vibration with the specified duration.
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci```ts
52e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
53e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_citry {
56e41f4b71Sopenharmony_ci  vibrator.startVibration({
57e41f4b71Sopenharmony_ci    type: 'time',
58e41f4b71Sopenharmony_ci    duration: 1000,
59e41f4b71Sopenharmony_ci  }, {
60e41f4b71Sopenharmony_ci    id: 0,
61e41f4b71Sopenharmony_ci    usage: 'alarm'
62e41f4b71Sopenharmony_ci  }, (error: BusinessError) => {
63e41f4b71Sopenharmony_ci    if (error) {
64e41f4b71Sopenharmony_ci      console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
65e41f4b71Sopenharmony_ci      return;
66e41f4b71Sopenharmony_ci    }
67e41f4b71Sopenharmony_ci    console.info('Succeed in starting vibration');
68e41f4b71Sopenharmony_ci  });
69e41f4b71Sopenharmony_ci} catch (err) {
70e41f4b71Sopenharmony_ci  let e: BusinessError = err as BusinessError;
71e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
72e41f4b71Sopenharmony_ci}
73e41f4b71Sopenharmony_ci```
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ciTrigger vibration with a preset effect.
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci```ts
78e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
79e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_citry {
82e41f4b71Sopenharmony_ci  vibrator.startVibration({
83e41f4b71Sopenharmony_ci    type: 'preset',
84e41f4b71Sopenharmony_ci    effectId: 'haptic.clock.timer',
85e41f4b71Sopenharmony_ci    count: 1,
86e41f4b71Sopenharmony_ci  }, {
87e41f4b71Sopenharmony_ci    id: 0,
88e41f4b71Sopenharmony_ci    usage: 'alarm'
89e41f4b71Sopenharmony_ci  }, (error: BusinessError) => {
90e41f4b71Sopenharmony_ci    if (error) {
91e41f4b71Sopenharmony_ci      console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
92e41f4b71Sopenharmony_ci      return;
93e41f4b71Sopenharmony_ci    }
94e41f4b71Sopenharmony_ci    console.info('Succeed in starting vibration');
95e41f4b71Sopenharmony_ci  });
96e41f4b71Sopenharmony_ci} catch (err) {
97e41f4b71Sopenharmony_ci  let e: BusinessError = err as BusinessError;
98e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
99e41f4b71Sopenharmony_ci}
100e41f4b71Sopenharmony_ci```
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ciTrigger vibration according to a custom vibration configuration file.
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci```ts
105e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
106e41f4b71Sopenharmony_ciimport { resourceManager } from '@kit.LocalizationKit';
107e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ciconst fileName: string = 'xxx.json';
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_cilet rawFd: resourceManager.RawFileDescriptor = getContext().resourceManager.getRawFdSync(fileName);
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_citry {
114e41f4b71Sopenharmony_ci  vibrator.startVibration({
115e41f4b71Sopenharmony_ci    type: "file",
116e41f4b71Sopenharmony_ci    hapticFd: { fd: rawFd.fd, offset: rawFd.offset, length: rawFd.length }
117e41f4b71Sopenharmony_ci  }, {
118e41f4b71Sopenharmony_ci    id: 0,
119e41f4b71Sopenharmony_ci    usage: 'alarm'
120e41f4b71Sopenharmony_ci  }, (error: BusinessError) => {
121e41f4b71Sopenharmony_ci    if (error) {
122e41f4b71Sopenharmony_ci      console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
123e41f4b71Sopenharmony_ci      return;
124e41f4b71Sopenharmony_ci    }
125e41f4b71Sopenharmony_ci    console.info('Succeed in starting vibration');
126e41f4b71Sopenharmony_ci  });
127e41f4b71Sopenharmony_ci} catch (err) {
128e41f4b71Sopenharmony_ci  let e: BusinessError = err as BusinessError;
129e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
130e41f4b71Sopenharmony_ci}
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_cigetContext().resourceManager.closeRawFdSync(fileName);
133e41f4b71Sopenharmony_ci```
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci## vibrator.startVibration<sup>9+</sup>
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_cistartVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise&lt;void&gt;
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ciStarts vibration with the specified effect and attribute. This API uses a promise to return the result.
140e41f4b71Sopenharmony_ci
141e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.VIBRATE
142e41f4b71Sopenharmony_ci
143e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci**Parameters**
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci| Name   | Type                                  | Mandatory| Description                                                        |
150e41f4b71Sopenharmony_ci| --------- | -------------------------------------- | ---- | ------------------------------------------------------------ |
151e41f4b71Sopenharmony_ci| effect    | [VibrateEffect](#vibrateeffect9)       | Yes  | Vibration effect. The options are as follows:<br>- [VibrateTime](#vibratetime9): vibration with the specified duration.<br>- [VibratePreset](#vibratepreset9): vibration with a preset effect.<br>- [VibrateFromFile](#vibratefromfile10): vibration according to a custom vibration configuration file.|
152e41f4b71Sopenharmony_ci| attribute | [VibrateAttribute](#vibrateattribute9) | Yes  | Vibration attribute.                                              |
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ci**Return value**
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci| Type               | Description                                  |
157e41f4b71Sopenharmony_ci| ------------------- | -------------------------------------- |
158e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | Promise that returns no value.|
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci**Error codes**
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_ciFor details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md) and [Universal Error Codes](../errorcode-universal.md).
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
165e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
166e41f4b71Sopenharmony_ci| 201      | Permission denied.                                           |
167e41f4b71Sopenharmony_ci| 401      | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. |
168e41f4b71Sopenharmony_ci| 801      | Capability not supported.                                    |
169e41f4b71Sopenharmony_ci| 14600101 | Device operation failed.                                     |
170e41f4b71Sopenharmony_ci
171e41f4b71Sopenharmony_ci**Example**
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_ciTrigger vibration with the specified duration.
174e41f4b71Sopenharmony_ci
175e41f4b71Sopenharmony_ci```ts
176e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
177e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
178e41f4b71Sopenharmony_ci
179e41f4b71Sopenharmony_citry {
180e41f4b71Sopenharmony_ci  vibrator.startVibration({
181e41f4b71Sopenharmony_ci    type: 'time',
182e41f4b71Sopenharmony_ci    duration: 1000
183e41f4b71Sopenharmony_ci  }, {
184e41f4b71Sopenharmony_ci    id: 0,
185e41f4b71Sopenharmony_ci    usage: 'alarm'
186e41f4b71Sopenharmony_ci  }).then(() => {
187e41f4b71Sopenharmony_ci    console.info('Succeed in starting vibration');
188e41f4b71Sopenharmony_ci  }, (error: BusinessError) => {
189e41f4b71Sopenharmony_ci    console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
190e41f4b71Sopenharmony_ci  });
191e41f4b71Sopenharmony_ci} catch (err) {
192e41f4b71Sopenharmony_ci  let e: BusinessError = err as BusinessError;
193e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
194e41f4b71Sopenharmony_ci}
195e41f4b71Sopenharmony_ci```
196e41f4b71Sopenharmony_ci
197e41f4b71Sopenharmony_ciTrigger vibration with a preset effect.
198e41f4b71Sopenharmony_ci
199e41f4b71Sopenharmony_ci```ts
200e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
201e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
202e41f4b71Sopenharmony_ci
203e41f4b71Sopenharmony_citry {
204e41f4b71Sopenharmony_ci  vibrator.startVibration({
205e41f4b71Sopenharmony_ci    type: 'preset',
206e41f4b71Sopenharmony_ci    effectId: 'haptic.clock.timer',
207e41f4b71Sopenharmony_ci    count: 1,
208e41f4b71Sopenharmony_ci  }, {
209e41f4b71Sopenharmony_ci    id: 0,
210e41f4b71Sopenharmony_ci    usage: 'alarm'
211e41f4b71Sopenharmony_ci  }).then(() => {
212e41f4b71Sopenharmony_ci    console.info('Succeed in starting vibration');
213e41f4b71Sopenharmony_ci  }, (error: BusinessError) => {
214e41f4b71Sopenharmony_ci    console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
215e41f4b71Sopenharmony_ci  });
216e41f4b71Sopenharmony_ci} catch (err) {
217e41f4b71Sopenharmony_ci  let e: BusinessError = err as BusinessError;
218e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
219e41f4b71Sopenharmony_ci}
220e41f4b71Sopenharmony_ci```
221e41f4b71Sopenharmony_ci
222e41f4b71Sopenharmony_ciTrigger vibration according to a custom vibration configuration file.
223e41f4b71Sopenharmony_ci
224e41f4b71Sopenharmony_ci```ts
225e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
226e41f4b71Sopenharmony_ciimport { resourceManager } from '@kit.LocalizationKit';
227e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
228e41f4b71Sopenharmony_ci
229e41f4b71Sopenharmony_ciconst fileName: string = 'xxx.json';
230e41f4b71Sopenharmony_ci
231e41f4b71Sopenharmony_cilet rawFd: resourceManager.RawFileDescriptor = getContext().resourceManager.getRawFdSync(fileName);
232e41f4b71Sopenharmony_ci
233e41f4b71Sopenharmony_citry {
234e41f4b71Sopenharmony_ci  vibrator.startVibration({
235e41f4b71Sopenharmony_ci    type: "file",
236e41f4b71Sopenharmony_ci    hapticFd: { fd: rawFd.fd, offset: rawFd.offset, length: rawFd.length }
237e41f4b71Sopenharmony_ci  }, {
238e41f4b71Sopenharmony_ci    id: 0,
239e41f4b71Sopenharmony_ci    usage: 'alarm'
240e41f4b71Sopenharmony_ci  }).then(() => {
241e41f4b71Sopenharmony_ci    console.info('Succeed in starting vibration');
242e41f4b71Sopenharmony_ci  }, (error: BusinessError) => {
243e41f4b71Sopenharmony_ci    console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
244e41f4b71Sopenharmony_ci  });
245e41f4b71Sopenharmony_ci} catch (err) {
246e41f4b71Sopenharmony_ci  let e: BusinessError = err as BusinessError;
247e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
248e41f4b71Sopenharmony_ci}
249e41f4b71Sopenharmony_ci
250e41f4b71Sopenharmony_cigetContext().resourceManager.closeRawFdSync(fileName);
251e41f4b71Sopenharmony_ci```
252e41f4b71Sopenharmony_ci
253e41f4b71Sopenharmony_ci## vibrator.stopVibration<sup>9+</sup>
254e41f4b71Sopenharmony_ci
255e41f4b71Sopenharmony_cistopVibration(stopMode: VibratorStopMode, callback: AsyncCallback&lt;void&gt;): void
256e41f4b71Sopenharmony_ci
257e41f4b71Sopenharmony_ciStops vibration in the specified mode. This API uses an asynchronous callback to return the result.
258e41f4b71Sopenharmony_ci
259e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.VIBRATE
260e41f4b71Sopenharmony_ci
261e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
262e41f4b71Sopenharmony_ci
263e41f4b71Sopenharmony_ci**Parameters**
264e41f4b71Sopenharmony_ci
265e41f4b71Sopenharmony_ci| Name  | Type                                 | Mandatory| Description                                                        |
266e41f4b71Sopenharmony_ci| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ |
267e41f4b71Sopenharmony_ci| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes  | Mode to stop the vibration. The options are as follows:<br>- **VIBRATOR_STOP_MODE_TIME**: used to stop fixed-duration vibration.<br>- **VIBRATOR_STOP_MODE_PRESET**: used to stop preset vibration.<br>To stop custom vibration, use [vibrator.stopVibration<sup>10+</sup>](#vibratorstopvibration10).                                 |
268e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;void&gt;             | Yes  | Callback used to return the result. If the vibration stops, **err** is **undefined**; otherwise, **err** is an error object.|
269e41f4b71Sopenharmony_ci
270e41f4b71Sopenharmony_ci**Error codes**
271e41f4b71Sopenharmony_ci
272e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
273e41f4b71Sopenharmony_ci
274e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
275e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
276e41f4b71Sopenharmony_ci| 201      | Permission denied.                                           |
277e41f4b71Sopenharmony_ci| 401      | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. |
278e41f4b71Sopenharmony_ci
279e41f4b71Sopenharmony_ci**Example**
280e41f4b71Sopenharmony_ci
281e41f4b71Sopenharmony_ciStop fixed-duration vibration.
282e41f4b71Sopenharmony_ci
283e41f4b71Sopenharmony_ci```ts
284e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
285e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
286e41f4b71Sopenharmony_ci
287e41f4b71Sopenharmony_citry {
288e41f4b71Sopenharmony_ci  // Start vibration at a fixed duration.
289e41f4b71Sopenharmony_ci  vibrator.startVibration({
290e41f4b71Sopenharmony_ci    type: 'time',
291e41f4b71Sopenharmony_ci    duration: 1000,
292e41f4b71Sopenharmony_ci  }, {
293e41f4b71Sopenharmony_ci    id: 0,
294e41f4b71Sopenharmony_ci    usage: 'alarm'
295e41f4b71Sopenharmony_ci  }, (error: BusinessError) => {
296e41f4b71Sopenharmony_ci    if (error) {
297e41f4b71Sopenharmony_ci      console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
298e41f4b71Sopenharmony_ci      return;
299e41f4b71Sopenharmony_ci    }
300e41f4b71Sopenharmony_ci    console.info('Succeed in starting vibration');
301e41f4b71Sopenharmony_ci  });
302e41f4b71Sopenharmony_ci} catch (err) {
303e41f4b71Sopenharmony_ci  let e: BusinessError = err as BusinessError;
304e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
305e41f4b71Sopenharmony_ci}
306e41f4b71Sopenharmony_ci
307e41f4b71Sopenharmony_citry {
308e41f4b71Sopenharmony_ci  // Stop vibration in VIBRATOR_STOP_MODE_TIME mode.
309e41f4b71Sopenharmony_ci  vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, (error: BusinessError) => {
310e41f4b71Sopenharmony_ci    if (error) {
311e41f4b71Sopenharmony_ci      console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
312e41f4b71Sopenharmony_ci      return;
313e41f4b71Sopenharmony_ci    }
314e41f4b71Sopenharmony_ci    console.info('Succeed in stopping vibration');
315e41f4b71Sopenharmony_ci  })
316e41f4b71Sopenharmony_ci} catch (err) {
317e41f4b71Sopenharmony_ci  let e: BusinessError = err as BusinessError;
318e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
319e41f4b71Sopenharmony_ci}
320e41f4b71Sopenharmony_ci```
321e41f4b71Sopenharmony_ci
322e41f4b71Sopenharmony_ciStop preset vibration.
323e41f4b71Sopenharmony_ci
324e41f4b71Sopenharmony_ci```ts
325e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
326e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
327e41f4b71Sopenharmony_ci
328e41f4b71Sopenharmony_citry {
329e41f4b71Sopenharmony_ci  // Start vibration with a preset effect.
330e41f4b71Sopenharmony_ci  vibrator.startVibration({
331e41f4b71Sopenharmony_ci    type: 'preset',
332e41f4b71Sopenharmony_ci    effectId: 'haptic.clock.timer',
333e41f4b71Sopenharmony_ci    count: 1,
334e41f4b71Sopenharmony_ci  }, {
335e41f4b71Sopenharmony_ci    id: 0,
336e41f4b71Sopenharmony_ci    usage: 'alarm'
337e41f4b71Sopenharmony_ci  }, (error: BusinessError) => {
338e41f4b71Sopenharmony_ci    if (error) {
339e41f4b71Sopenharmony_ci      console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
340e41f4b71Sopenharmony_ci      return;
341e41f4b71Sopenharmony_ci    }
342e41f4b71Sopenharmony_ci    console.info('Succeed in starting vibration');
343e41f4b71Sopenharmony_ci  });
344e41f4b71Sopenharmony_ci} catch (err) {
345e41f4b71Sopenharmony_ci  let e: BusinessError = err as BusinessError;
346e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
347e41f4b71Sopenharmony_ci}
348e41f4b71Sopenharmony_ci
349e41f4b71Sopenharmony_citry {
350e41f4b71Sopenharmony_ci  // Stop vibration in VIBRATOR_STOP_MODE_PRESET mode.
351e41f4b71Sopenharmony_ci  vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, (error: BusinessError) => {
352e41f4b71Sopenharmony_ci    if (error) {
353e41f4b71Sopenharmony_ci      console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
354e41f4b71Sopenharmony_ci      return;
355e41f4b71Sopenharmony_ci    }
356e41f4b71Sopenharmony_ci    console.info('Succeed in stopping vibration');
357e41f4b71Sopenharmony_ci  })
358e41f4b71Sopenharmony_ci} catch (err) {
359e41f4b71Sopenharmony_ci  let e: BusinessError = err as BusinessError;
360e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
361e41f4b71Sopenharmony_ci}
362e41f4b71Sopenharmony_ci```
363e41f4b71Sopenharmony_ci
364e41f4b71Sopenharmony_ci## vibrator.stopVibration<sup>9+</sup>
365e41f4b71Sopenharmony_ci
366e41f4b71Sopenharmony_cistopVibration(stopMode: VibratorStopMode): Promise&lt;void&gt;
367e41f4b71Sopenharmony_ci
368e41f4b71Sopenharmony_ciStops vibration in the specified mode. This API uses a promise to return the result.
369e41f4b71Sopenharmony_ci
370e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.VIBRATE
371e41f4b71Sopenharmony_ci
372e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
373e41f4b71Sopenharmony_ci
374e41f4b71Sopenharmony_ci**Parameters**
375e41f4b71Sopenharmony_ci
376e41f4b71Sopenharmony_ci| Name  | Type                                 | Mandatory| Description                    |
377e41f4b71Sopenharmony_ci| -------- | ------------------------------------- | ---- | ------------------------ |
378e41f4b71Sopenharmony_ci| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes  | Mode to stop the vibration. The options are as follows:<br>- **VIBRATOR_STOP_MODE_TIME**: used to stop fixed-duration vibration.<br>- **VIBRATOR_STOP_MODE_PRESET**: used to stop preset vibration.<br>To stop custom vibration, use [vibrator.stopVibration<sup>10+</sup>](#vibratorstopvibration10-1).|
379e41f4b71Sopenharmony_ci
380e41f4b71Sopenharmony_ci**Return value**
381e41f4b71Sopenharmony_ci
382e41f4b71Sopenharmony_ci| Type               | Description                                  |
383e41f4b71Sopenharmony_ci| ------------------- | -------------------------------------- |
384e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | Promise that returns no value.|
385e41f4b71Sopenharmony_ci
386e41f4b71Sopenharmony_ci**Error codes**
387e41f4b71Sopenharmony_ci
388e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
389e41f4b71Sopenharmony_ci
390e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
391e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
392e41f4b71Sopenharmony_ci| 201      | Permission denied.                                           |
393e41f4b71Sopenharmony_ci| 401      | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. |
394e41f4b71Sopenharmony_ci
395e41f4b71Sopenharmony_ci**Example**
396e41f4b71Sopenharmony_ci
397e41f4b71Sopenharmony_ciStop fixed-duration vibration.
398e41f4b71Sopenharmony_ci
399e41f4b71Sopenharmony_ci```ts
400e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
401e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
402e41f4b71Sopenharmony_ci
403e41f4b71Sopenharmony_citry {
404e41f4b71Sopenharmony_ci  // Start vibration at a fixed duration.
405e41f4b71Sopenharmony_ci  vibrator.startVibration({
406e41f4b71Sopenharmony_ci    type: 'time',
407e41f4b71Sopenharmony_ci    duration: 1000,
408e41f4b71Sopenharmony_ci  }, {
409e41f4b71Sopenharmony_ci    id: 0,
410e41f4b71Sopenharmony_ci    usage: 'alarm'
411e41f4b71Sopenharmony_ci  }).then(() => {
412e41f4b71Sopenharmony_ci    console.info('Succeed in starting vibration');
413e41f4b71Sopenharmony_ci  }, (error: BusinessError) => {
414e41f4b71Sopenharmony_ci    console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
415e41f4b71Sopenharmony_ci  });
416e41f4b71Sopenharmony_ci} catch (err) {
417e41f4b71Sopenharmony_ci  let e: BusinessError = err as BusinessError;
418e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
419e41f4b71Sopenharmony_ci}
420e41f4b71Sopenharmony_ci
421e41f4b71Sopenharmony_citry {
422e41f4b71Sopenharmony_ci  // Stop vibration in VIBRATOR_STOP_MODE_TIME mode.
423e41f4b71Sopenharmony_ci  vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME).then(() => {
424e41f4b71Sopenharmony_ci    console.info('Succeed in stopping vibration');
425e41f4b71Sopenharmony_ci  }, (error: BusinessError) => {
426e41f4b71Sopenharmony_ci    console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
427e41f4b71Sopenharmony_ci  });
428e41f4b71Sopenharmony_ci} catch (err) {
429e41f4b71Sopenharmony_ci  let e: BusinessError = err as BusinessError;
430e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
431e41f4b71Sopenharmony_ci}
432e41f4b71Sopenharmony_ci```
433e41f4b71Sopenharmony_ci
434e41f4b71Sopenharmony_ciStop preset vibration.
435e41f4b71Sopenharmony_ci
436e41f4b71Sopenharmony_ci```ts
437e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
438e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
439e41f4b71Sopenharmony_ci
440e41f4b71Sopenharmony_citry {
441e41f4b71Sopenharmony_ci  // Start vibration with a preset effect.
442e41f4b71Sopenharmony_ci  vibrator.startVibration({
443e41f4b71Sopenharmony_ci    type: 'preset',
444e41f4b71Sopenharmony_ci    effectId: 'haptic.clock.timer',
445e41f4b71Sopenharmony_ci    count: 1,
446e41f4b71Sopenharmony_ci  }, {
447e41f4b71Sopenharmony_ci    id: 0,
448e41f4b71Sopenharmony_ci    usage: 'alarm'
449e41f4b71Sopenharmony_ci  }).then(() => {
450e41f4b71Sopenharmony_ci    console.info('Succeed in starting vibration');
451e41f4b71Sopenharmony_ci  }, (error: BusinessError) => {
452e41f4b71Sopenharmony_ci    console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
453e41f4b71Sopenharmony_ci  });
454e41f4b71Sopenharmony_ci} catch (err) {
455e41f4b71Sopenharmony_ci  let e: BusinessError = err as BusinessError;
456e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
457e41f4b71Sopenharmony_ci}
458e41f4b71Sopenharmony_ci
459e41f4b71Sopenharmony_citry {
460e41f4b71Sopenharmony_ci  // Stop vibration in VIBRATOR_STOP_MODE_PRESET mode.
461e41f4b71Sopenharmony_ci  vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => {
462e41f4b71Sopenharmony_ci    console.info('Succeed in stopping vibration');
463e41f4b71Sopenharmony_ci  }, (error: BusinessError) => {
464e41f4b71Sopenharmony_ci    console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
465e41f4b71Sopenharmony_ci  });
466e41f4b71Sopenharmony_ci} catch (err) {
467e41f4b71Sopenharmony_ci  let e: BusinessError = err as BusinessError;
468e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
469e41f4b71Sopenharmony_ci}
470e41f4b71Sopenharmony_ci```
471e41f4b71Sopenharmony_ci
472e41f4b71Sopenharmony_ci## vibrator.stopVibration<sup>10+</sup>
473e41f4b71Sopenharmony_ci
474e41f4b71Sopenharmony_cistopVibration(callback: AsyncCallback&lt;void&gt;): void
475e41f4b71Sopenharmony_ci
476e41f4b71Sopenharmony_ciStops vibration in all modes. This API uses an asynchronous callback to return the result.
477e41f4b71Sopenharmony_ci
478e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.VIBRATE
479e41f4b71Sopenharmony_ci
480e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
481e41f4b71Sopenharmony_ci
482e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
483e41f4b71Sopenharmony_ci
484e41f4b71Sopenharmony_ci**Parameters**
485e41f4b71Sopenharmony_ci
486e41f4b71Sopenharmony_ci| Name  | Type                     | Mandatory| Description                                                        |
487e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
488e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result. If the vibration stops, **err** is **undefined**; otherwise, **err** is an error object.|
489e41f4b71Sopenharmony_ci
490e41f4b71Sopenharmony_ci**Error codes**
491e41f4b71Sopenharmony_ci
492e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
493e41f4b71Sopenharmony_ci
494e41f4b71Sopenharmony_ci| ID| Error Message          |
495e41f4b71Sopenharmony_ci| -------- | ------------------ |
496e41f4b71Sopenharmony_ci| 201      | Permission denied. |
497e41f4b71Sopenharmony_ci
498e41f4b71Sopenharmony_ci**Example**
499e41f4b71Sopenharmony_ci
500e41f4b71Sopenharmony_ci```ts
501e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
502e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
503e41f4b71Sopenharmony_ci
504e41f4b71Sopenharmony_citry {
505e41f4b71Sopenharmony_ci  // Stop vibration in all modes.
506e41f4b71Sopenharmony_ci  vibrator.stopVibration((error: BusinessError) => {
507e41f4b71Sopenharmony_ci    if (error) {
508e41f4b71Sopenharmony_ci      console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
509e41f4b71Sopenharmony_ci      return;
510e41f4b71Sopenharmony_ci    }
511e41f4b71Sopenharmony_ci    console.info('Succeed in stopping vibration');
512e41f4b71Sopenharmony_ci  })
513e41f4b71Sopenharmony_ci} catch (error) {
514e41f4b71Sopenharmony_ci  let e: BusinessError = error as BusinessError;
515e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
516e41f4b71Sopenharmony_ci}
517e41f4b71Sopenharmony_ci```
518e41f4b71Sopenharmony_ci
519e41f4b71Sopenharmony_ci## vibrator.stopVibration<sup>10+</sup>
520e41f4b71Sopenharmony_ci
521e41f4b71Sopenharmony_cistopVibration(): Promise&lt;void&gt;
522e41f4b71Sopenharmony_ci
523e41f4b71Sopenharmony_ciStops vibration in all modes. This API uses a promise to return the result.
524e41f4b71Sopenharmony_ci
525e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.VIBRATE
526e41f4b71Sopenharmony_ci
527e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
528e41f4b71Sopenharmony_ci
529e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
530e41f4b71Sopenharmony_ci
531e41f4b71Sopenharmony_ci**Return value**
532e41f4b71Sopenharmony_ci
533e41f4b71Sopenharmony_ci| Type               | Description         |
534e41f4b71Sopenharmony_ci| ------------------- | ------------- |
535e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | Promise that returns no value.|
536e41f4b71Sopenharmony_ci
537e41f4b71Sopenharmony_ci**Error codes**
538e41f4b71Sopenharmony_ci
539e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
540e41f4b71Sopenharmony_ci
541e41f4b71Sopenharmony_ci| ID| Error Message          |
542e41f4b71Sopenharmony_ci| -------- | ------------------ |
543e41f4b71Sopenharmony_ci| 201      | Permission denied. |
544e41f4b71Sopenharmony_ci
545e41f4b71Sopenharmony_ci**Example**
546e41f4b71Sopenharmony_ci
547e41f4b71Sopenharmony_ci```ts
548e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
549e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
550e41f4b71Sopenharmony_ci
551e41f4b71Sopenharmony_citry {
552e41f4b71Sopenharmony_ci  // Stop vibration in all modes.
553e41f4b71Sopenharmony_ci  vibrator.stopVibration().then(() => {
554e41f4b71Sopenharmony_ci    console.info('Succeed in stopping vibration');
555e41f4b71Sopenharmony_ci  }, (error: BusinessError) => {
556e41f4b71Sopenharmony_ci    console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
557e41f4b71Sopenharmony_ci  });
558e41f4b71Sopenharmony_ci} catch (error) {
559e41f4b71Sopenharmony_ci  let e: BusinessError = error as BusinessError;
560e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
561e41f4b71Sopenharmony_ci}
562e41f4b71Sopenharmony_ci```
563e41f4b71Sopenharmony_ci
564e41f4b71Sopenharmony_ci## vibrator.stopVibrationSync<sup>12+</sup>
565e41f4b71Sopenharmony_ci
566e41f4b71Sopenharmony_cistopVibrationSync(): void
567e41f4b71Sopenharmony_ci
568e41f4b71Sopenharmony_ciStops any form of motor vibration.
569e41f4b71Sopenharmony_ci
570e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.VIBRATE
571e41f4b71Sopenharmony_ci
572e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
573e41f4b71Sopenharmony_ci
574e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
575e41f4b71Sopenharmony_ci
576e41f4b71Sopenharmony_ci**Error codes**
577e41f4b71Sopenharmony_ci
578e41f4b71Sopenharmony_ciFor details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md) and [Universal Error Codes](../errorcode-universal.md).
579e41f4b71Sopenharmony_ci
580e41f4b71Sopenharmony_ci| ID| Error Message                |
581e41f4b71Sopenharmony_ci| -------- | ------------------------ |
582e41f4b71Sopenharmony_ci| 201      | Permission denied.       |
583e41f4b71Sopenharmony_ci| 14600101 | Device operation failed. |
584e41f4b71Sopenharmony_ci
585e41f4b71Sopenharmony_ci**Example**
586e41f4b71Sopenharmony_ci
587e41f4b71Sopenharmony_ci```ts
588e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
589e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
590e41f4b71Sopenharmony_ci
591e41f4b71Sopenharmony_citry {
592e41f4b71Sopenharmony_ci  // Stop any form of motor vibration.
593e41f4b71Sopenharmony_ci    vibrator.stopVibrationSync()
594e41f4b71Sopenharmony_ci    console.info('Succeed in stopping vibration');
595e41f4b71Sopenharmony_ci} catch (error) {
596e41f4b71Sopenharmony_ci  let e: BusinessError = error as BusinessError;
597e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
598e41f4b71Sopenharmony_ci}
599e41f4b71Sopenharmony_ci```
600e41f4b71Sopenharmony_ci
601e41f4b71Sopenharmony_ci## vibrator.isSupportEffect<sup>10+</sup>
602e41f4b71Sopenharmony_ci
603e41f4b71Sopenharmony_ciisSupportEffect(effectId: string, callback: AsyncCallback&lt;boolean&gt;): void
604e41f4b71Sopenharmony_ci
605e41f4b71Sopenharmony_ciChecks whether an effect ID is supported. This API uses an asynchronous callback to return the result.
606e41f4b71Sopenharmony_ci
607e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
608e41f4b71Sopenharmony_ci
609e41f4b71Sopenharmony_ci**Parameters**
610e41f4b71Sopenharmony_ci
611e41f4b71Sopenharmony_ci| Name  | Type                        | Mandatory| Description                                                  |
612e41f4b71Sopenharmony_ci| -------- | ---------------------------- | ---- | ------------------------------------------------------ |
613e41f4b71Sopenharmony_ci| effectId | string                       | Yes  | Vibration effect ID.                                          |
614e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;boolean&gt; | Yes  | Callback used to return the result. The value **true** means that the effect ID is supported, and **false** means the opposite.|
615e41f4b71Sopenharmony_ci
616e41f4b71Sopenharmony_ci**Error codes**
617e41f4b71Sopenharmony_ci
618e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
619e41f4b71Sopenharmony_ci
620e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
621e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
622e41f4b71Sopenharmony_ci| 201      | Permission denied.                                           |
623e41f4b71Sopenharmony_ci| 401      | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. |
624e41f4b71Sopenharmony_ci
625e41f4b71Sopenharmony_ci**Example**
626e41f4b71Sopenharmony_ci
627e41f4b71Sopenharmony_ci```ts
628e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
629e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
630e41f4b71Sopenharmony_ci
631e41f4b71Sopenharmony_citry {
632e41f4b71Sopenharmony_ci  // Check whether 'haptic.clock.timer' is supported.
633e41f4b71Sopenharmony_ci  vibrator.isSupportEffect('haptic.clock.timer', (err: BusinessError, state: boolean) => {
634e41f4b71Sopenharmony_ci    if (err) {
635e41f4b71Sopenharmony_ci      console.error(`Failed to query effect. Code: ${err.code}, message: ${err.message}`);
636e41f4b71Sopenharmony_ci      return;
637e41f4b71Sopenharmony_ci    }
638e41f4b71Sopenharmony_ci    console.info('Succeed in querying effect');
639e41f4b71Sopenharmony_ci    if (state) {
640e41f4b71Sopenharmony_ci      try {
641e41f4b71Sopenharmony_ci        // To use startVibration, you must configure the ohos.permission.VIBRATE permission.
642e41f4b71Sopenharmony_ci        vibrator.startVibration({
643e41f4b71Sopenharmony_ci          type: 'preset',
644e41f4b71Sopenharmony_ci          effectId: 'haptic.clock.timer',
645e41f4b71Sopenharmony_ci          count: 1,
646e41f4b71Sopenharmony_ci        }, {
647e41f4b71Sopenharmony_ci          usage: 'unknown'
648e41f4b71Sopenharmony_ci        }, (error: BusinessError) => {
649e41f4b71Sopenharmony_ci          if (error) {
650e41f4b71Sopenharmony_ci            console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
651e41f4b71Sopenharmony_ci          } else {
652e41f4b71Sopenharmony_ci            console.info('Succeed in starting vibration');
653e41f4b71Sopenharmony_ci          }
654e41f4b71Sopenharmony_ci        });
655e41f4b71Sopenharmony_ci      } catch (error) {
656e41f4b71Sopenharmony_ci        let e: BusinessError = error as BusinessError;
657e41f4b71Sopenharmony_ci        console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
658e41f4b71Sopenharmony_ci      }
659e41f4b71Sopenharmony_ci    }
660e41f4b71Sopenharmony_ci  })
661e41f4b71Sopenharmony_ci} catch (error) {
662e41f4b71Sopenharmony_ci  let e: BusinessError = error as BusinessError;
663e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
664e41f4b71Sopenharmony_ci}
665e41f4b71Sopenharmony_ci```
666e41f4b71Sopenharmony_ci
667e41f4b71Sopenharmony_ci## vibrator.isSupportEffect<sup>10+</sup>
668e41f4b71Sopenharmony_ci
669e41f4b71Sopenharmony_ciisSupportEffect(effectId: string): Promise&lt;boolean&gt;
670e41f4b71Sopenharmony_ci
671e41f4b71Sopenharmony_ciChecks whether an effect ID is supported. This API uses a promise to return the result.
672e41f4b71Sopenharmony_ci
673e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
674e41f4b71Sopenharmony_ci
675e41f4b71Sopenharmony_ci**Parameters**
676e41f4b71Sopenharmony_ci
677e41f4b71Sopenharmony_ci| Name  | Type  | Mandatory| Description        |
678e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ------------ |
679e41f4b71Sopenharmony_ci| effectId | string | Yes  | Vibration effect ID.|
680e41f4b71Sopenharmony_ci
681e41f4b71Sopenharmony_ci**Return value**
682e41f4b71Sopenharmony_ci
683e41f4b71Sopenharmony_ci| Type                  | Description                                                     |
684e41f4b71Sopenharmony_ci| ---------------------- | --------------------------------------------------------- |
685e41f4b71Sopenharmony_ci| Promise&lt;boolean&gt; | Promise that returns the result. The value **true** means that the effect ID is supported, and **false** means the opposite.|
686e41f4b71Sopenharmony_ci
687e41f4b71Sopenharmony_ci**Error codes**
688e41f4b71Sopenharmony_ci
689e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
690e41f4b71Sopenharmony_ci
691e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
692e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
693e41f4b71Sopenharmony_ci| 201      | Permission denied.                                           |
694e41f4b71Sopenharmony_ci| 401      | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. |
695e41f4b71Sopenharmony_ci
696e41f4b71Sopenharmony_ci**Example**
697e41f4b71Sopenharmony_ci
698e41f4b71Sopenharmony_ci```ts
699e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
700e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
701e41f4b71Sopenharmony_ci
702e41f4b71Sopenharmony_citry {
703e41f4b71Sopenharmony_ci  // Check whether 'haptic.clock.timer' is supported.
704e41f4b71Sopenharmony_ci  vibrator.isSupportEffect('haptic.clock.timer').then((state: boolean) => {
705e41f4b71Sopenharmony_ci    console.info(`The query result is ${state}`);
706e41f4b71Sopenharmony_ci    if (state) {
707e41f4b71Sopenharmony_ci      try {
708e41f4b71Sopenharmony_ci        vibrator.startVibration({
709e41f4b71Sopenharmony_ci          type: 'preset',
710e41f4b71Sopenharmony_ci          effectId: 'haptic.clock.timer',
711e41f4b71Sopenharmony_ci          count: 1,
712e41f4b71Sopenharmony_ci        }, {
713e41f4b71Sopenharmony_ci          usage: 'unknown'
714e41f4b71Sopenharmony_ci        }).then(() => {
715e41f4b71Sopenharmony_ci          console.info('Succeed in starting vibration');
716e41f4b71Sopenharmony_ci        }).catch((error: BusinessError) => {
717e41f4b71Sopenharmony_ci          console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
718e41f4b71Sopenharmony_ci        });
719e41f4b71Sopenharmony_ci      } catch (error) {
720e41f4b71Sopenharmony_ci        let e: BusinessError = error as BusinessError;
721e41f4b71Sopenharmony_ci        console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
722e41f4b71Sopenharmony_ci      }
723e41f4b71Sopenharmony_ci    }
724e41f4b71Sopenharmony_ci  }, (error: BusinessError) => {
725e41f4b71Sopenharmony_ci    console.error(`Failed to query effect. Code: ${error.code}, message: ${error.message}`);
726e41f4b71Sopenharmony_ci  })
727e41f4b71Sopenharmony_ci} catch (error) {
728e41f4b71Sopenharmony_ci  let e: BusinessError = error as BusinessError;
729e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
730e41f4b71Sopenharmony_ci}
731e41f4b71Sopenharmony_ci```
732e41f4b71Sopenharmony_ci
733e41f4b71Sopenharmony_ci## vibrator.isSupportEffectSync<sup>12+</sup>
734e41f4b71Sopenharmony_ci
735e41f4b71Sopenharmony_ciisSupportEffectSync(effectId: string): boolean
736e41f4b71Sopenharmony_ci
737e41f4b71Sopenharmony_ciChecks whether the preset vibration effect is supported.
738e41f4b71Sopenharmony_ci
739e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
740e41f4b71Sopenharmony_ci
741e41f4b71Sopenharmony_ci**Parameters**
742e41f4b71Sopenharmony_ci
743e41f4b71Sopenharmony_ci| Name  | Type  | Mandatory| Description                |
744e41f4b71Sopenharmony_ci| -------- | ------ | ---- | -------------------- |
745e41f4b71Sopenharmony_ci| effectId | string | Yes  | ID of the preset vibration effect.|
746e41f4b71Sopenharmony_ci
747e41f4b71Sopenharmony_ci**Return value**
748e41f4b71Sopenharmony_ci
749e41f4b71Sopenharmony_ci| Type   | Description                                                  |
750e41f4b71Sopenharmony_ci| ------- | ------------------------------------------------------ |
751e41f4b71Sopenharmony_ci| boolean | Returned object. The value **true** means that the effect ID is supported, and **false** means the opposite.|
752e41f4b71Sopenharmony_ci
753e41f4b71Sopenharmony_ci**Error codes**
754e41f4b71Sopenharmony_ci
755e41f4b71Sopenharmony_ciFor details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md) and [Universal Error Codes](../errorcode-universal.md).
756e41f4b71Sopenharmony_ci
757e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
758e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
759e41f4b71Sopenharmony_ci| 401      | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. |
760e41f4b71Sopenharmony_ci| 14600101 | Device operation failed.                                     |
761e41f4b71Sopenharmony_ci
762e41f4b71Sopenharmony_ci**Example**
763e41f4b71Sopenharmony_ci
764e41f4b71Sopenharmony_ci```ts
765e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
766e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
767e41f4b71Sopenharmony_ci
768e41f4b71Sopenharmony_citry {
769e41f4b71Sopenharmony_ci    // Check whether the preset 'haptic.clock.timer' is supported.
770e41f4b71Sopenharmony_ci    let ret = vibrator.isSupportEffectSync('haptic.clock.timer');
771e41f4b71Sopenharmony_ci    console.info(`The query result is ${ret}`);
772e41f4b71Sopenharmony_ci} catch (error) {
773e41f4b71Sopenharmony_ci    let e: BusinessError = error as BusinessError;
774e41f4b71Sopenharmony_ci    console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
775e41f4b71Sopenharmony_ci}
776e41f4b71Sopenharmony_ci```
777e41f4b71Sopenharmony_ci
778e41f4b71Sopenharmony_ci## vibrator.isHdHapticSupported<sup>12+</sup>
779e41f4b71Sopenharmony_ci
780e41f4b71Sopenharmony_ciisHdHapticSupported(): boolean
781e41f4b71Sopenharmony_ci
782e41f4b71Sopenharmony_ciChecks whether HD vibration is supported.
783e41f4b71Sopenharmony_ci
784e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
785e41f4b71Sopenharmony_ci
786e41f4b71Sopenharmony_ci**Return value**
787e41f4b71Sopenharmony_ci
788e41f4b71Sopenharmony_ci| Type   | Description      |
789e41f4b71Sopenharmony_ci| ------- | ---------- |
790e41f4b71Sopenharmony_ci| boolean | Returned object.|
791e41f4b71Sopenharmony_ci
792e41f4b71Sopenharmony_ci**Error codes**
793e41f4b71Sopenharmony_ci
794e41f4b71Sopenharmony_ciFor details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md).
795e41f4b71Sopenharmony_ci
796e41f4b71Sopenharmony_ci| ID| Error Message                |
797e41f4b71Sopenharmony_ci| -------- | ------------------------ |
798e41f4b71Sopenharmony_ci| 14600101 | Device operation failed. |
799e41f4b71Sopenharmony_ci
800e41f4b71Sopenharmony_ci**Example**
801e41f4b71Sopenharmony_ci
802e41f4b71Sopenharmony_ci```ts
803e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
804e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
805e41f4b71Sopenharmony_ci
806e41f4b71Sopenharmony_citry {
807e41f4b71Sopenharmony_ci    // Check whether HD vibration is supported.
808e41f4b71Sopenharmony_ci    let ret = vibrator.isHdHapticSupported();
809e41f4b71Sopenharmony_ci    console.info(`The query result is ${ret}`);
810e41f4b71Sopenharmony_ci} catch (error) {
811e41f4b71Sopenharmony_ci    let e: BusinessError = error as BusinessError;
812e41f4b71Sopenharmony_ci    console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
813e41f4b71Sopenharmony_ci}
814e41f4b71Sopenharmony_ci```
815e41f4b71Sopenharmony_ci
816e41f4b71Sopenharmony_ci## EffectId
817e41f4b71Sopenharmony_ci
818e41f4b71Sopenharmony_ciEnumerates the preset vibration effect IDs.
819e41f4b71Sopenharmony_ci
820e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
821e41f4b71Sopenharmony_ci
822e41f4b71Sopenharmony_ci| Name              | Value                  | Description                            |
823e41f4b71Sopenharmony_ci| ------------------ | -------------------- | -------------------------------- |
824e41f4b71Sopenharmony_ci| EFFECT_CLOCK_TIMER | 'haptic.clock.timer' | Vibration effect when a user adjusts the timer.|
825e41f4b71Sopenharmony_ci
826e41f4b71Sopenharmony_ci## HapticFeedback<sup>12+</sup>
827e41f4b71Sopenharmony_ci
828e41f4b71Sopenharmony_ciDefines the vibration effect.
829e41f4b71Sopenharmony_ci
830e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
831e41f4b71Sopenharmony_ci
832e41f4b71Sopenharmony_ci| Name        | Value                   | Description                        |
833e41f4b71Sopenharmony_ci| ------------ | --------------------- | ---------------------------- |
834e41f4b71Sopenharmony_ci| EFFECT_SOFT  | 'haptic.effect.soft'  | Soft vibration, low frequency.|
835e41f4b71Sopenharmony_ci| EFFECT_HARD  | 'haptic.effect.hard'  | Hard vibration, medium frequency.|
836e41f4b71Sopenharmony_ci| EFFECT_SHARP | 'haptic.effect.sharp' | Sharp vibration, high frequency.|
837e41f4b71Sopenharmony_ci
838e41f4b71Sopenharmony_ci## VibratorStopMode
839e41f4b71Sopenharmony_ci
840e41f4b71Sopenharmony_ciEnumerates the modes available to stop the vibration.
841e41f4b71Sopenharmony_ci
842e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
843e41f4b71Sopenharmony_ci
844e41f4b71Sopenharmony_ci| Name                     | Value      | Description                          |
845e41f4b71Sopenharmony_ci| ------------------------- | -------- | ------------------------------ |
846e41f4b71Sopenharmony_ci| VIBRATOR_STOP_MODE_TIME   | 'time'   | The vibration to stop is in **duration** mode.|
847e41f4b71Sopenharmony_ci| VIBRATOR_STOP_MODE_PRESET | 'preset' | The vibration to stop is in **EffectId** mode.|
848e41f4b71Sopenharmony_ci
849e41f4b71Sopenharmony_ci## VibrateEffect<sup>9+</sup>
850e41f4b71Sopenharmony_ci
851e41f4b71Sopenharmony_ciDescribes the vibration effect.
852e41f4b71Sopenharmony_ci
853e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
854e41f4b71Sopenharmony_ci
855e41f4b71Sopenharmony_ci| Type                            | Description                          |
856e41f4b71Sopenharmony_ci| -------------------------------- | ------------------------------ |
857e41f4b71Sopenharmony_ci| [VibrateTime](#vibratetime9) | Vibration with the specified duration.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
858e41f4b71Sopenharmony_ci| [VibratePreset](#vibratepreset9) | Vibration with a preset effect.|
859e41f4b71Sopenharmony_ci| [VibrateFromFile](#vibratefromfile10) | Vibration according to a custom vibration configuration file.|
860e41f4b71Sopenharmony_ci
861e41f4b71Sopenharmony_ci## VibrateTime<sup>9+</sup>
862e41f4b71Sopenharmony_ci
863e41f4b71Sopenharmony_ciDescribes the fixed-duration vibration.
864e41f4b71Sopenharmony_ci
865e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
866e41f4b71Sopenharmony_ci
867e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
868e41f4b71Sopenharmony_ci
869e41f4b71Sopenharmony_ci| Name    | Type   | Mandatory| Description                          |
870e41f4b71Sopenharmony_ci| -------- | ------ | ----- | ------------------------------ |
871e41f4b71Sopenharmony_ci| type     | 'time' |  Yes  | The value **time** means vibration with the specified duration.|
872e41f4b71Sopenharmony_ci| duration | number |  Yes  | Vibration duration, in ms.        |
873e41f4b71Sopenharmony_ci
874e41f4b71Sopenharmony_ci## VibratePreset<sup>9+</sup>
875e41f4b71Sopenharmony_ci
876e41f4b71Sopenharmony_ciDescribes the preset vibration.
877e41f4b71Sopenharmony_ci
878e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
879e41f4b71Sopenharmony_ci
880e41f4b71Sopenharmony_ci| Name    | Type     | Mandatory| Description                          |
881e41f4b71Sopenharmony_ci| -------- | -------- | ---- |------------------------------ |
882e41f4b71Sopenharmony_ci| type     | 'preset' |  Yes | The value **preset** means vibration with the specified effect.|
883e41f4b71Sopenharmony_ci| effectId | string   |  Yes | Preset vibration effect ID.            |
884e41f4b71Sopenharmony_ci| count    | number   |  No | Number of repeated vibrations. The default value is **1**. This parameter is optional.|
885e41f4b71Sopenharmony_ci| intensity<sup>12+</sup> | number | No| Vibration intensity. The value ranges from 0 to 100. The default value is **100**. This parameter is optional. If vibration intensity adjustment is not supported, the default vibration intensity will be used.|
886e41f4b71Sopenharmony_ci
887e41f4b71Sopenharmony_ci## VibrateFromFile<sup>10+</sup>
888e41f4b71Sopenharmony_ci
889e41f4b71Sopenharmony_ciDescribes the custom vibration type, which is supported only by certain devices. If a device does not support this vibration type, [an error code indicating unsupported device](../errorcode-universal.md) is returned.
890e41f4b71Sopenharmony_ci
891e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
892e41f4b71Sopenharmony_ci
893e41f4b71Sopenharmony_ci| Name    | Type      | Mandatory| Description                          |
894e41f4b71Sopenharmony_ci| -------- | --------  | ---- | ------------------------------ |
895e41f4b71Sopenharmony_ci| type     | 'file' |  Yes | The value **file** means vibration according to a vibration configuration file.|
896e41f4b71Sopenharmony_ci| hapticFd | [HapticFileDescriptor](#hapticfiledescriptor10)<sup>10+</sup> | Yes| File descriptor (FD) of the vibration configuration file.|
897e41f4b71Sopenharmony_ci
898e41f4b71Sopenharmony_ci## HapticFileDescriptor<sup>10+</sup>
899e41f4b71Sopenharmony_ci
900e41f4b71Sopenharmony_ciDescribes the FD of a custom vibration configuration file. Ensure that the file is available, and the parameters in it can be obtained from the sandbox path through the [file management API](../apis-core-file-kit/js-apis-file-fs.md#fsopen) or from the HAP resource through the [resource management API](../apis-localization-kit/js-apis-resource-manager.md#getrawfd9). The use case is as follows: The system triggers vibration according to the sequence set in a configuration file, based on the specified offset and length. For details about the storage format of the vibration sequence, see [Custom Vibration](../../device/sensor/vibrator-guidelines.md#custom-vibration).
901e41f4b71Sopenharmony_ci
902e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
903e41f4b71Sopenharmony_ci
904e41f4b71Sopenharmony_ci| Name    | Type     |  Mandatory | Description                          |
905e41f4b71Sopenharmony_ci| -------- | -------- |--------| ------------------------------|
906e41f4b71Sopenharmony_ci| fd       | number   |  Yes   | FD of the custom vibration configuration file.               |
907e41f4b71Sopenharmony_ci| offset   | number   |  No   | Offset from the start position of the file, in bytes. The default value is the start position of the file, and the value cannot exceed the valid range of the file.|
908e41f4b71Sopenharmony_ci| length   | number   |  No   | Resource length, in bytes. The default value is the length from the offset position to the end of the file, and the value cannot exceed the valid range of the file.|
909e41f4b71Sopenharmony_ci
910e41f4b71Sopenharmony_ci## VibrateAttribute<sup>9+</sup>
911e41f4b71Sopenharmony_ci
912e41f4b71Sopenharmony_ciDescribes the vibration attribute.
913e41f4b71Sopenharmony_ci
914e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
915e41f4b71Sopenharmony_ci
916e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
917e41f4b71Sopenharmony_ci
918e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description          |
919e41f4b71Sopenharmony_ci| ----- | ------ | ---- | -------------- |
920e41f4b71Sopenharmony_ci| id    | number      |  No| Vibrator ID. The default value is **0**.   |
921e41f4b71Sopenharmony_ci| usage | [Usage](#usage9) | Yes| Vibration scenario.|
922e41f4b71Sopenharmony_ci
923e41f4b71Sopenharmony_ci## Usage<sup>9+</sup>
924e41f4b71Sopenharmony_ci
925e41f4b71Sopenharmony_citype Usage = 'unknown' | 'alarm' | 'ring' | 'notification' | 'communication' | 'touch' | 'media' | 'physicalFeedback' | 'simulateReality'
926e41f4b71Sopenharmony_ci
927e41f4b71Sopenharmony_ciEnumerates the vibration scenarios.
928e41f4b71Sopenharmony_ci
929e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
930e41f4b71Sopenharmony_ci
931e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
932e41f4b71Sopenharmony_ci<!--RP1-->
933e41f4b71Sopenharmony_ci| Type    | Description                          |
934e41f4b71Sopenharmony_ci| ---------------- | ------------------------------ |
935e41f4b71Sopenharmony_ci| 'unknown'     | Unknown scenario, with the lowest priority. This parameter has a fixed value of **unknown**.|
936e41f4b71Sopenharmony_ci| 'alarm'      | Vibration for alarms. This parameter has a fixed value of **alarm**.|
937e41f4b71Sopenharmony_ci| 'ring'         | Vibration for ringing. This parameter has a fixed value of **ring**.|
938e41f4b71Sopenharmony_ci| 'notification' | Vibration for notification. This parameter has a fixed value of **notification**.|
939e41f4b71Sopenharmony_ci| 'communication' | Vibration for communication. This parameter has a fixed value of **communication**.|
940e41f4b71Sopenharmony_ci| 'touch'        | Vibration for touch. This parameter has a fixed value of **touch**.|
941e41f4b71Sopenharmony_ci| 'media'        | Vibration for media. This parameter has a fixed value of **media**.|
942e41f4b71Sopenharmony_ci| 'physicalFeedback' | Vibration for physical feedback. This parameter has a fixed value of **physicalFeedback**.|
943e41f4b71Sopenharmony_ci| 'simulateReality' | Vibration for simulated reality. This parameter has a fixed value of **simulateReality**.|
944e41f4b71Sopenharmony_ci<!--RP1End-->
945e41f4b71Sopenharmony_ci
946e41f4b71Sopenharmony_ci## vibrator.vibrate<sup>(deprecated)</sup>
947e41f4b71Sopenharmony_ci
948e41f4b71Sopenharmony_civibrate(duration: number): Promise&lt;void&gt;
949e41f4b71Sopenharmony_ci
950e41f4b71Sopenharmony_ciTriggers vibration with the specified duration. This API uses a promise to return the result.
951e41f4b71Sopenharmony_ci
952e41f4b71Sopenharmony_ciThis API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9-1)<sup>9+</sup> instead.
953e41f4b71Sopenharmony_ci
954e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.VIBRATE
955e41f4b71Sopenharmony_ci
956e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
957e41f4b71Sopenharmony_ci
958e41f4b71Sopenharmony_ci**Parameters**
959e41f4b71Sopenharmony_ci
960e41f4b71Sopenharmony_ci| Name  | Type  | Mandatory| Description                  |
961e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ---------------------- |
962e41f4b71Sopenharmony_ci| duration | number | Yes  | Vibration duration, in ms.|
963e41f4b71Sopenharmony_ci
964e41f4b71Sopenharmony_ci**Return value**
965e41f4b71Sopenharmony_ci
966e41f4b71Sopenharmony_ci| Type               | Description                                  |
967e41f4b71Sopenharmony_ci| ------------------- | -------------------------------------- |
968e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | Promise that returns no value.|
969e41f4b71Sopenharmony_ci
970e41f4b71Sopenharmony_ci**Example**
971e41f4b71Sopenharmony_ci
972e41f4b71Sopenharmony_ci```ts
973e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
974e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
975e41f4b71Sopenharmony_ci
976e41f4b71Sopenharmony_civibrator.vibrate(1000).then(() => {
977e41f4b71Sopenharmony_ci  console.info('Succeed in vibrating');
978e41f4b71Sopenharmony_ci}, (error: BusinessError) => {
979e41f4b71Sopenharmony_ci  console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
980e41f4b71Sopenharmony_ci});
981e41f4b71Sopenharmony_ci```
982e41f4b71Sopenharmony_ci
983e41f4b71Sopenharmony_ci## vibrator.vibrate<sup>(deprecated)</sup>
984e41f4b71Sopenharmony_ci
985e41f4b71Sopenharmony_civibrate(duration: number, callback?: AsyncCallback&lt;void&gt;): void
986e41f4b71Sopenharmony_ci
987e41f4b71Sopenharmony_ciTriggers vibration with the specified duration. This API uses an asynchronous callback to return the result.
988e41f4b71Sopenharmony_ci
989e41f4b71Sopenharmony_ciThis API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9)<sup>9+</sup> instead.
990e41f4b71Sopenharmony_ci
991e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.VIBRATE
992e41f4b71Sopenharmony_ci
993e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
994e41f4b71Sopenharmony_ci
995e41f4b71Sopenharmony_ci**Parameters**
996e41f4b71Sopenharmony_ci
997e41f4b71Sopenharmony_ci| Name  | Type                     | Mandatory| Description                                                      |
998e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ---------------------------------------------------------- |
999e41f4b71Sopenharmony_ci| duration | number                    | Yes  | Vibration duration, in ms.                                    |
1000e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;void&gt; | No  | Callback used to return the result. If the vibration starts, **err** is **undefined**; otherwise, **err** is an error object.|
1001e41f4b71Sopenharmony_ci
1002e41f4b71Sopenharmony_ci**Example**
1003e41f4b71Sopenharmony_ci
1004e41f4b71Sopenharmony_ci```ts
1005e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
1006e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
1007e41f4b71Sopenharmony_ci
1008e41f4b71Sopenharmony_civibrator.vibrate(1000, (error: BusinessError) => {
1009e41f4b71Sopenharmony_ci  if (error) {
1010e41f4b71Sopenharmony_ci    console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
1011e41f4b71Sopenharmony_ci  } else {
1012e41f4b71Sopenharmony_ci    console.info('Succeed in vibrating');
1013e41f4b71Sopenharmony_ci  }
1014e41f4b71Sopenharmony_ci})
1015e41f4b71Sopenharmony_ci```
1016e41f4b71Sopenharmony_ci
1017e41f4b71Sopenharmony_ci
1018e41f4b71Sopenharmony_ci## vibrator.vibrate<sup>(deprecated)</sup>
1019e41f4b71Sopenharmony_ci
1020e41f4b71Sopenharmony_civibrate(effectId: EffectId): Promise&lt;void&gt;
1021e41f4b71Sopenharmony_ci
1022e41f4b71Sopenharmony_ciTriggers vibration with the specified effect. This API uses a promise to return the result.
1023e41f4b71Sopenharmony_ci
1024e41f4b71Sopenharmony_ciThis API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9-1)<sup>9+</sup> instead.
1025e41f4b71Sopenharmony_ci
1026e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.VIBRATE
1027e41f4b71Sopenharmony_ci
1028e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
1029e41f4b71Sopenharmony_ci
1030e41f4b71Sopenharmony_ci**Parameters**
1031e41f4b71Sopenharmony_ci
1032e41f4b71Sopenharmony_ci| Name  | Type                 | Mandatory| Description              |
1033e41f4b71Sopenharmony_ci| -------- | --------------------- | ---- | ------------------ |
1034e41f4b71Sopenharmony_ci| effectId | [EffectId](#effectid) | Yes  | Preset vibration effect ID.|
1035e41f4b71Sopenharmony_ci
1036e41f4b71Sopenharmony_ci**Return value**
1037e41f4b71Sopenharmony_ci
1038e41f4b71Sopenharmony_ci| Type               | Description                                  |
1039e41f4b71Sopenharmony_ci| ------------------- | -------------------------------------- |
1040e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | Promise that returns no value.|
1041e41f4b71Sopenharmony_ci
1042e41f4b71Sopenharmony_ci**Example**
1043e41f4b71Sopenharmony_ci
1044e41f4b71Sopenharmony_ci```ts
1045e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
1046e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
1047e41f4b71Sopenharmony_ci
1048e41f4b71Sopenharmony_civibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(() => {
1049e41f4b71Sopenharmony_ci  console.info('Succeed in vibrating');
1050e41f4b71Sopenharmony_ci}, (error: BusinessError) => {
1051e41f4b71Sopenharmony_ci  console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
1052e41f4b71Sopenharmony_ci});
1053e41f4b71Sopenharmony_ci```
1054e41f4b71Sopenharmony_ci
1055e41f4b71Sopenharmony_ci
1056e41f4b71Sopenharmony_ci## vibrator.vibrate<sup>(deprecated)</sup>
1057e41f4b71Sopenharmony_ci
1058e41f4b71Sopenharmony_civibrate(effectId: EffectId, callback?: AsyncCallback&lt;void&gt;): void
1059e41f4b71Sopenharmony_ci
1060e41f4b71Sopenharmony_ciTriggers vibration with the specified effect. This API uses an asynchronous callback to return the result.
1061e41f4b71Sopenharmony_ci
1062e41f4b71Sopenharmony_ciThis API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9)<sup>9+</sup> instead.
1063e41f4b71Sopenharmony_ci
1064e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.VIBRATE
1065e41f4b71Sopenharmony_ci
1066e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
1067e41f4b71Sopenharmony_ci
1068e41f4b71Sopenharmony_ci**Parameters**
1069e41f4b71Sopenharmony_ci
1070e41f4b71Sopenharmony_ci| Name  | Type                     | Mandatory| Description                                                      |
1071e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ---------------------------------------------------------- |
1072e41f4b71Sopenharmony_ci| effectId | [EffectId](#effectid)     | Yes  | Preset vibration effect ID.                                        |
1073e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;void&gt; | No  | Callback used to return the result. If the vibration starts, **err** is **undefined**; otherwise, **err** is an error object.|
1074e41f4b71Sopenharmony_ci
1075e41f4b71Sopenharmony_ci**Example**
1076e41f4b71Sopenharmony_ci
1077e41f4b71Sopenharmony_ci```ts
1078e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
1079e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
1080e41f4b71Sopenharmony_ci
1081e41f4b71Sopenharmony_civibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => {
1082e41f4b71Sopenharmony_ci  if (error) {
1083e41f4b71Sopenharmony_ci    console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
1084e41f4b71Sopenharmony_ci  } else {
1085e41f4b71Sopenharmony_ci    console.info('Succeed in vibrating');
1086e41f4b71Sopenharmony_ci  }
1087e41f4b71Sopenharmony_ci})
1088e41f4b71Sopenharmony_ci```
1089e41f4b71Sopenharmony_ci
1090e41f4b71Sopenharmony_ci## vibrator.stop<sup>(deprecated)</sup>
1091e41f4b71Sopenharmony_ci
1092e41f4b71Sopenharmony_cistop(stopMode: VibratorStopMode): Promise&lt;void&gt;
1093e41f4b71Sopenharmony_ci
1094e41f4b71Sopenharmony_ciStops vibration in the specified mode. This API uses a promise to return the result.
1095e41f4b71Sopenharmony_ci
1096e41f4b71Sopenharmony_ciThis API is deprecated since API version 9. You are advised to use [vibrator.stopVibration](#vibratorstopvibration9-1)<sup>9+</sup> instead.
1097e41f4b71Sopenharmony_ci
1098e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.VIBRATE
1099e41f4b71Sopenharmony_ci
1100e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
1101e41f4b71Sopenharmony_ci
1102e41f4b71Sopenharmony_ci**Parameters**
1103e41f4b71Sopenharmony_ci
1104e41f4b71Sopenharmony_ci| Name  | Type                                 | Mandatory| Description                    |
1105e41f4b71Sopenharmony_ci| -------- | ------------------------------------- | ---- | ------------------------ |
1106e41f4b71Sopenharmony_ci| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes  | Mode to stop the vibration.|
1107e41f4b71Sopenharmony_ci
1108e41f4b71Sopenharmony_ci**Return value**
1109e41f4b71Sopenharmony_ci
1110e41f4b71Sopenharmony_ci| Type               | Description                                  |
1111e41f4b71Sopenharmony_ci| ------------------- | -------------------------------------- |
1112e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | Promise that returns no value.|
1113e41f4b71Sopenharmony_ci
1114e41f4b71Sopenharmony_ci**Example**
1115e41f4b71Sopenharmony_ci
1116e41f4b71Sopenharmony_ci```ts
1117e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
1118e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
1119e41f4b71Sopenharmony_ci
1120e41f4b71Sopenharmony_ci// Start vibration based on the specified effect ID.
1121e41f4b71Sopenharmony_civibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => {
1122e41f4b71Sopenharmony_ci  if (error) {
1123e41f4b71Sopenharmony_ci    console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
1124e41f4b71Sopenharmony_ci  } else {
1125e41f4b71Sopenharmony_ci    console.info('Succeed in vibrating');
1126e41f4b71Sopenharmony_ci  }
1127e41f4b71Sopenharmony_ci})
1128e41f4b71Sopenharmony_ci// Stop vibration in VIBRATOR_STOP_MODE_PRESET mode.
1129e41f4b71Sopenharmony_civibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => {
1130e41f4b71Sopenharmony_ci  console.info('Succeed in stopping');
1131e41f4b71Sopenharmony_ci}, (error: BusinessError) => {
1132e41f4b71Sopenharmony_ci  console.error(`Failed to stop. Code: ${error.code}, message: ${error.message}`);
1133e41f4b71Sopenharmony_ci});
1134e41f4b71Sopenharmony_ci```
1135e41f4b71Sopenharmony_ci
1136e41f4b71Sopenharmony_ci
1137e41f4b71Sopenharmony_ci## vibrator.stop<sup>(deprecated)</sup>
1138e41f4b71Sopenharmony_ci
1139e41f4b71Sopenharmony_cistop(stopMode: VibratorStopMode, callback?: AsyncCallback&lt;void&gt;): void
1140e41f4b71Sopenharmony_ci
1141e41f4b71Sopenharmony_ciStops vibration in the specified mode. This API uses an asynchronous callback to return the result.
1142e41f4b71Sopenharmony_ci
1143e41f4b71Sopenharmony_ciThis API is deprecated since API version 9. You are advised to use [vibrator.stopVibration](#vibratorstopvibration9)<sup>9+</sup> instead.
1144e41f4b71Sopenharmony_ci
1145e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.VIBRATE
1146e41f4b71Sopenharmony_ci
1147e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.MiscDevice
1148e41f4b71Sopenharmony_ci
1149e41f4b71Sopenharmony_ci**Parameters**
1150e41f4b71Sopenharmony_ci
1151e41f4b71Sopenharmony_ci| Name  | Type                                 | Mandatory| Description                                                        |
1152e41f4b71Sopenharmony_ci| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ |
1153e41f4b71Sopenharmony_ci| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes  | Mode to stop the vibration.                                    |
1154e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;void&gt;             | No  | Callback used to return the result. If the vibration stops, **err** is **undefined**; otherwise, **err** is an error object.|
1155e41f4b71Sopenharmony_ci
1156e41f4b71Sopenharmony_ci**Example**
1157e41f4b71Sopenharmony_ci
1158e41f4b71Sopenharmony_ci```ts
1159e41f4b71Sopenharmony_ciimport { vibrator } from '@kit.SensorServiceKit';
1160e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
1161e41f4b71Sopenharmony_ci
1162e41f4b71Sopenharmony_ci// Start vibration based on the specified effect ID.
1163e41f4b71Sopenharmony_civibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => {
1164e41f4b71Sopenharmony_ci  if (error) {
1165e41f4b71Sopenharmony_ci    console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
1166e41f4b71Sopenharmony_ci  } else {
1167e41f4b71Sopenharmony_ci    console.info('Succeed in vibrating');
1168e41f4b71Sopenharmony_ci  }
1169e41f4b71Sopenharmony_ci})
1170e41f4b71Sopenharmony_ci// Stop vibration in VIBRATOR_STOP_MODE_PRESET mode.
1171e41f4b71Sopenharmony_civibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, (error: BusinessError) => {
1172e41f4b71Sopenharmony_ci  if (error) {
1173e41f4b71Sopenharmony_ci    console.error(`Failed to stop. Code: ${error.code}, message: ${error.message}`);
1174e41f4b71Sopenharmony_ci  } else {
1175e41f4b71Sopenharmony_ci    console.info('Succeed in stopping');
1176e41f4b71Sopenharmony_ci  }
1177e41f4b71Sopenharmony_ci})
1178e41f4b71Sopenharmony_ci```
1179