• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 # @ohos.vibrator (Vibrator)
2 
3 The **vibrator** module provides APIs for starting or stopping vibration.
4 
5 > **NOTE**
6 >
7 > 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.
8 
9 
10 ## Modules to Import
11 
12 ```ts
13 import vibrator from '@ohos.vibrator';
14 ```
15 
16 ## vibrator.startVibration<sup>9+</sup>
17 
18 startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: AsyncCallback&lt;void&gt;): void
19 
20 Starts vibration with the specified effect and attribute. This API uses an asynchronous callback to return the result.
21 
22 **Required permissions**: ohos.permission.VIBRATE
23 
24 **System capability**: SystemCapability.Sensors.MiscDevice
25 
26 **Parameters**
27 
28 | Name   | Type                                  | Mandatory| Description                                                        |
29 | --------- | -------------------------------------- | ---- | :----------------------------------------------------------- |
30 | 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.|
31 | attribute | [VibrateAttribute](#vibrateattribute9) | Yes  | Vibration attribute.                                              |
32 | 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.  |
33 
34 **Error codes**
35 
36 For details about the error codes, see [Vibrator Error Codes](../errorcodes/errorcode-vibrator.md).
37 
38 | ID| Error Message                |
39 | -------- | ------------------------ |
40 | 14600101 | Device operation failed. |
41 
42 **Example**
43 
44 Trigger vibration with the specified duration.
45 
46 ```ts
47 import vibrator from '@ohos.vibrator';
48 import { BusinessError } from '@ohos.base';
49 
50 try {
51   vibrator.startVibration({
52     type: 'time',
53     duration: 1000,
54   }, {
55     id: 0,
56     usage: 'alarm'
57   }, (error: BusinessError) => {
58     if (error) {
59       console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
60       return;
61     }
62     console.info('Succeed in starting vibration');
63   });
64 } catch (err) {
65   let e: BusinessError = err as BusinessError;
66   console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
67 }
68 ```
69 
70 Trigger vibration with a preset effect.
71 
72 ```ts
73 import vibrator from '@ohos.vibrator';
74 import { BusinessError } from '@ohos.base';
75 
76 try {
77   vibrator.startVibration({
78     type: 'preset',
79     effectId: 'haptic.clock.timer',
80     count: 1,
81   }, {
82     id: 0,
83     usage: 'alarm'
84   }, (error: BusinessError) => {
85     if (error) {
86       console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
87       return;
88     }
89     console.info('Succeed in starting vibration');
90   });
91 } catch (err) {
92   let e: BusinessError = err as BusinessError;
93   console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
94 }
95 ```
96 
97 Trigger vibration according to a custom vibration configuration file.
98 
99 ```ts
100 import vibrator from '@ohos.vibrator';
101 import resourceManager from '@ohos.resourceManager';
102 import { BusinessError } from '@ohos.base';
103 
104 const fileName: string = 'xxx.json';
105 
106 let rawFd: resourceManager.RawFileDescriptor = getContext().resourceManager.getRawFdSync(fileName);
107 
108 try {
109   vibrator.startVibration({
110     type: "file",
111     hapticFd: { fd: rawFd.fd, offset: rawFd.offset, length: rawFd.length }
112   }, {
113     id: 0,
114     usage: 'alarm'
115   }, (error: BusinessError) => {
116     if (error) {
117       console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
118       return;
119     }
120     console.info('Succeed in starting vibration');
121   });
122 } catch (err) {
123   let e: BusinessError = err as BusinessError;
124   console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
125 }
126 
127 getContext().resourceManager.closeRawFdSync(fileName);
128 ```
129 
130 ## vibrator.startVibration<sup>9+</sup>
131 
132 startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise&lt;void&gt;
133 
134 Starts vibration with the specified effect and attribute. This API uses a promise to return the result.
135 
136 **Required permissions**: ohos.permission.VIBRATE
137 
138 **System capability**: SystemCapability.Sensors.MiscDevice
139 
140 **Parameters**
141 
142 | Name   | Type                                  | Mandatory| Description                                                        |
143 | --------- | -------------------------------------- | ---- | ------------------------------------------------------------ |
144 | 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.|
145 | attribute | [VibrateAttribute](#vibrateattribute9) | Yes  | Vibration attribute.                                              |
146 
147 **Return value**
148 
149 | Type               | Description                                  |
150 | ------------------- | -------------------------------------- |
151 | Promise&lt;void&gt; | Promise that returns no value.|
152 
153 **Error codes**
154 
155 For details about the error codes, see [Vibrator Error Codes](../errorcodes/errorcode-vibrator.md).
156 
157 | ID| Error Message                |
158 | -------- | ------------------------ |
159 | 14600101 | Device operation failed. |
160 
161 **Example**
162 
163 Trigger vibration with the specified duration.
164 
165 ```ts
166 import vibrator from '@ohos.vibrator';
167 import { BusinessError } from '@ohos.base';
168 
169 try {
170   vibrator.startVibration({
171     type: 'time',
172     duration: 1000
173   }, {
174     id: 0,
175     usage: 'alarm'
176   }).then(() => {
177     console.info('Succeed in starting vibration');
178   }, (error: BusinessError) => {
179     console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
180   });
181 } catch (err) {
182   let e: BusinessError = err as BusinessError;
183   console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
184 }
185 ```
186 
187 Trigger vibration with a preset effect.
188 
189 ```ts
190 import vibrator from '@ohos.vibrator';
191 import { BusinessError } from '@ohos.base';
192 
193 try {
194   vibrator.startVibration({
195     type: 'preset',
196     effectId: 'haptic.clock.timer',
197     count: 1,
198   }, {
199     id: 0,
200     usage: 'alarm'
201   }).then(() => {
202     console.info('Succeed in starting vibration');
203   }, (error: BusinessError) => {
204     console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
205   });
206 } catch (err) {
207   let e: BusinessError = err as BusinessError;
208   console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
209 }
210 ```
211 
212 Trigger vibration according to a custom vibration configuration file.
213 
214 ```ts
215 import vibrator from '@ohos.vibrator';
216 import resourceManager from '@ohos.resourceManager';
217 import { BusinessError } from '@ohos.base';
218 
219 const fileName: string = 'xxx.json';
220 
221 let rawFd: resourceManager.RawFileDescriptor = getContext().resourceManager.getRawFdSync(fileName);
222 
223 try {
224   vibrator.startVibration({
225     type: "file",
226     hapticFd: { fd: rawFd.fd, offset: rawFd.offset, length: rawFd.length }
227   }, {
228     id: 0,
229     usage: 'alarm'
230   }).then(() => {
231     console.info('Succeed in starting vibration');
232   }, (error: BusinessError) => {
233     console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
234   });
235 } catch (err) {
236   let e: BusinessError = err as BusinessError;
237   console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
238 }
239 
240 getContext().resourceManager.closeRawFdSync(fileName);
241 ```
242 
243 ## vibrator.stopVibration<sup>9+</sup>
244 
245 stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback&lt;void&gt;): void
246 
247 Stops vibration in the specified mode. This API uses an asynchronous callback to return the result.
248 
249 **Required permissions**: ohos.permission.VIBRATE
250 
251 **System capability**: SystemCapability.Sensors.MiscDevice
252 
253 **Parameters**
254 
255 | Name  | Type                                 | Mandatory| Description                                                        |
256 | -------- | ------------------------------------- | ---- | ------------------------------------------------------------ |
257 | 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).                                 |
258 | 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.|
259 
260 **Example**
261 
262 Stop fixed-duration vibration.
263 
264 ```ts
265 import vibrator from '@ohos.vibrator';
266 import { BusinessError } from '@ohos.base';
267 
268 try {
269   // Start vibration at a fixed duration.
270   vibrator.startVibration({
271     type: 'time',
272     duration: 1000,
273   }, {
274     id: 0,
275     usage: 'alarm'
276   }, (error: BusinessError) => {
277     if (error) {
278       console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
279       return;
280     }
281     console.info('Succeed in starting vibration');
282   });
283 } catch (err) {
284   let e: BusinessError = err as BusinessError;
285   console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
286 }
287 
288 try {
289   // Stop vibration in VIBRATOR_STOP_MODE_TIME mode.
290   vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, (error: BusinessError) => {
291     if (error) {
292       console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
293       return;
294     }
295     console.info('Succeed in stopping vibration');
296   })
297 } catch (err) {
298   let e: BusinessError = err as BusinessError;
299   console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
300 }
301 ```
302 
303 Stop preset vibration.
304 
305 ```ts
306 import vibrator from '@ohos.vibrator';
307 import { BusinessError } from '@ohos.base';
308 
309 try {
310   // Start vibration with a preset effect.
311   vibrator.startVibration({
312     type: 'preset',
313     effectId: 'haptic.clock.timer',
314     count: 1,
315   }, {
316     id: 0,
317     usage: 'alarm'
318   }, (error: BusinessError) => {
319     if (error) {
320       console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
321       return;
322     }
323     console.info('Succeed in starting vibration');
324   });
325 } catch (err) {
326   let e: BusinessError = err as BusinessError;
327   console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
328 }
329 
330 try {
331   // Stop vibration in VIBRATOR_STOP_MODE_PRESET mode.
332   vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, (error: BusinessError) => {
333     if (error) {
334       console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
335       return;
336     }
337     console.info('Succeed in stopping vibration');
338   })
339 } catch (err) {
340   let e: BusinessError = err as BusinessError;
341   console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
342 }
343 ```
344 
345 ## vibrator.stopVibration<sup>9+</sup>
346 
347 stopVibration(stopMode: VibratorStopMode): Promise&lt;void&gt;
348 
349 Stops vibration in the specified mode. This API uses a promise to return the result.
350 
351 **Required permissions**: ohos.permission.VIBRATE
352 
353 **System capability**: SystemCapability.Sensors.MiscDevice
354 
355 **Parameters**
356 
357 | Name  | Type                                 | Mandatory| Description                    |
358 | -------- | ------------------------------------- | ---- | ------------------------ |
359 | 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).|
360 
361 **Return value**
362 
363 | Type               | Description                                  |
364 | ------------------- | -------------------------------------- |
365 | Promise&lt;void&gt; | Promise that returns no value.|
366 
367 **Example**
368 
369 Stop fixed-duration vibration.
370 
371 ```ts
372 import vibrator from '@ohos.vibrator';
373 import { BusinessError } from '@ohos.base';
374 
375 try {
376   // Start vibration at a fixed duration.
377   vibrator.startVibration({
378     type: 'time',
379     duration: 1000,
380   }, {
381     id: 0,
382     usage: 'alarm'
383   }).then(() => {
384     console.info('Succeed in starting vibration');
385   }, (error: BusinessError) => {
386     console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
387   });
388 } catch (err) {
389   let e: BusinessError = err as BusinessError;
390   console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
391 }
392 
393 try {
394   // Stop vibration in VIBRATOR_STOP_MODE_TIME mode.
395   vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME).then(() => {
396     console.info('Succeed in stopping vibration');
397   }, (error: BusinessError) => {
398     console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
399   });
400 } catch (err) {
401   let e: BusinessError = err as BusinessError;
402   console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
403 }
404 ```
405 
406 Stop preset vibration.
407 
408 ```ts
409 import vibrator from '@ohos.vibrator';
410 import { BusinessError } from '@ohos.base';
411 
412 try {
413   // Start vibration with a preset effect.
414   vibrator.startVibration({
415     type: 'preset',
416     effectId: 'haptic.clock.timer',
417     count: 1,
418   }, {
419     id: 0,
420     usage: 'alarm'
421   }).then(() => {
422     console.info('Succeed in starting vibration');
423   }, (error: BusinessError) => {
424     console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
425   });
426 } catch (err) {
427   let e: BusinessError = err as BusinessError;
428   console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
429 }
430 
431 try {
432   // Stop vibration in VIBRATOR_STOP_MODE_PRESET mode.
433   vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => {
434     console.info('Succeed in stopping vibration');
435   }, (error: BusinessError) => {
436     console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
437   });
438 } catch (err) {
439   let e: BusinessError = err as BusinessError;
440   console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
441 }
442 ```
443 
444 ## vibrator.stopVibration<sup>10+</sup>
445 
446 stopVibration(callback: AsyncCallback&lt;void&gt;): void
447 
448 Stops vibration in all modes. This API uses an asynchronous callback to return the result.
449 
450 **Required permissions**: ohos.permission.VIBRATE
451 
452 **System capability**: SystemCapability.Sensors.MiscDevice
453 
454 **Parameters**
455 
456 | Name  | Type                     | Mandatory| Description                                                        |
457 | -------- | ------------------------- | ---- | ------------------------------------------------------------ |
458 | 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.|
459 
460 **Example**
461 
462 ```ts
463 import vibrator from '@ohos.vibrator';
464 import { BusinessError } from '@ohos.base';
465 
466 try {
467   // Stop vibration in all modes.
468   vibrator.stopVibration((error: BusinessError) => {
469     if (error) {
470       console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
471       return;
472     }
473     console.info('Succeed in stopping vibration');
474   })
475 } catch (error) {
476   let e: BusinessError = error as BusinessError;
477   console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
478 }
479 ```
480 
481 ## vibrator.stopVibration<sup>10+</sup>
482 
483 stopVibration(): Promise&lt;void&gt;
484 
485 Stops vibration in all modes. This API uses a promise to return the result.
486 
487 **Required permissions**: ohos.permission.VIBRATE
488 
489 **System capability**: SystemCapability.Sensors.MiscDevice
490 
491 **Return value**
492 
493 | Type               | Description         |
494 | ------------------- | ------------- |
495 | Promise&lt;void&gt; | Promise that returns no value.|
496 
497 **Example**
498 
499 ```ts
500 import vibrator from '@ohos.vibrator';
501 import { BusinessError } from '@ohos.base';
502 
503 try {
504   // Stop vibration in all modes.
505   vibrator.stopVibration().then(() => {
506     console.info('Succeed in stopping vibration');
507   }, (error: BusinessError) => {
508     console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
509   });
510 } catch (error) {
511   let e: BusinessError = error as BusinessError;
512   console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
513 }
514 ```
515 
516 ## vibrator.isSupportEffect<sup>10+</sup>
517 
518 isSupportEffect(effectId: string, callback: AsyncCallback&lt;boolean&gt;): void
519 
520 Checks whether an effect ID is supported. This API uses an asynchronous callback to return the result.
521 
522 **System capability**: SystemCapability.Sensors.MiscDevice
523 
524 **Parameters**
525 
526 | Name  | Type                        | Mandatory| Description                                                  |
527 | -------- | ---------------------------- | ---- | ------------------------------------------------------ |
528 | effectId | string                       | Yes  | Vibration effect ID.                                          |
529 | 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.|
530 
531 **Example**
532 
533 ```ts
534 import vibrator from '@ohos.vibrator';
535 import { BusinessError } from '@ohos.base';
536 
537 try {
538   // Check whether 'haptic.clock.timer' is supported.
539   vibrator.isSupportEffect('haptic.clock.timer', (err: BusinessError, state: boolean) => {
540     if (err) {
541       console.error(`Failed to query effect. Code: ${err.code}, message: ${err.message}`);
542       return;
543     }
544     console.info('Succeed in querying effect');
545     if (state) {
546       try {
547         // To use startVibration, you must configure the ohos.permission.VIBRATE permission.
548         vibrator.startVibration({
549           type: 'preset',
550           effectId: 'haptic.clock.timer',
551           count: 1,
552         }, {
553           usage: 'unknown'
554         }, (error: BusinessError) => {
555           if (error) {
556             console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
557           } else {
558             console.info('Succeed in starting vibration');
559           }
560         });
561       } catch (error) {
562         let e: BusinessError = error as BusinessError;
563         console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
564       }
565     }
566   })
567 } catch (error) {
568   let e: BusinessError = error as BusinessError;
569   console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
570 }
571 ```
572 
573 ## vibrator.isSupportEffect<sup>10+</sup>
574 
575 isSupportEffect(effectId: string): Promise&lt;boolean&gt;
576 
577 Checks whether an effect ID is supported. This API uses a promise to return the result.
578 
579 **System capability**: SystemCapability.Sensors.MiscDevice
580 
581 **Parameters**
582 
583 | Name  | Type  | Mandatory| Description        |
584 | -------- | ------ | ---- | ------------ |
585 | effectId | string | Yes  | Vibration effect ID.|
586 
587 **Return value**
588 
589 | Type                  | Description                                                     |
590 | ---------------------- | --------------------------------------------------------- |
591 | Promise&lt;boolean&gt; | Promise that returns the result. The value **true** means that the effect ID is supported, and **false** means the opposite.|
592 
593 **Example**
594 
595 ```ts
596 import vibrator from '@ohos.vibrator';
597 import { BusinessError } from '@ohos.base';
598 
599 try {
600   // Check whether 'haptic.clock.timer' is supported.
601   vibrator.isSupportEffect('haptic.clock.timer').then((state: boolean) => {
602     console.info(`The query result is ${state}`);
603     if (state) {
604       try {
605         vibrator.startVibration({
606           type: 'preset',
607           effectId: 'haptic.clock.timer',
608           count: 1,
609         }, {
610           usage: 'unknown'
611         }).then(() => {
612           console.info('Succeed in starting vibration');
613         }).catch((error: BusinessError) => {
614           console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
615         });
616       } catch (error) {
617         let e: BusinessError = error as BusinessError;
618         console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
619       }
620     }
621   }, (error: BusinessError) => {
622     console.error(`Failed to query effect. Code: ${error.code}, message: ${error.message}`);
623   })
624 } catch (error) {
625   let e: BusinessError = error as BusinessError;
626   console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
627 }
628 ```
629 
630 ## EffectId
631 
632 Enumerates the preset vibration effect IDs.
633 
634 **System capability**: SystemCapability.Sensors.MiscDevice
635 
636 | Name              | Value                  | Description                            |
637 | ------------------ | -------------------- | -------------------------------- |
638 | EFFECT_CLOCK_TIMER | "haptic.clock.timer" | Vibration effect when a user adjusts the timer.|
639 
640 
641 ## VibratorStopMode
642 
643 Enumerates the modes available to stop the vibration.
644 
645 **System capability**: SystemCapability.Sensors.MiscDevice
646 
647 | Name                     | Value      | Description                          |
648 | ------------------------- | -------- | ------------------------------ |
649 | VIBRATOR_STOP_MODE_TIME   | "time"   | The vibration to stop is in **duration** mode.|
650 | VIBRATOR_STOP_MODE_PRESET | "preset" | The vibration to stop is in **EffectId** mode.|
651 
652 ## VibrateEffect<sup>9+</sup>
653 
654 Describes the vibration effect.
655 
656 **System capability**: SystemCapability.Sensors.MiscDevice
657 
658 | Type                            | Description                          |
659 | -------------------------------- | ------------------------------ |
660 | [VibrateTime](#vibratetime9) | Vibration with the specified duration.|
661 | [VibratePreset](#vibratepreset9) | Vibration with a preset effect.|
662 | [VibrateFromFile](#vibratefromfile10) | Vibration according to a custom vibration configuration file.|
663 
664 ## VibrateTime<sup>9+</sup>
665 
666 Describes the fixed-duration vibration.
667 
668 **System capability**: SystemCapability.Sensors.MiscDevice
669 
670 | Name    | Type   | Mandatory| Description                          |
671 | -------- | ------ | ----- | ------------------------------ |
672 | type     | string |  Yes  | The value **time** means vibration with the specified duration.|
673 | duration | number |  Yes  | Vibration duration, in ms.        |
674 
675 ## VibratePreset<sup>9+</sup>
676 
677 Describes the preset vibration.
678 
679 **System capability**: SystemCapability.Sensors.MiscDevice
680 
681 | Name    | Type     | Mandatory| Description                          |
682 | -------- | -------- | ---- |------------------------------ |
683 | type     | string   |  Yes | The value **preset** means vibration with the specified effect.|
684 | effectId | string   |  Yes | Preset vibration effect ID.            |
685 | count    | number   |  Yes | Number of vibrations to repeat.              |
686 
687 ## VibrateFromFile<sup>10+</sup>
688 
689 Describes 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](../errorcodes/errorcode-universal.md) is returned.
690 
691 **System capability**: SystemCapability.Sensors.MiscDevice
692 
693 | Name    | Type      | Mandatory| Description                          |
694 | -------- | --------  | ---- | ------------------------------ |
695 | type     | string    |  Yes | The value **file** means vibration according to a vibration configuration file.|
696 | hapticFd | [HapticFileDescriptor](#hapticfiledescriptor10)<sup>10+</sup> | Yes| File descriptor (FD) of the vibration configuration file.|
697 
698 ## HapticFileDescriptor<sup>10+</sup>
699 
700 Describes 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](js-apis-file-fs.md#fsopen) or from the HAP resource through the [resource management API](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/vibrator-guidelines.md#custom-vibration).
701 
702 **System capability**: SystemCapability.Sensors.MiscDevice
703 
704 | Name    | Type     |  Mandatory | Description                          |
705 | -------- | -------- |--------| ------------------------------|
706 | fd       | number   |  Yes   | FD of the custom vibration configuration file.               |
707 | 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.|
708 | 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.|
709 
710 ## VibrateAttribute<sup>9+</sup>
711 
712 Describes the vibration attribute.
713 
714 **System capability**: SystemCapability.Sensors.MiscDevice
715 
716 | Name | Type| Mandatory| Description          |
717 | ----- | ------ | ---- | -------------- |
718 | id    | number      |  No| Vibrator ID. The default value is **0**.   |
719 | usage | [Usage](#usage) | Yes| Vibration scenario.|
720 
721 ## Usage<sup>9+</sup>
722 
723 Enumerates the vibration scenarios.
724 
725 **System capability**: SystemCapability.Sensors.MiscDevice
726 
727 | Name            | Type  | Description                          |
728 | ---------------- | ------ | ------------------------------ |
729 | unknown          | string | Unknown scenario, with the lowest priority.|
730 | alarm            | string | Vibration for alarms.          |
731 | ring             | string | Vibration for incoming calls.          |
732 | notification     | string | Vibration for notifications.          |
733 | communication    | string | Vibration for communication.          |
734 | touch            | string | Touch vibration scenario.          |
735 | media            | string | Multimedia vibration scenario.        |
736 | physicalFeedback | string | Physical feedback vibration scenario.      |
737 | simulateReality  | string | Simulated reality vibration scenario.      |
738 
739 ## vibrator.vibrate<sup>(deprecated)</sup>
740 
741 vibrate(duration: number): Promise&lt;void&gt;
742 
743 Triggers vibration with the specified duration. This API uses a promise to return the result.
744 
745 This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9-1)<sup>9+</sup> instead.
746 
747 **Required permissions**: ohos.permission.VIBRATE
748 
749 **System capability**: SystemCapability.Sensors.MiscDevice
750 
751 **Parameters**
752 
753 | Name  | Type  | Mandatory| Description                  |
754 | -------- | ------ | ---- | ---------------------- |
755 | duration | number | Yes  | Vibration duration, in ms.|
756 
757 **Return value**
758 
759 | Type               | Description                                  |
760 | ------------------- | -------------------------------------- |
761 | Promise&lt;void&gt; | Promise that returns no value.|
762 
763 **Example**
764 
765 ```ts
766 import vibrator from '@ohos.vibrator';
767 import { BusinessError } from '@ohos.base';
768 
769 vibrator.vibrate(1000).then(() => {
770   console.info('Succeed in vibrating');
771 }, (error: BusinessError) => {
772   console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
773 });
774 ```
775 
776 ## vibrator.vibrate<sup>(deprecated)</sup>
777 
778 vibrate(duration: number, callback?: AsyncCallback&lt;void&gt;): void
779 
780 Triggers vibration with the specified duration. This API uses an asynchronous callback to return the result.
781 
782 This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9)<sup>9+</sup> instead.
783 
784 **Required permissions**: ohos.permission.VIBRATE
785 
786 **System capability**: SystemCapability.Sensors.MiscDevice
787 
788 **Parameters**
789 
790 | Name  | Type                     | Mandatory| Description                                                      |
791 | -------- | ------------------------- | ---- | ---------------------------------------------------------- |
792 | duration | number                    | Yes  | Vibration duration, in ms.                                    |
793 | 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.|
794 
795 **Example**
796 
797 ```ts
798 import vibrator from '@ohos.vibrator';
799 import { BusinessError } from '@ohos.base';
800 
801 vibrator.vibrate(1000, (error: BusinessError) => {
802   if (error) {
803     console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
804   } else {
805     console.info('Succeed in vibrating');
806   }
807 })
808 ```
809 
810 
811 ## vibrator.vibrate<sup>(deprecated)</sup>
812 
813 vibrate(effectId: EffectId): Promise&lt;void&gt;
814 
815 Triggers vibration with the specified effect. This API uses a promise to return the result.
816 
817 This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9-1)<sup>9+</sup> instead.
818 
819 **Required permissions**: ohos.permission.VIBRATE
820 
821 **System capability**: SystemCapability.Sensors.MiscDevice
822 
823 **Parameters**
824 
825 | Name  | Type                 | Mandatory| Description              |
826 | -------- | --------------------- | ---- | ------------------ |
827 | effectId | [EffectId](#effectid) | Yes  | Preset vibration effect ID.|
828 
829 **Return value**
830 
831 | Type               | Description                                  |
832 | ------------------- | -------------------------------------- |
833 | Promise&lt;void&gt; | Promise that returns no value.|
834 
835 **Example**
836 
837 ```ts
838 import vibrator from '@ohos.vibrator';
839 import { BusinessError } from '@ohos.base';
840 
841 vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(() => {
842   console.info('Succeed in vibrating');
843 }, (error: BusinessError) => {
844   console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
845 });
846 ```
847 
848 
849 ## vibrator.vibrate<sup>(deprecated)</sup>
850 
851 vibrate(effectId: EffectId, callback?: AsyncCallback&lt;void&gt;): void
852 
853 Triggers vibration with the specified effect. This API uses an asynchronous callback to return the result.
854 
855 This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9)<sup>9+</sup> instead.
856 
857 **Required permissions**: ohos.permission.VIBRATE
858 
859 **System capability**: SystemCapability.Sensors.MiscDevice
860 
861 **Parameters**
862 
863 | Name  | Type                     | Mandatory| Description                                                      |
864 | -------- | ------------------------- | ---- | ---------------------------------------------------------- |
865 | effectId | [EffectId](#effectid)     | Yes  | Preset vibration effect ID.                                        |
866 | 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.|
867 
868 **Example**
869 
870 ```ts
871 import vibrator from '@ohos.vibrator';
872 import { BusinessError } from '@ohos.base';
873 
874 vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => {
875   if (error) {
876     console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
877   } else {
878     console.info('Succeed in vibrating');
879   }
880 })
881 ```
882 
883 ## vibrator.stop<sup>(deprecated)</sup>
884 
885 stop(stopMode: VibratorStopMode): Promise&lt;void&gt;
886 
887 Stops vibration in the specified mode. This API uses a promise to return the result.
888 
889 This API is deprecated since API version 9. You are advised to use [vibrator.stopVibration](#vibratorstopvibration9-1)<sup>9+</sup> instead.
890 
891 **Required permissions**: ohos.permission.VIBRATE
892 
893 **System capability**: SystemCapability.Sensors.MiscDevice
894 
895 **Parameters**
896 
897 | Name  | Type                                 | Mandatory| Description                    |
898 | -------- | ------------------------------------- | ---- | ------------------------ |
899 | stopMode | [VibratorStopMode](#vibratorstopmode) | Yes  | Mode to stop the vibration.|
900 
901 **Return value**
902 
903 | Type               | Description                                  |
904 | ------------------- | -------------------------------------- |
905 | Promise&lt;void&gt; | Promise that returns no value.|
906 
907 **Example**
908 
909 ```ts
910 import vibrator from '@ohos.vibrator';
911 import { BusinessError } from '@ohos.base';
912 
913 // Start vibration based on the specified effect ID.
914 vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => {
915   if (error) {
916     console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
917   } else {
918     console.info('Succeed in vibrating');
919   }
920 })
921 // Stop vibration in VIBRATOR_STOP_MODE_PRESET mode.
922 vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => {
923   console.info('Succeed in stopping');
924 }, (error: BusinessError) => {
925   console.error(`Failed to stop. Code: ${error.code}, message: ${error.message}`);
926 });
927 ```
928 
929 
930 ## vibrator.stop<sup>(deprecated)</sup>
931 
932 stop(stopMode: VibratorStopMode, callback?: AsyncCallback&lt;void&gt;): void
933 
934 Stops vibration in the specified mode. This API uses an asynchronous callback to return the result.
935 
936 This API is deprecated since API version 9. You are advised to use [vibrator.stopVibration](#vibratorstopvibration9)<sup>9+</sup> instead.
937 
938 **Required permissions**: ohos.permission.VIBRATE
939 
940 **System capability**: SystemCapability.Sensors.MiscDevice
941 
942 **Parameters**
943 
944 | Name  | Type                                 | Mandatory| Description                                                        |
945 | -------- | ------------------------------------- | ---- | ------------------------------------------------------------ |
946 | stopMode | [VibratorStopMode](#vibratorstopmode) | Yes  | Mode to stop the vibration.                                    |
947 | 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.|
948 
949 **Example**
950 
951 ```ts
952 import vibrator from '@ohos.vibrator';
953 import { BusinessError } from '@ohos.base';
954 
955 // Start vibration based on the specified effect ID.
956 vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => {
957   if (error) {
958     console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
959   } else {
960     console.info('Succeed in vibrating');
961   }
962 })
963 // Stop vibration in VIBRATOR_STOP_MODE_PRESET mode.
964 vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, (error: BusinessError) => {
965   if (error) {
966     console.error(`Failed to stop. Code: ${error.code}, message: ${error.message}`);
967   } else {
968     console.info('Succeed in stopping');
969   }
970 })
971 ```
972