1# @ohos.vibrator (振动)
2
3vibrator模块提供控制马达振动启、停的能力。
4
5> **说明:**
6>
7> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9
10## 导入模块
11
12```ts
13import { vibrator } from '@kit.SensorServiceKit';
14```
15
16## vibrator.startVibration<sup>9+</sup>
17
18startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: AsyncCallback&lt;void&gt;): void
19
20根据指定的振动效果和振动属性触发马达振动。使用callback异步回调。
21
22**需要权限**:ohos.permission.VIBRATE
23
24**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。
25
26**系统能力**:SystemCapability.Sensors.MiscDevice
27
28**参数**:
29
30| 参数名    | 类型                                   | 必填 | 说明                                                         |
31| --------- | -------------------------------------- | ---- | :----------------------------------------------------------- |
32| effect    | [VibrateEffect](#vibrateeffect9)       | 是   | 马达振动效果,支持三种:<br>1、[VibrateTime](#vibratetime9):按照指定持续时间触发马达振动;<br>2、[VibratePreset](#vibratepreset9):按照预置振动效果触发马达振动;<br>3、[VibrateFromFile](#vibratefromfile10):按照自定义振动配置文件触发马达振动。 |
33| attribute | [VibrateAttribute](#vibrateattribute9) | 是   | 马达振动属性。                                               |
34| callback  | AsyncCallback&lt;void&gt;              | 是   | 回调函数,当马达振动成功,err为undefined,否则为错误对象。   |
35
36**错误码**:
37
38以下错误码的详细介绍请参见[振动错误码](errorcode-vibrator.md)和[通用错误码](../errorcode-universal.md)。
39
40| 错误码ID | 错误信息                                                     |
41| -------- | ------------------------------------------------------------ |
42| 201      | Permission denied.                                           |
43| 401      | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. |
44| 801      | Capability not supported.                                    |
45| 14600101 | Device operation failed.                                     |
46
47**示例**:
48
49按照指定持续时间触发马达振动:
50
51```ts
52import { vibrator } from '@kit.SensorServiceKit';
53import { BusinessError } from '@kit.BasicServicesKit';
54
55try {
56  vibrator.startVibration({
57    type: 'time',
58    duration: 1000,
59  }, {
60    id: 0,
61    usage: 'alarm'
62  }, (error: BusinessError) => {
63    if (error) {
64      console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
65      return;
66    }
67    console.info('Succeed in starting vibration');
68  });
69} catch (err) {
70  let e: BusinessError = err as BusinessError;
71  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
72}
73```
74
75按照预置振动效果触发马达振动:
76
77```ts
78import { vibrator } from '@kit.SensorServiceKit';
79import { BusinessError } from '@kit.BasicServicesKit';
80
81try {
82  vibrator.startVibration({
83    type: 'preset',
84    effectId: 'haptic.clock.timer',
85    count: 1,
86  }, {
87    id: 0,
88    usage: 'alarm'
89  }, (error: BusinessError) => {
90    if (error) {
91      console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
92      return;
93    }
94    console.info('Succeed in starting vibration');
95  });
96} catch (err) {
97  let e: BusinessError = err as BusinessError;
98  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
99}
100```
101
102按照自定义振动配置文件触发马达振动:
103
104```ts
105import { vibrator } from '@kit.SensorServiceKit';
106import { resourceManager } from '@kit.LocalizationKit';
107import { BusinessError } from '@kit.BasicServicesKit';
108
109const fileName: string = 'xxx.json';
110
111let rawFd: resourceManager.RawFileDescriptor = getContext().resourceManager.getRawFdSync(fileName);
112
113try {
114  vibrator.startVibration({
115    type: "file",
116    hapticFd: { fd: rawFd.fd, offset: rawFd.offset, length: rawFd.length }
117  }, {
118    id: 0,
119    usage: 'alarm'
120  }, (error: BusinessError) => {
121    if (error) {
122      console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
123      return;
124    }
125    console.info('Succeed in starting vibration');
126  });
127} catch (err) {
128  let e: BusinessError = err as BusinessError;
129  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
130}
131
132getContext().resourceManager.closeRawFdSync(fileName);
133```
134
135## vibrator.startVibration<sup>9+</sup>
136
137startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise&lt;void&gt;
138
139根据指定的振动效果和振动属性触发马达振动。使用promise异步回调。
140
141**需要权限**:ohos.permission.VIBRATE
142
143**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。
144
145**系统能力**:SystemCapability.Sensors.MiscDevice
146
147**参数**:
148
149| 参数名    | 类型                                   | 必填 | 说明                                                         |
150| --------- | -------------------------------------- | ---- | ------------------------------------------------------------ |
151| effect    | [VibrateEffect](#vibrateeffect9)       | 是   | 马达振动效果,支持三种:<br>1、[VibrateTime](#vibratetime9):按照指定持续时间触发马达振动;<br>2、[VibratePreset](#vibratepreset9):按照预置振动效果触发马达振动;<br>3、[VibrateFromFile](#vibratefromfile10):按照自定义振动配置文件触发马达振动。 |
152| attribute | [VibrateAttribute](#vibrateattribute9) | 是   | 马达振动属性。                                               |
153
154**返回值**:
155
156| 类型                | 说明                                   |
157| ------------------- | -------------------------------------- |
158| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
159
160**错误码**:
161
162以下错误码的详细介绍请参见[振动错误码](errorcode-vibrator.md)和[通用错误码](../errorcode-universal.md)。
163
164| 错误码ID | 错误信息                                                     |
165| -------- | ------------------------------------------------------------ |
166| 201      | Permission denied.                                           |
167| 401      | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. |
168| 801      | Capability not supported.                                    |
169| 14600101 | Device operation failed.                                     |
170
171**示例**:
172
173按照指定持续时间触发马达振动:
174
175```ts
176import { vibrator } from '@kit.SensorServiceKit';
177import { BusinessError } from '@kit.BasicServicesKit';
178
179try {
180  vibrator.startVibration({
181    type: 'time',
182    duration: 1000
183  }, {
184    id: 0,
185    usage: 'alarm'
186  }).then(() => {
187    console.info('Succeed in starting vibration');
188  }, (error: BusinessError) => {
189    console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
190  });
191} catch (err) {
192  let e: BusinessError = err as BusinessError;
193  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
194}
195```
196
197按照预置振动效果触发马达振动:
198
199```ts
200import { vibrator } from '@kit.SensorServiceKit';
201import { BusinessError } from '@kit.BasicServicesKit';
202
203try {
204  vibrator.startVibration({
205    type: 'preset',
206    effectId: 'haptic.clock.timer',
207    count: 1,
208  }, {
209    id: 0,
210    usage: 'alarm'
211  }).then(() => {
212    console.info('Succeed in starting vibration');
213  }, (error: BusinessError) => {
214    console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
215  });
216} catch (err) {
217  let e: BusinessError = err as BusinessError;
218  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
219}
220```
221
222按照自定义振动配置文件触发马达振动:
223
224```ts
225import { vibrator } from '@kit.SensorServiceKit';
226import { resourceManager } from '@kit.LocalizationKit';
227import { BusinessError } from '@kit.BasicServicesKit';
228
229const fileName: string = 'xxx.json';
230
231let rawFd: resourceManager.RawFileDescriptor = getContext().resourceManager.getRawFdSync(fileName);
232
233try {
234  vibrator.startVibration({
235    type: "file",
236    hapticFd: { fd: rawFd.fd, offset: rawFd.offset, length: rawFd.length }
237  }, {
238    id: 0,
239    usage: 'alarm'
240  }).then(() => {
241    console.info('Succeed in starting vibration');
242  }, (error: BusinessError) => {
243    console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
244  });
245} catch (err) {
246  let e: BusinessError = err as BusinessError;
247  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
248}
249
250getContext().resourceManager.closeRawFdSync(fileName);
251```
252
253## vibrator.stopVibration<sup>9+</sup>
254
255stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback&lt;void&gt;): void
256
257按照指定模式停止马达振动。使用callback异步回调。
258
259**需要权限**:ohos.permission.VIBRATE
260
261**系统能力**:SystemCapability.Sensors.MiscDevice
262
263**参数**:
264
265| 参数名   | 类型                                  | 必填 | 说明                                                         |
266| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ |
267| stopMode | [VibratorStopMode](#vibratorstopmode) | 是   | 指定的停止振动模式,支持两种:<br>VIBRATOR_STOP_MODE_TIME:停止固定时长振动;<br>VIBRATOR_STOP_MODE_PRESET:停止预置振动。<br>此接口无法停止自定义振动,请使用[vibrator.stopVibration<sup>10+</sup>](#vibratorstopvibration10)。                                  |
268| callback | AsyncCallback&lt;void&gt;             | 是   | 回调函数,当马达停止振动成功,err为undefined,否则为错误对象。 |
269
270**错误码**:
271
272以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
273
274| 错误码ID | 错误信息                                                     |
275| -------- | ------------------------------------------------------------ |
276| 201      | Permission denied.                                           |
277| 401      | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. |
278
279**示例**:
280
281停止固定时长振动:
282
283```ts
284import { vibrator } from '@kit.SensorServiceKit';
285import { BusinessError } from '@kit.BasicServicesKit';
286
287try {
288  // 按照固定时长振动
289  vibrator.startVibration({
290    type: 'time',
291    duration: 1000,
292  }, {
293    id: 0,
294    usage: 'alarm'
295  }, (error: BusinessError) => {
296    if (error) {
297      console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
298      return;
299    }
300    console.info('Succeed in starting vibration');
301  });
302} catch (err) {
303  let e: BusinessError = err as BusinessError;
304  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
305}
306
307try {
308  // 按照VIBRATOR_STOP_MODE_TIME模式停止振动
309  vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, (error: BusinessError) => {
310    if (error) {
311      console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
312      return;
313    }
314    console.info('Succeed in stopping vibration');
315  })
316} catch (err) {
317  let e: BusinessError = err as BusinessError;
318  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
319}
320```
321
322停止预置振动:
323
324```ts
325import { vibrator } from '@kit.SensorServiceKit';
326import { BusinessError } from '@kit.BasicServicesKit';
327
328try {
329  // 按照预置效果振动
330  vibrator.startVibration({
331    type: 'preset',
332    effectId: 'haptic.clock.timer',
333    count: 1,
334  }, {
335    id: 0,
336    usage: 'alarm'
337  }, (error: BusinessError) => {
338    if (error) {
339      console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
340      return;
341    }
342    console.info('Succeed in starting vibration');
343  });
344} catch (err) {
345  let e: BusinessError = err as BusinessError;
346  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
347}
348
349try {
350  // 按照VIBRATOR_STOP_MODE_PRESET模式停止振动
351  vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, (error: BusinessError) => {
352    if (error) {
353      console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
354      return;
355    }
356    console.info('Succeed in stopping vibration');
357  })
358} catch (err) {
359  let e: BusinessError = err as BusinessError;
360  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
361}
362```
363
364## vibrator.stopVibration<sup>9+</sup>
365
366stopVibration(stopMode: VibratorStopMode): Promise&lt;void&gt;
367
368按照指定模式停止马达的振动。使用promise异步回调。
369
370**需要权限**:ohos.permission.VIBRATE
371
372**系统能力**:SystemCapability.Sensors.MiscDevice
373
374**参数**:
375
376| 参数名   | 类型                                  | 必填 | 说明                     |
377| -------- | ------------------------------------- | ---- | ------------------------ |
378| stopMode | [VibratorStopMode](#vibratorstopmode) | 是   | 指定的停止振动模式,支持两种:<br>VIBRATOR_STOP_MODE_TIME:停止固定时长振动;<br>VIBRATOR_STOP_MODE_PRESET:停止预置振动。<br>此接口无法停止自定义振动,请使用[vibrator.stopVibration<sup>10+</sup>](#vibratorstopvibration10-1)。 |
379
380**返回值**:
381
382| 类型                | 说明                                   |
383| ------------------- | -------------------------------------- |
384| Promise&lt;void&gt; | Promise对象。 |
385
386**错误码**:
387
388以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
389
390| 错误码ID | 错误信息                                                     |
391| -------- | ------------------------------------------------------------ |
392| 201      | Permission denied.                                           |
393| 401      | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. |
394
395**示例**:
396
397停止固定时长振动:
398
399```ts
400import { vibrator } from '@kit.SensorServiceKit';
401import { BusinessError } from '@kit.BasicServicesKit';
402
403try {
404  // 按照固定时长振动
405  vibrator.startVibration({
406    type: 'time',
407    duration: 1000,
408  }, {
409    id: 0,
410    usage: 'alarm'
411  }).then(() => {
412    console.info('Succeed in starting vibration');
413  }, (error: BusinessError) => {
414    console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
415  });
416} catch (err) {
417  let e: BusinessError = err as BusinessError;
418  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
419}
420
421try {
422  // 按照VIBRATOR_STOP_MODE_TIME模式停止振动
423  vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME).then(() => {
424    console.info('Succeed in stopping vibration');
425  }, (error: BusinessError) => {
426    console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
427  });
428} catch (err) {
429  let e: BusinessError = err as BusinessError;
430  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
431}
432```
433
434停止预置振动:
435
436```ts
437import { vibrator } from '@kit.SensorServiceKit';
438import { BusinessError } from '@kit.BasicServicesKit';
439
440try {
441  // 按照预置效果振动
442  vibrator.startVibration({
443    type: 'preset',
444    effectId: 'haptic.clock.timer',
445    count: 1,
446  }, {
447    id: 0,
448    usage: 'alarm'
449  }).then(() => {
450    console.info('Succeed in starting vibration');
451  }, (error: BusinessError) => {
452    console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
453  });
454} catch (err) {
455  let e: BusinessError = err as BusinessError;
456  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
457}
458
459try {
460  // 按照VIBRATOR_STOP_MODE_PRESET模式停止振动
461  vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => {
462    console.info('Succeed in stopping vibration');
463  }, (error: BusinessError) => {
464    console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
465  });
466} catch (err) {
467  let e: BusinessError = err as BusinessError;
468  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
469}
470```
471
472## vibrator.stopVibration<sup>10+</sup>
473
474stopVibration(callback: AsyncCallback&lt;void&gt;): void
475
476停止所有模式的马达振动。使用callback异步回调。
477
478**需要权限**:ohos.permission.VIBRATE
479
480**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。
481
482**系统能力**:SystemCapability.Sensors.MiscDevice
483
484**参数**:
485
486| 参数名   | 类型                      | 必填 | 说明                                                         |
487| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
488| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数,当马达停止振动成功,err为undefined,否则为错误对象。 |
489
490**错误码**:
491
492以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
493
494| 错误码ID | 错误信息           |
495| -------- | ------------------ |
496| 201      | Permission denied. |
497
498**示例**:
499
500```ts
501import { vibrator } from '@kit.SensorServiceKit';
502import { BusinessError } from '@kit.BasicServicesKit';
503
504try {
505  // 停止所有模式的马达振动
506  vibrator.stopVibration((error: BusinessError) => {
507    if (error) {
508      console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
509      return;
510    }
511    console.info('Succeed in stopping vibration');
512  })
513} catch (error) {
514  let e: BusinessError = error as BusinessError;
515  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
516}
517```
518
519## vibrator.stopVibration<sup>10+</sup>
520
521stopVibration(): Promise&lt;void&gt;
522
523停止所有模式的马达振动。使用promise异步回调。
524
525**需要权限**:ohos.permission.VIBRATE
526
527**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。
528
529**系统能力**:SystemCapability.Sensors.MiscDevice
530
531**返回值**:
532
533| 类型                | 说明          |
534| ------------------- | ------------- |
535| Promise&lt;void&gt; | Promise对象。 |
536
537**错误码**:
538
539以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
540
541| 错误码ID | 错误信息           |
542| -------- | ------------------ |
543| 201      | Permission denied. |
544
545**示例**:
546
547```ts
548import { vibrator } from '@kit.SensorServiceKit';
549import { BusinessError } from '@kit.BasicServicesKit';
550
551try {
552  // 停止所有模式的马达振动
553  vibrator.stopVibration().then(() => {
554    console.info('Succeed in stopping vibration');
555  }, (error: BusinessError) => {
556    console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
557  });
558} catch (error) {
559  let e: BusinessError = error as BusinessError;
560  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
561}
562```
563
564## vibrator.stopVibrationSync<sup>12+</sup>
565
566stopVibrationSync(): void
567
568停止任何形式的马达振动。
569
570**需要权限**:ohos.permission.VIBRATE
571
572**原子化服务API**:从API Version 12开始,该接口支持在原子化服务中使用。
573
574**系统能力**:SystemCapability.Sensors.MiscDevice
575
576**错误码**:
577
578以下错误码的详细介绍请参见[振动错误码](errorcode-vibrator.md)和[通用错误码](../errorcode-universal.md)。
579
580| 错误码ID | 错误信息                 |
581| -------- | ------------------------ |
582| 201      | Permission denied.       |
583| 14600101 | Device operation failed. |
584
585**示例**:
586
587```ts
588import { vibrator } from '@kit.SensorServiceKit';
589import { BusinessError } from '@kit.BasicServicesKit';
590
591try {
592  // 停止任何形式的马达振动
593    vibrator.stopVibrationSync()
594    console.info('Succeed in stopping vibration');
595} catch (error) {
596  let e: BusinessError = error as BusinessError;
597  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
598}
599```
600
601## vibrator.isSupportEffect<sup>10+</sup>
602
603isSupportEffect(effectId: string, callback: AsyncCallback&lt;boolean&gt;): void
604
605查询是否支持传入参数effectId。使用callback异步回调。
606
607**系统能力**:SystemCapability.Sensors.MiscDevice
608
609**参数**:
610
611| 参数名   | 类型                         | 必填 | 说明                                                   |
612| -------- | ---------------------------- | ---- | ------------------------------------------------------ |
613| effectId | string                       | 是   | 预置的振动效果。                                           |
614| callback | AsyncCallback&lt;boolean&gt; | 是   | 回调函数,当返回true则表示支持该effectId,否则不支持。 |
615
616**错误码**:
617
618以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
619
620| 错误码ID | 错误信息                                                     |
621| -------- | ------------------------------------------------------------ |
622| 201      | Permission denied.                                           |
623| 401      | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. |
624
625**示例**:
626
627```ts
628import { vibrator } from '@kit.SensorServiceKit';
629import { BusinessError } from '@kit.BasicServicesKit';
630
631try {
632  // 查询是否支持'haptic.clock.timer'
633  vibrator.isSupportEffect('haptic.clock.timer', (err: BusinessError, state: boolean) => {
634    if (err) {
635      console.error(`Failed to query effect. Code: ${err.code}, message: ${err.message}`);
636      return;
637    }
638    console.info('Succeed in querying effect');
639    if (state) {
640      try {
641        // 使用startVibration需要添加ohos.permission.VIBRATE权限
642        vibrator.startVibration({
643          type: 'preset',
644          effectId: 'haptic.clock.timer',
645          count: 1,
646        }, {
647          usage: 'unknown'
648        }, (error: BusinessError) => {
649          if (error) {
650            console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
651          } else {
652            console.info('Succeed in starting vibration');
653          }
654        });
655      } catch (error) {
656        let e: BusinessError = error as BusinessError;
657        console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
658      }
659    }
660  })
661} catch (error) {
662  let e: BusinessError = error as BusinessError;
663  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
664}
665```
666
667## vibrator.isSupportEffect<sup>10+</sup>
668
669isSupportEffect(effectId: string): Promise&lt;boolean&gt;
670
671查询是否支持传入参数effectId。使用promise异步回调。
672
673**系统能力**:SystemCapability.Sensors.MiscDevice
674
675**参数**:
676
677| 参数名   | 类型   | 必填 | 说明         |
678| -------- | ------ | ---- | ------------ |
679| effectId | string | 是   | 预置的振动效果。 |
680
681**返回值**:
682
683| 类型                   | 说明                                                      |
684| ---------------------- | --------------------------------------------------------- |
685| Promise&lt;boolean&gt; | Promise对象。当返回true则表示支持该effectId,否则不支持。 |
686
687**错误码**:
688
689以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
690
691| 错误码ID | 错误信息                                                     |
692| -------- | ------------------------------------------------------------ |
693| 201      | Permission denied.                                           |
694| 401      | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. |
695
696**示例**:
697
698```ts
699import { vibrator } from '@kit.SensorServiceKit';
700import { BusinessError } from '@kit.BasicServicesKit';
701
702try {
703  // 查询是否支持'haptic.clock.timer'
704  vibrator.isSupportEffect('haptic.clock.timer').then((state: boolean) => {
705    console.info(`The query result is ${state}`);
706    if (state) {
707      try {
708        vibrator.startVibration({
709          type: 'preset',
710          effectId: 'haptic.clock.timer',
711          count: 1,
712        }, {
713          usage: 'unknown'
714        }).then(() => {
715          console.info('Succeed in starting vibration');
716        }).catch((error: BusinessError) => {
717          console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
718        });
719      } catch (error) {
720        let e: BusinessError = error as BusinessError;
721        console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
722      }
723    }
724  }, (error: BusinessError) => {
725    console.error(`Failed to query effect. Code: ${error.code}, message: ${error.message}`);
726  })
727} catch (error) {
728  let e: BusinessError = error as BusinessError;
729  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
730}
731```
732
733## vibrator.isSupportEffectSync<sup>12+</sup>
734
735isSupportEffectSync(effectId: string): boolean
736
737查询是否支持预设的振动效果。
738
739**系统能力**:SystemCapability.Sensors.MiscDevice
740
741**参数**:
742
743| 参数名   | 类型   | 必填 | 说明                 |
744| -------- | ------ | ---- | -------------------- |
745| effectId | string | 是   | 是否预设的振动效果。 |
746
747**返回值**:
748
749| 类型    | 说明                                                   |
750| ------- | ------------------------------------------------------ |
751| boolean | 返回对象。当返回true则表示支持该effectId,否则不支持。 |
752
753**错误码**:
754
755以下错误码的详细介绍请参见[振动错误码](errorcode-vibrator.md)和[通用错误码](../errorcode-universal.md)。
756
757| 错误码ID | 错误信息                                                     |
758| -------- | ------------------------------------------------------------ |
759| 401      | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. |
760| 14600101 | Device operation failed.                                     |
761
762**示例**:
763
764```ts
765import { vibrator } from '@kit.SensorServiceKit';
766import { BusinessError } from '@kit.BasicServicesKit';
767
768try {
769    // 查询是否支持预设'haptic.clock.timer'
770    let ret = vibrator.isSupportEffectSync('haptic.clock.timer');
771    console.info(`The query result is ${ret}`);
772} catch (error) {
773    let e: BusinessError = error as BusinessError;
774    console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
775}
776```
777
778## vibrator.isHdHapticSupported<sup>12+</sup>
779
780isHdHapticSupported(): boolean
781
782查询是否支持高清振动。
783
784**系统能力**:SystemCapability.Sensors.MiscDevice
785
786**返回值**:
787
788| 类型    | 说明       |
789| ------- | ---------- |
790| boolean | 返回对象。 |
791
792**错误码**:
793
794以下错误码的详细介绍请参见[振动错误码](errorcode-vibrator.md)。
795
796| 错误码ID | 错误信息                 |
797| -------- | ------------------------ |
798| 14600101 | Device operation failed. |
799
800**示例**:
801
802```ts
803import { vibrator } from '@kit.SensorServiceKit';
804import { BusinessError } from '@kit.BasicServicesKit';
805
806try {
807    // 查询是否支持高清振动
808    let ret = vibrator.isHdHapticSupported();
809    console.info(`The query result is ${ret}`);
810} catch (error) {
811    let e: BusinessError = error as BusinessError;
812    console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
813}
814```
815
816## EffectId
817
818预置的振动效果。
819
820**系统能力**:SystemCapability.Sensors.MiscDevice
821
822| 名称               | 值                   | 说明                             |
823| ------------------ | -------------------- | -------------------------------- |
824| EFFECT_CLOCK_TIMER | 'haptic.clock.timer' | 描述用户调整计时器时的振动效果。|
825
826## HapticFeedback<sup>12+</sup>
827
828简单而通用的振动效果。
829
830**系统能力**:SystemCapability.Sensors.MiscDevice
831
832| 名称         | 值                    | 说明                         |
833| ------------ | --------------------- | ---------------------------- |
834| EFFECT_SOFT  | 'haptic.effect.soft'  | 较松散的振动效果,频率偏低。 |
835| EFFECT_HARD  | 'haptic.effect.hard'  | 较沉重的振动效果,频率居中。 |
836| EFFECT_SHARP | 'haptic.effect.sharp' | 较尖锐的振动效果,频率偏高。 |
837
838## VibratorStopMode
839
840停止振动的模式。
841
842**系统能力**:SystemCapability.Sensors.MiscDevice
843
844| 名称                      | 值       | 说明                           |
845| ------------------------- | -------- | ------------------------------ |
846| VIBRATOR_STOP_MODE_TIME   | 'time'   | 停止duration模式的振动。 |
847| VIBRATOR_STOP_MODE_PRESET | 'preset' | 停止预置EffectId的振动。|
848
849## VibrateEffect<sup>9+</sup>
850
851马达振动效果,支持以下三种。
852
853**系统能力**:SystemCapability.Sensors.MiscDevice
854
855| 类型                             | 说明                           |
856| -------------------------------- | ------------------------------ |
857| [VibrateTime](#vibratetime9) | 按照指定持续时间触发马达振动。<br/>**原子化服务API:** 从API Version 11开始,该接口支持在原子化服务中使用。 |
858| [VibratePreset](#vibratepreset9) | 按照预置振动类型触发马达振动。 |
859| [VibrateFromFile](#vibratefromfile10) | 按照自定义振动配置文件触发马达振动。 |
860
861## VibrateTime<sup>9+</sup>
862
863固定时长振动类型。
864
865**原子化服务API**:从API Version 11开始,该接口在支持原子化服务中使用。
866
867**系统能力**:SystemCapability.Sensors.MiscDevice
868
869| 名称     | 类型    | 必填 | 说明                           |
870| -------- | ------ | ----- | ------------------------------ |
871| type     | 'time' |  是   | 值为'time',按照指定持续时间触发马达振动。 |
872| duration | number |  是   | 马达持续振动时长, 单位ms。         |
873
874## VibratePreset<sup>9+</sup>
875
876预置振动类型。
877
878**系统能力**:SystemCapability.Sensors.MiscDevice
879
880| 名称     | 类型      | 必填 | 说明                           |
881| -------- | -------- | ---- |------------------------------ |
882| type     | 'preset' |  是  | 值为'preset',按照预置振动效果触发马达振动。 |
883| effectId | string   |  是  | 预置的振动效果ID。             |
884| count    | number   |  否  | 可选参数,振动的重复次数,默认值为1。 |
885| intensity<sup>12+</sup> | number | 否 | 可选参数,振动调节强度,范围为0到100,默认值为100。若振动效果不支持强度调节或设备不支持时,则按默认强度振动。 |
886
887## VibrateFromFile<sup>10+</sup>
888
889自定义振动类型,仅部分设备支持,当设备不支持此振动类型时,返回[设备不支持错误码](../errorcode-universal.md)。
890
891**系统能力**:SystemCapability.Sensors.MiscDevice
892
893| 名称     | 类型       | 必填 | 说明                           |
894| -------- | --------  | ---- | ------------------------------ |
895| type     | 'file' |  是  | 值为'file',按照振动配置文件触发马达振动。 |
896| hapticFd | [HapticFileDescriptor](#hapticfiledescriptor10)<sup>10+</sup> | 是 | 振动配置文件的描述符。|
897
898## HapticFileDescriptor<sup>10+</sup>
899
900自定义振动配置文件的描述符,必须确认资源文件可用,其参数可通过[文件管理API](../apis-core-file-kit/js-apis-file-fs.md#fsopen)从沙箱路径获取或者通过[资源管理API](../apis-localization-kit/js-apis-resource-manager.md#getrawfd9)从HAP资源获取。使用场景:振动序列被存储在一个文件中,需要根据偏移量和长度进行振动,振动序列存储格式,请参考[自定义振动格式](../../device/sensor/vibrator-guidelines.md#自定义振动)。
901
902**系统能力**:SystemCapability.Sensors.MiscDevice
903
904| 名称     | 类型      |  必填  | 说明                           |
905| -------- | -------- |--------| ------------------------------|
906| fd       | number   |  是    | 资源文件描述符。                |
907| offset   | number   |  否    | 距文件起始位置的偏移量,单位为字节,默认为文件起始位置,不可超出文件有效范围。|
908| length   | number   |  否    | 资源长度,单位为字节,默认值为从偏移位置至文件结尾的长度,不可超出文件有效范围。|
909
910## VibrateAttribute<sup>9+</sup>
911
912马达振动属性。
913
914**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。
915
916**系统能力**:SystemCapability.Sensors.MiscDevice
917
918| 名称  | 类型 | 必填 | 说明           |
919| ----- | ------ | ---- | -------------- |
920| id    | number      |  否 | 振动器id, 默认值为0。    |
921| usage | [Usage](#usage9) | 是 | 马达振动的使用场景。 |
922
923## Usage<sup>9+</sup>
924
925type Usage = 'unknown' | 'alarm' | 'ring' | 'notification' | 'communication' | 'touch' | 'media' | 'physicalFeedback' | 'simulateReality'
926
927振动使用场景。
928
929**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。
930
931**系统能力**:SystemCapability.Sensors.MiscDevice
932<!--RP1-->
933
934| 类型     | 说明                           |
935| ---------------- | ------------------------------ |
936| 'unknown'     | 没有明确使用场景,最低优先级,值固定为'unknown'字符串。 |
937| 'alarm'      | 用于警报场景,值固定为'alarm'字符串。 |
938| 'ring'         | 用于铃声场景,值固定为'ring'字符串。 |
939| 'notification' | 用于通知场景,值固定为'notification'字符串。 |
940| 'communication' | 用于通信场景,值固定为'communication'字符串。 |
941| 'touch'        | 用于触摸场景,值固定为'touch'字符串。 |
942| 'media'        | 用于多媒体场景,值固定为'media'字符串。 |
943| 'physicalFeedback' | 用于物理反馈场景,值固定为'physicalFeedback'字符串。 |
944| 'simulateReality' | 用于模拟现实场景,值固定为'simulateReality'字符串。 |
945<!--RP1End-->
946
947## vibrator.vibrate<sup>(deprecated)</sup>
948
949vibrate(duration: number): Promise&lt;void&gt;
950
951按照指定持续时间触发马达振动。
952
953从API version 9 开始不再维护,建议使用 [vibrator.startVibration](#vibratorstartvibration9-1)<sup>9+</sup>代替。
954
955**需要权限**:ohos.permission.VIBRATE
956
957**系统能力**:SystemCapability.Sensors.MiscDevice
958
959**参数**:
960
961| 参数名   | 类型   | 必填 | 说明                   |
962| -------- | ------ | ---- | ---------------------- |
963| duration | number | 是   | 马达振动时长, 单位ms。 |
964
965**返回值**:
966
967| 类型                | 说明                                   |
968| ------------------- | -------------------------------------- |
969| Promise&lt;void&gt; | Promise对象。 |
970
971**示例**:
972
973```ts
974import { vibrator } from '@kit.SensorServiceKit';
975import { BusinessError } from '@kit.BasicServicesKit';
976
977vibrator.vibrate(1000).then(() => {
978  console.info('Succeed in vibrating');
979}, (error: BusinessError) => {
980  console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
981});
982```
983
984## vibrator.vibrate<sup>(deprecated)</sup>
985
986vibrate(duration: number, callback?: AsyncCallback&lt;void&gt;): void
987
988按照指定持续时间触发马达振动。
989
990从API version 9 开始不再维护,建议使用 [vibrator.startVibration](#vibratorstartvibration9)<sup>9+</sup>代替。
991
992**需要权限**:ohos.permission.VIBRATE
993
994**系统能力**:SystemCapability.Sensors.MiscDevice
995
996**参数**:
997
998| 参数名   | 类型                      | 必填 | 说明                                                       |
999| -------- | ------------------------- | ---- | ---------------------------------------------------------- |
1000| duration | number                    | 是   | 马达振动时长, 单位ms。                                     |
1001| callback | AsyncCallback&lt;void&gt; | 否   | 回调函数,当马达振动成功,err为undefined,否则为错误对象。 |
1002
1003**示例**:
1004
1005```ts
1006import { vibrator } from '@kit.SensorServiceKit';
1007import { BusinessError } from '@kit.BasicServicesKit';
1008
1009vibrator.vibrate(1000, (error: BusinessError) => {
1010  if (error) {
1011    console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
1012  } else {
1013    console.info('Succeed in vibrating');
1014  }
1015})
1016```
1017
1018
1019## vibrator.vibrate<sup>(deprecated)</sup>
1020
1021vibrate(effectId: EffectId): Promise&lt;void&gt;
1022
1023按照预置振动效果触发马达振动。
1024
1025从API version 9 开始不再维护,建议使用 [vibrator.startVibration](#vibratorstartvibration9-1)<sup>9+</sup>代替。
1026
1027**需要权限**:ohos.permission.VIBRATE
1028
1029**系统能力**:SystemCapability.Sensors.MiscDevice
1030
1031**参数**:
1032
1033| 参数名   | 类型                  | 必填 | 说明               |
1034| -------- | --------------------- | ---- | ------------------ |
1035| effectId | [EffectId](#effectid) | 是   | 预置的振动效果ID。 |
1036
1037**返回值**:
1038
1039| 类型                | 说明                                   |
1040| ------------------- | -------------------------------------- |
1041| Promise&lt;void&gt; | Promise对象。 |
1042
1043**示例**:
1044
1045```ts
1046import { vibrator } from '@kit.SensorServiceKit';
1047import { BusinessError } from '@kit.BasicServicesKit';
1048
1049vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(() => {
1050  console.info('Succeed in vibrating');
1051}, (error: BusinessError) => {
1052  console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
1053});
1054```
1055
1056
1057## vibrator.vibrate<sup>(deprecated)</sup>
1058
1059vibrate(effectId: EffectId, callback?: AsyncCallback&lt;void&gt;): void
1060
1061按照指定振动效果触发马达振动。
1062
1063从API version 9 开始不再维护,建议使用 [vibrator.startVibration](#vibratorstartvibration9)<sup>9+</sup>代替。
1064
1065**需要权限**:ohos.permission.VIBRATE
1066
1067**系统能力**:SystemCapability.Sensors.MiscDevice
1068
1069**参数**:
1070
1071| 参数名   | 类型                      | 必填 | 说明                                                       |
1072| -------- | ------------------------- | ---- | ---------------------------------------------------------- |
1073| effectId | [EffectId](#effectid)     | 是   | 预置的振动效果ID。                                         |
1074| callback | AsyncCallback&lt;void&gt; | 否   | 回调函数,当马达振动成功,err为undefined,否则为错误对象。 |
1075
1076**示例**:
1077
1078```ts
1079import { vibrator } from '@kit.SensorServiceKit';
1080import { BusinessError } from '@kit.BasicServicesKit';
1081
1082vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => {
1083  if (error) {
1084    console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
1085  } else {
1086    console.info('Succeed in vibrating');
1087  }
1088})
1089```
1090
1091## vibrator.stop<sup>(deprecated)</sup>
1092
1093stop(stopMode: VibratorStopMode): Promise&lt;void&gt;
1094
1095按照指定模式停止马达的振动。
1096
1097从API version 9 开始不再维护,建议使用 [vibrator.stopVibration](#vibratorstopvibration9-1)<sup>9+</sup>代替。
1098
1099**需要权限**:ohos.permission.VIBRATE
1100
1101**系统能力**:SystemCapability.Sensors.MiscDevice
1102
1103**参数**:
1104
1105| 参数名   | 类型                                  | 必填 | 说明                     |
1106| -------- | ------------------------------------- | ---- | ------------------------ |
1107| stopMode | [VibratorStopMode](#vibratorstopmode) | 是   | 马达停止指定的振动模式。 |
1108
1109**返回值**:
1110
1111| 类型                | 说明                                   |
1112| ------------------- | -------------------------------------- |
1113| Promise&lt;void&gt; | Promise对象。 |
1114
1115**示例**:
1116
1117```ts
1118import { vibrator } from '@kit.SensorServiceKit';
1119import { BusinessError } from '@kit.BasicServicesKit';
1120
1121// 按照effectId类型启动振动
1122vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => {
1123  if (error) {
1124    console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
1125  } else {
1126    console.info('Succeed in vibrating');
1127  }
1128})
1129// 使用VIBRATOR_STOP_MODE_PRESET模式停止振动
1130vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => {
1131  console.info('Succeed in stopping');
1132}, (error: BusinessError) => {
1133  console.error(`Failed to stop. Code: ${error.code}, message: ${error.message}`);
1134});
1135```
1136
1137
1138## vibrator.stop<sup>(deprecated)</sup>
1139
1140stop(stopMode: VibratorStopMode, callback?: AsyncCallback&lt;void&gt;): void
1141
1142按照指定模式停止马达的振动。
1143
1144从API version 9 开始不再维护,建议使用 [vibrator.stopVibration](#vibratorstopvibration9)<sup>9+</sup>代替。
1145
1146**需要权限**:ohos.permission.VIBRATE
1147
1148**系统能力**:SystemCapability.Sensors.MiscDevice
1149
1150**参数**:
1151
1152| 参数名   | 类型                                  | 必填 | 说明                                                         |
1153| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ |
1154| stopMode | [VibratorStopMode](#vibratorstopmode) | 是   | 马达停止指定的振动模式。                                     |
1155| callback | AsyncCallback&lt;void&gt;             | 否   | 回调函数,当马达停止振动成功,err为undefined,否则为错误对象。 |
1156
1157**示例**:
1158
1159```ts
1160import { vibrator } from '@kit.SensorServiceKit';
1161import { BusinessError } from '@kit.BasicServicesKit';
1162
1163// 按照effectId类型启动振动
1164vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => {
1165  if (error) {
1166    console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
1167  } else {
1168    console.info('Succeed in vibrating');
1169  }
1170})
1171// 使用VIBRATOR_STOP_MODE_PRESET模式停止振动
1172vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, (error: BusinessError) => {
1173  if (error) {
1174    console.error(`Failed to stop. Code: ${error.code}, message: ${error.message}`);
1175  } else {
1176    console.info('Succeed in stopping');
1177  }
1178})
1179```
1180