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