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 '@kit.SensorServiceKit'; 14``` 15 16## vibrator.startVibration<sup>9+</sup> 17 18startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: AsyncCallback<void>): 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**Atomic service API**: This API can be used in atomic services since API version 11. 25 26**System capability**: SystemCapability.Sensors.MiscDevice 27 28**Parameters** 29 30| Name | Type | Mandatory| Description | 31| --------- | -------------------------------------- | ---- | :----------------------------------------------------------- | 32| effect | [VibrateEffect](#vibrateeffect9) | Yes | Vibration effect. The following options are supported:<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.<br>- [VibrateFromPattern<sup>18+</sup>](#vibratefrompattern18): vibration according to a custom vibration pattern.| 33| attribute | [VibrateAttribute](#vibrateattribute9) | Yes | Vibration attribute. | 34| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the vibration starts, **err** is **undefined**; otherwise, **err** is an error object. | 35 36**Error codes** 37 38For details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md) and [Universal Error Codes](../errorcode-universal.md). 39 40| ID| Error Message | 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**Example** 48 49Trigger vibration with the specified duration. 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' // The switch control is subject to the selected type. 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 75Trigger vibration with a preset effect. 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' // The switch control is subject to the selected type. 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 102Trigger vibration according to a custom vibration configuration file. 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 111@Entry 112@Component 113struct Index { 114 uiContext = this.getUIContext(); 115 116 build() { 117 Row() { 118 Column() { 119 Button('alarm-file') 120 .onClick(() => { 121 let rawFd: resourceManager.RawFileDescriptor | undefined = this.uiContext.getHostContext()?.resourceManager.getRawFdSync(fileName); 122 if (rawFd != undefined) { 123 try { 124 vibrator.startVibration({ 125 type: "file", 126 hapticFd: { fd: rawFd.fd, offset: rawFd.offset, length: rawFd.length } 127 }, { 128 id: 0, 129 usage: 'alarm' // The switch control is subject to the selected type. 130 }, (error: BusinessError) => { 131 if (error) { 132 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 133 return; 134 } 135 console.info('Succeed in starting vibration'); 136 }); 137 } catch (err) { 138 let e: BusinessError = err as BusinessError; 139 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 140 } 141 } 142 this.uiContext.getHostContext()?.resourceManager.closeRawFdSync(fileName); 143 }) 144 } 145 .width('100%') 146 } 147 .height('100%') 148 } 149} 150``` 151 152## vibrator.startVibration<sup>9+</sup> 153 154startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise<void> 155 156Starts vibration with the specified effect and attribute. This API uses a promise to return the result. 157 158**Required permissions**: ohos.permission.VIBRATE 159 160**Atomic service API**: This API can be used in atomic services since API version 11. 161 162**System capability**: SystemCapability.Sensors.MiscDevice 163 164**Parameters** 165 166| Name | Type | Mandatory| Description | 167| --------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 168| effect | [VibrateEffect](#vibrateeffect9) | Yes | Vibration effect. The following options are supported:<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.<br>- [VibrateFromPattern<sup>18+</sup>](#vibratefrompattern18): vibration according to a custom vibration pattern.| 169| attribute | [VibrateAttribute](#vibrateattribute9) | Yes | Vibration attribute. | 170 171**Return value** 172 173| Type | Description | 174| ------------------- | -------------------------------------- | 175| Promise<void> | Promise that returns no value.| 176 177**Error codes** 178 179For details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md) and [Universal Error Codes](../errorcode-universal.md). 180 181| ID| Error Message | 182| -------- | ------------------------------------------------------------ | 183| 201 | Permission denied. | 184| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 185| 801 | Capability not supported. | 186| 14600101 | Device operation failed. | 187 188**Example** 189 190Trigger vibration with the specified duration. 191 192```ts 193import { vibrator } from '@kit.SensorServiceKit'; 194import { BusinessError } from '@kit.BasicServicesKit'; 195 196try { 197 vibrator.startVibration({ 198 type: 'time', 199 duration: 1000 200 }, { 201 id: 0, 202 usage: 'alarm' // The switch control is subject to the selected type. 203 }).then(() => { 204 console.info('Succeed in starting vibration'); 205 }, (error: BusinessError) => { 206 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 207 }); 208} catch (err) { 209 let e: BusinessError = err as BusinessError; 210 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 211} 212``` 213 214Trigger vibration with a preset effect. 215 216```ts 217import { vibrator } from '@kit.SensorServiceKit'; 218import { BusinessError } from '@kit.BasicServicesKit'; 219 220try { 221 vibrator.startVibration({ 222 type: 'preset', 223 effectId: 'haptic.clock.timer', 224 count: 1, 225 }, { 226 id: 0, 227 usage: 'alarm' // The switch control is subject to the selected type. 228 }).then(() => { 229 console.info('Succeed in starting vibration'); 230 }, (error: BusinessError) => { 231 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 232 }); 233} catch (err) { 234 let e: BusinessError = err as BusinessError; 235 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 236} 237``` 238 239Trigger vibration according to a custom vibration configuration file. 240 241```ts 242import { vibrator } from '@kit.SensorServiceKit'; 243import { resourceManager } from '@kit.LocalizationKit'; 244import { BusinessError } from '@kit.BasicServicesKit'; 245 246const fileName: string = 'xxx.json'; 247 248@Entry 249@Component 250struct Index { 251 uiContext = this.getUIContext(); 252 253 build() { 254 Row() { 255 Column() { 256 Button('alarm-file') 257 .onClick(() => { 258 let rawFd: resourceManager.RawFileDescriptor | undefined = this.uiContext.getHostContext()?.resourceManager.getRawFdSync(fileName); 259 if (rawFd != undefined) { 260 try { 261 vibrator.startVibration({ 262 type: "file", 263 hapticFd: { fd: rawFd.fd, offset: rawFd.offset, length: rawFd.length } 264 }, { 265 id: 0, 266 usage: 'alarm' // The switch control is subject to the selected type. 267 }, (error: BusinessError) => { 268 if (error) { 269 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 270 return; 271 } 272 console.info('Succeed in starting vibration'); 273 }); 274 } catch (err) { 275 let e: BusinessError = err as BusinessError; 276 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 277 } 278 } 279 this.uiContext.getHostContext()?.resourceManager.closeRawFdSync(fileName); 280 }) 281 } 282 .width('100%') 283 } 284 .height('100%') 285 } 286} 287``` 288 289## vibrator.stopVibration<sup>9+</sup> 290 291stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback<void>): void 292 293Stops vibration in the specified mode. This API uses an asynchronous callback to return the result. 294 295**Required permissions**: ohos.permission.VIBRATE 296 297**System capability**: SystemCapability.Sensors.MiscDevice 298 299**Parameters** 300 301| Name | Type | Mandatory| Description | 302| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 303| 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).| 304| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the vibration stops, **err** is **undefined**; otherwise, **err** is an error object.| 305 306**Error codes** 307 308For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 309 310| ID| Error Message | 311| -------- | ------------------------------------------------------------ | 312| 201 | Permission denied. | 313| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 314 315**Example** 316 317Stop fixed-duration vibration. 318 319```ts 320import { vibrator } from '@kit.SensorServiceKit'; 321import { BusinessError } from '@kit.BasicServicesKit'; 322 323try { 324 // Start vibration at a fixed duration. 325 vibrator.startVibration({ 326 type: 'time', 327 duration: 1000, 328 }, { 329 id: 0, 330 usage: 'alarm' // The switch control is subject to the selected type. 331 }, (error: BusinessError) => { 332 if (error) { 333 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 334 return; 335 } 336 console.info('Succeed in starting vibration'); 337 }); 338} catch (err) { 339 let e: BusinessError = err as BusinessError; 340 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 341} 342 343try { 344 // Stop vibration in VIBRATOR_STOP_MODE_TIME mode. 345 vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, (error: BusinessError) => { 346 if (error) { 347 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 348 return; 349 } 350 console.info('Succeed in stopping vibration'); 351 }) 352} catch (err) { 353 let e: BusinessError = err as BusinessError; 354 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 355} 356``` 357 358Stop preset vibration. 359 360```ts 361import { vibrator } from '@kit.SensorServiceKit'; 362import { BusinessError } from '@kit.BasicServicesKit'; 363 364try { 365 // Start vibration with a preset effect. 366 vibrator.startVibration({ 367 type: 'preset', 368 effectId: 'haptic.clock.timer', 369 count: 1, 370 }, { 371 id: 0, 372 usage: 'alarm' // The switch control is subject to the selected type. 373 }, (error: BusinessError) => { 374 if (error) { 375 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 376 return; 377 } 378 console.info('Succeed in starting vibration'); 379 }); 380} catch (err) { 381 let e: BusinessError = err as BusinessError; 382 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 383} 384 385try { 386 // Stop vibration in VIBRATOR_STOP_MODE_PRESET mode. 387 vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, (error: BusinessError) => { 388 if (error) { 389 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 390 return; 391 } 392 console.info('Succeed in stopping vibration'); 393 }) 394} catch (err) { 395 let e: BusinessError = err as BusinessError; 396 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 397} 398``` 399 400## vibrator.stopVibration<sup>9+</sup> 401 402stopVibration(stopMode: VibratorStopMode): Promise<void> 403 404Stops vibration in the specified mode. This API uses a promise to return the result. 405 406**Required permissions**: ohos.permission.VIBRATE 407 408**System capability**: SystemCapability.Sensors.MiscDevice 409 410**Parameters** 411 412| Name | Type | Mandatory| Description | 413| -------- | ------------------------------------- | ---- | ------------------------ | 414| 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).| 415 416**Return value** 417 418| Type | Description | 419| ------------------- | -------------------------------------- | 420| Promise<void> | Promise that returns no value.| 421 422**Error codes** 423 424For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 425 426| ID| Error Message | 427| -------- | ------------------------------------------------------------ | 428| 201 | Permission denied. | 429| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 430 431**Example** 432 433Stop fixed-duration vibration. 434 435```ts 436import { vibrator } from '@kit.SensorServiceKit'; 437import { BusinessError } from '@kit.BasicServicesKit'; 438 439try { 440 // Start vibration at a fixed duration. 441 vibrator.startVibration({ 442 type: 'time', 443 duration: 1000, 444 }, { 445 id: 0, 446 usage: 'alarm' // The switch control is subject to the selected type. 447 }).then(() => { 448 console.info('Succeed in starting vibration'); 449 }, (error: BusinessError) => { 450 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 451 }); 452} catch (err) { 453 let e: BusinessError = err as BusinessError; 454 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 455} 456 457try { 458 // Stop vibration in VIBRATOR_STOP_MODE_TIME mode. 459 vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME).then(() => { 460 console.info('Succeed in stopping vibration'); 461 }, (error: BusinessError) => { 462 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 463 }); 464} catch (err) { 465 let e: BusinessError = err as BusinessError; 466 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 467} 468``` 469 470Stop preset vibration. 471 472```ts 473import { vibrator } from '@kit.SensorServiceKit'; 474import { BusinessError } from '@kit.BasicServicesKit'; 475 476try { 477 // Start vibration with a preset effect. 478 vibrator.startVibration({ 479 type: 'preset', 480 effectId: 'haptic.clock.timer', 481 count: 1, 482 }, { 483 id: 0, 484 usage: 'alarm' // The switch control is subject to the selected type. 485 }).then(() => { 486 console.info('Succeed in starting vibration'); 487 }, (error: BusinessError) => { 488 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 489 }); 490} catch (err) { 491 let e: BusinessError = err as BusinessError; 492 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 493} 494 495try { 496 // Stop vibration in VIBRATOR_STOP_MODE_PRESET mode. 497 vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => { 498 console.info('Succeed in stopping vibration'); 499 }, (error: BusinessError) => { 500 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 501 }); 502} catch (err) { 503 let e: BusinessError = err as BusinessError; 504 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 505} 506``` 507 508## vibrator.stopVibration<sup>10+</sup> 509 510stopVibration(callback: AsyncCallback<void>): void 511 512Stops vibration in all modes. This API uses an asynchronous callback to return the result. 513 514**Required permissions**: ohos.permission.VIBRATE 515 516**Atomic service API**: This API can be used in atomic services since API version 11. 517 518**System capability**: SystemCapability.Sensors.MiscDevice 519 520**Parameters** 521 522| Name | Type | Mandatory| Description | 523| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 524| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the vibration stops, **err** is **undefined**; otherwise, **err** is an error object.| 525 526**Error codes** 527 528For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 529 530| ID| Error Message | 531| -------- | ------------------ | 532| 201 | Permission denied. | 533 534**Example** 535 536```ts 537import { vibrator } from '@kit.SensorServiceKit'; 538import { BusinessError } from '@kit.BasicServicesKit'; 539 540try { 541 // Stop vibration in all modes. 542 vibrator.stopVibration((error: BusinessError) => { 543 if (error) { 544 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 545 return; 546 } 547 console.info('Succeed in stopping vibration'); 548 }) 549} catch (error) { 550 let e: BusinessError = error as BusinessError; 551 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 552} 553``` 554 555## vibrator.stopVibration<sup>10+</sup> 556 557stopVibration(): Promise<void> 558 559Stops vibration in all modes. This API uses a promise to return the result. 560 561**Required permissions**: ohos.permission.VIBRATE 562 563**Atomic service API**: This API can be used in atomic services since API version 11. 564 565**System capability**: SystemCapability.Sensors.MiscDevice 566 567**Return value** 568 569| Type | Description | 570| ------------------- | ------------- | 571| Promise<void> | Promise that returns no value.| 572 573**Error codes** 574 575For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 576 577| ID| Error Message | 578| -------- | ------------------ | 579| 201 | Permission denied. | 580 581**Example** 582 583```ts 584import { vibrator } from '@kit.SensorServiceKit'; 585import { BusinessError } from '@kit.BasicServicesKit'; 586 587try { 588 // Stop vibration in all modes. 589 vibrator.stopVibration().then(() => { 590 console.info('Succeed in stopping vibration'); 591 }, (error: BusinessError) => { 592 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 593 }); 594} catch (error) { 595 let e: BusinessError = error as BusinessError; 596 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 597} 598``` 599 600## vibrator.stopVibrationSync<sup>12+</sup> 601 602stopVibrationSync(): void 603 604Stops any form of motor vibration. 605 606**Required permissions**: ohos.permission.VIBRATE 607 608**Atomic service API**: This API can be used in atomic services since API version 12. 609 610**System capability**: SystemCapability.Sensors.MiscDevice 611 612**Error codes** 613 614For details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md) and [Universal Error Codes](../errorcode-universal.md). 615 616| ID| Error Message | 617| -------- | ------------------------ | 618| 201 | Permission denied. | 619| 14600101 | Device operation failed. | 620 621**Example** 622 623```ts 624import { vibrator } from '@kit.SensorServiceKit'; 625import { BusinessError } from '@kit.BasicServicesKit'; 626 627try { 628 // Stop any form of motor vibration. 629 vibrator.stopVibrationSync() 630 console.info('Succeed in stopping vibration'); 631} catch (error) { 632 let e: BusinessError = error as BusinessError; 633 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 634} 635``` 636 637## vibrator.isSupportEffect<sup>10+</sup> 638 639isSupportEffect(effectId: string, callback: AsyncCallback<boolean>): void 640 641Checks whether an effect ID is supported. This API uses an asynchronous callback to return the result. 642 643**System capability**: SystemCapability.Sensors.MiscDevice 644 645**Parameters** 646 647| Name | Type | Mandatory| Description | 648| -------- | ---------------------------- | ---- | ------------------------------------------------------ | 649| effectId | string | Yes | Vibration effect ID. | 650| callback | AsyncCallback<boolean> | Yes | Callback used to return the result. The value **true** means that the effect ID is supported, and **false** means the opposite.| 651 652**Error codes** 653 654For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 655 656| ID| Error Message | 657| -------- | ------------------------------------------------------------ | 658| 201 | Permission denied. | 659| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 660 661**Example** 662 663```ts 664import { vibrator } from '@kit.SensorServiceKit'; 665import { BusinessError } from '@kit.BasicServicesKit'; 666 667try { 668 // Check whether 'haptic.clock.timer' is supported. 669 vibrator.isSupportEffect('haptic.clock.timer', (err: BusinessError, state: boolean) => { 670 if (err) { 671 console.error(`Failed to query effect. Code: ${err.code}, message: ${err.message}`); 672 return; 673 } 674 console.info('Succeed in querying effect'); 675 if (state) { 676 try { 677 // To use startVibration, you must configure the ohos.permission.VIBRATE permission. 678 vibrator.startVibration({ 679 type: 'preset', 680 effectId: 'haptic.clock.timer', 681 count: 1, 682 }, { 683 usage: 'unknown' // The switch control is subject to the selected type. 684 }, (error: BusinessError) => { 685 if (error) { 686 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 687 } else { 688 console.info('Succeed in starting vibration'); 689 } 690 }); 691 } catch (error) { 692 let e: BusinessError = error as BusinessError; 693 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 694 } 695 } 696 }) 697} catch (error) { 698 let e: BusinessError = error as BusinessError; 699 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 700} 701``` 702 703## vibrator.isSupportEffect<sup>10+</sup> 704 705isSupportEffect(effectId: string): Promise<boolean> 706 707Checks whether an effect ID is supported. This API uses a promise to return the result. 708 709**System capability**: SystemCapability.Sensors.MiscDevice 710 711**Parameters** 712 713| Name | Type | Mandatory| Description | 714| -------- | ------ | ---- | ------------ | 715| effectId | string | Yes | Vibration effect ID.| 716 717**Return value** 718 719| Type | Description | 720| ---------------------- | --------------------------------------------------------- | 721| Promise<boolean> | Promise that returns the result. The value **true** means that the effect ID is supported, and **false** means the opposite.| 722 723**Error codes** 724 725For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 726 727| ID| Error Message | 728| -------- | ------------------------------------------------------------ | 729| 201 | Permission denied. | 730| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 731 732**Example** 733 734```ts 735import { vibrator } from '@kit.SensorServiceKit'; 736import { BusinessError } from '@kit.BasicServicesKit'; 737 738try { 739 // Check whether 'haptic.clock.timer' is supported. 740 vibrator.isSupportEffect('haptic.clock.timer').then((state: boolean) => { 741 console.info(`The query result is ${state}`); 742 if (state) { 743 try { 744 vibrator.startVibration({ 745 type: 'preset', 746 effectId: 'haptic.clock.timer', 747 count: 1, 748 }, { 749 usage: 'unknown' // The switch control is subject to the selected type. 750 }).then(() => { 751 console.info('Succeed in starting vibration'); 752 }).catch((error: BusinessError) => { 753 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 754 }); 755 } catch (error) { 756 let e: BusinessError = error as BusinessError; 757 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 758 } 759 } 760 }, (error: BusinessError) => { 761 console.error(`Failed to query effect. Code: ${error.code}, message: ${error.message}`); 762 }) 763} catch (error) { 764 let e: BusinessError = error as BusinessError; 765 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 766} 767``` 768 769## vibrator.isSupportEffectSync<sup>12+</sup> 770 771isSupportEffectSync(effectId: string): boolean 772 773Checks whether the preset vibration effect is supported. 774 775**System capability**: SystemCapability.Sensors.MiscDevice 776 777**Parameters** 778 779| Name | Type | Mandatory| Description | 780| -------- | ------ | ---- | -------------------- | 781| effectId | string | Yes | ID of the preset vibration effect.| 782 783**Return value** 784 785| Type | Description | 786| ------- | ------------------------------------------------------ | 787| boolean | Returned object. The value **true** means that the effect ID is supported, and **false** means the opposite.| 788 789**Error codes** 790 791For details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md) and [Universal Error Codes](../errorcode-universal.md). 792 793| ID| Error Message | 794| -------- | ------------------------------------------------------------ | 795| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 796| 14600101 | Device operation failed. | 797 798**Example** 799 800```ts 801import { vibrator } from '@kit.SensorServiceKit'; 802import { BusinessError } from '@kit.BasicServicesKit'; 803 804try { 805 // Check whether the preset 'haptic.clock.timer' is supported. 806 let ret = vibrator.isSupportEffectSync('haptic.clock.timer'); 807 console.info(`The query result is ${ret}`); 808} catch (error) { 809 let e: BusinessError = error as BusinessError; 810 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 811} 812``` 813 814## vibrator.isHdHapticSupported<sup>12+</sup> 815 816isHdHapticSupported(): boolean 817 818Checks whether HD vibration is supported. 819 820**System capability**: SystemCapability.Sensors.MiscDevice 821 822**Return value** 823 824| Type | Description | 825| ------- | -------------------------------------------------- | 826| boolean | Boolean value indicating whether HD vibration is supported. The value **true** indicates that HD vibration is supported, and the value **false** indicates the opposite.| 827 828**Error codes** 829 830For details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md). 831 832| ID| Error Message | 833| -------- | ------------------------ | 834| 14600101 | Device operation failed. | 835 836**Example** 837 838```ts 839import { vibrator } from '@kit.SensorServiceKit'; 840import { BusinessError } from '@kit.BasicServicesKit'; 841 842try { 843 // Check whether HD vibration is supported. 844 let ret = vibrator.isHdHapticSupported(); 845 console.info(`The query result is ${ret}`); 846} catch (error) { 847 let e: BusinessError = error as BusinessError; 848 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 849} 850``` 851 852## VibratorPatternBuilder<sup>18+</sup> 853 854### vibrator('addContinuousEvent')<sup>18+</sup> 855 856addContinuousEvent(time: number, duration: number, options?: ContinuousParam): VibratorPatternBuilder; 857 858Adds a long vibration event as a **VibratorPattern** object. 859 860**System capability**: SystemCapability.Sensors.MiscDevice 861 862**Parameters** 863 864| Name | Type | Mandatory| Description | 865| -------- | ------------------------------------- | ---- | ------------------------ | 866| time | number | Yes | Start time of the long vibration. | 867| duration | number | Yes | Duration of the long vibration. | 868| options | [ContinuousParam](#continuousparam18) | No | Optional parameters.| 869 870**Return value** 871 872| Type | Description | 873| ---------------------- | ---------------------------------------------------- | 874| VibratorPatternBuilder | **VibratorPattern** object representing a long vibration event.| 875 876**Error codes** 877 878For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 879 880| ID| Error Message | 881| -------- | ---------------- | 882| 401 | Parameter error. | 883 884**Example** 885 886```ts 887import { vibrator } from '@kit.SensorServiceKit'; 888import { BusinessError } from '@kit.BasicServicesKit'; 889 890let builder = new vibrator.VibratorPatternBuilder(); 891try { 892 let pointsMe: vibrator.VibratorCurvePoint[] = [ 893 { time: 0, intensity: 0, frequency: -7 }, 894 { time: 42, intensity: 1, frequency: -6 }, 895 { time: 128, intensity: 0.94, frequency: -4 }, 896 { time: 217, intensity: 0.63, frequency: -14 }, 897 { time: 763, intensity: 0.48, frequency: -14 }, 898 { time: 1125, intensity: 0.53, frequency: -10 }, 899 { time: 1503, intensity: 0.42, frequency: -14 }, 900 { time: 1858, intensity: 0.39, frequency: -14 }, 901 { time: 2295, intensity: 0.34, frequency: -17 }, 902 { time: 2448, intensity: 0.21, frequency: -14 }, 903 { time: 2468, intensity: 0, frequency: -21 } 904 ] // No less than four VibratorCurvePoint objects must be set. The maximum value is 16. 905 let param: vibrator.ContinuousParam = { 906 intensity: 97, 907 frequency: 34, 908 points:pointsMe, 909 index: 0 910 } 911 builder.addContinuousEvent(0, 2468, param); 912 console.info(`addContinuousEvent builder is ${builder.build()}`); 913} catch(error) { 914 let e: BusinessError = error as BusinessError; 915 console.error(`Exception. Code ${e.code}`); 916} 917``` 918 919### vibrator('addTransientEvent')<sup>18+</sup> 920 921addTransientEvent(time: number, options?: TransientParam): VibratorPatternBuilder; 922 923Adds a short vibration event as a **VibratorPattern** object. 924 925**System capability**: SystemCapability.Sensors.MiscDevice 926 927**Parameters** 928 929| Name | Type | Mandatory| Description | 930| ------- | ----------------------------------- | ---- | ------------------------ | 931| time | number | Yes | Start time of long vibration. | 932| options | [TransientParam](#transientparam18) | No | Optional parameters.| 933 934**Return value** 935 936| Type | Description | 937| ---------------------- | ------------------------------------------------ | 938| VibratorPatternBuilder | **VibratorPatternBuilder** object representing a short vibration event.| 939 940**Error codes** 941 942For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 943 944| ID| Error Message | 945| -------- | ---------------- | 946| 401 | Parameter error. | 947 948**Example** 949 950```ts 951import { vibrator } from '@kit.SensorServiceKit'; 952import { BusinessError } from '@kit.BasicServicesKit'; 953 954let builder = new vibrator.VibratorPatternBuilder(); 955try { 956 let param: vibrator.TransientParam = { 957 intensity: 80, 958 frequency: 70, 959 index: 0 960 } 961 builder.addTransientEvent(0, param); 962 console.log(`addTransientEvent builder is ${builder.build()}`); 963} catch(error) { 964 let e: BusinessError = error as BusinessError; 965 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 966} 967``` 968 969### vibrator('build')<sup>18+</sup> 970 971build(): VibratorPattern; 972 973Constructor used to create a **VibratorPattern** object, which determines the vibration sequence of short or long events. 974 975**System capability**: SystemCapability.Sensors.MiscDevice 976 977**Return value** 978 979| Type | Description | 980| ------------------------------------- | ---------------------------------- | 981| [VibratorPattern](#vibratorpattern18) | **VibratorPattern** object.| 982 983**Example** 984 985```ts 986import { vibrator } from '@kit.SensorServiceKit'; 987import { BusinessError } from '@kit.BasicServicesKit'; 988 989let builder = new vibrator.VibratorPatternBuilder(); 990try { 991 let param: vibrator.TransientParam = { 992 intensity: 80, 993 frequency: 70, 994 index: 0 995 } 996 builder.addTransientEvent(0, param); 997 console.log(`addTransientEvent builder is ${builder.build()}`); 998} catch(error) { 999 let e: BusinessError = error as BusinessError; 1000 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 1001} 1002try { 1003 vibrator.startVibration({ 1004 type: "pattern", 1005 pattern: builder.build() 1006 }, { 1007 usage: "alarm", // The switch control is subject to the selected type. 1008 }, (error) => { 1009 if (error) { 1010 let e: BusinessError = error as BusinessError; 1011 console.error(`Vibrate fail. Code: ${e.code}, message: ${e.message}`); 1012 } else { 1013 console.info(`vibrate success`); 1014 } 1015 }); 1016} catch(error) { 1017 let e: BusinessError = error as BusinessError; 1018 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 1019} 1020``` 1021 1022## EffectId 1023 1024Enumerates the preset vibration effect IDs. 1025 1026**System capability**: SystemCapability.Sensors.MiscDevice 1027 1028| Name | Value | Description | 1029| ------------------ | -------------------- | -------------------------------- | 1030| EFFECT_CLOCK_TIMER | 'haptic.clock.timer' | Vibration effect when a user adjusts the timer.| 1031 1032## HapticFeedback<sup>12+</sup> 1033 1034Defines the vibration effect. 1035 1036**System capability**: SystemCapability.Sensors.MiscDevice 1037 1038| Name | Value | Description | 1039| ----------------------------------- | ----------------------- | ---------------------------- | 1040| EFFECT_SOFT | 'haptic.effect.soft' | Soft vibration, low frequency.| 1041| EFFECT_HARD | 'haptic.effect.hard' | Hard vibration, medium frequency.| 1042| EFFECT_SHARP | 'haptic.effect.sharp' | Sharp vibration, high frequency.| 1043| EFFECT_NOTICE_SUCCESS<sup>18+</sup> | 'haptic.notice.success' | Vibration for a successful notification. | 1044| EFFECT_NOTICE_FAILURE<sup>18+</sup> | 'haptic.notice.fail' | Vibration for a notification failure. | 1045| EFFECT_NOTICE_WARNING<sup>18+</sup> | 'haptic.notice.warning' | Vibration for an alert. | 1046 1047## VibratorStopMode 1048 1049Enumerates the modes available to stop the vibration. 1050 1051**System capability**: SystemCapability.Sensors.MiscDevice 1052 1053| Name | Value | Description | 1054| ------------------------- | -------- | ------------------------------ | 1055| VIBRATOR_STOP_MODE_TIME | 'time' | The vibration to stop is in **duration** mode.| 1056| VIBRATOR_STOP_MODE_PRESET | 'preset' | The vibration to stop is in **EffectId** mode.| 1057 1058## VibrateEffect<sup>9+</sup> 1059 1060Describes the vibration effect. 1061 1062**System capability**: SystemCapability.Sensors.MiscDevice 1063 1064| Type | Description | 1065| -------------------------------- | ------------------------------ | 1066| [VibrateTime](#vibratetime9) | Vibration with the specified duration.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 1067| [VibratePreset](#vibratepreset9) | Vibration with a preset effect.| 1068| [VibrateFromFile](#vibratefromfile10) | Vibration according to a custom vibration configuration file.| 1069| VibrateFromPattern<sup>18+</sup> | Triggers vibration with the custom effect. This API uses an asynchronous callback to return the result.| 1070 1071## VibrateTime<sup>9+</sup> 1072 1073Describes the fixed-duration vibration. 1074 1075**Atomic service API**: This API can be used in atomic services since API version 11. 1076 1077**System capability**: SystemCapability.Sensors.MiscDevice 1078 1079| Name | Type | Mandatory| Description | 1080| -------- | ------ | ----- | ------------------------------ | 1081| type | 'time' | Yes | The value **time** means vibration with the specified duration.| 1082| duration | number | Yes | Vibration duration, in ms. | 1083 1084## VibratePreset<sup>9+</sup> 1085 1086Describes the preset vibration. 1087 1088**System capability**: SystemCapability.Sensors.MiscDevice 1089 1090| Name | Type | Mandatory| Description | 1091| -------- | -------- | ---- |------------------------------ | 1092| type | 'preset' | Yes | The value **preset** means vibration with the specified effect.| 1093| effectId | string | Yes | Preset vibration effect ID. | 1094| count | number | No | Number of repeated vibrations. This parameter is optional. The default value is **1**. | 1095| intensity<sup>12+</sup> | number | No| Vibration intensity. This parameter is optional. The value range is [0, 100]. The default value is **100**. If vibration intensity adjustment is not supported, the default vibration intensity will be used.| 1096 1097## VibrateFromFile<sup>10+</sup> 1098 1099Describes the custom vibration type, which is supported only by certain devices. If a device does not support this vibration type, [an error code indicating unsupported device](../errorcode-universal.md) is returned. 1100 1101**System capability**: SystemCapability.Sensors.MiscDevice 1102 1103| Name | Type | Mandatory| Description | 1104| -------- | -------- | ---- | ------------------------------ | 1105| type | 'file' | Yes | The value **file** means vibration according to a vibration configuration file.| 1106| hapticFd | [HapticFileDescriptor](#hapticfiledescriptor10)<sup>10+</sup> | Yes| File descriptor (FD) of the vibration configuration file.| 1107 1108## HapticFileDescriptor<sup>10+</sup> 1109 1110Describes the FD of a custom vibration configuration file. Ensure that the file is available, and the parameters in it can be obtained from the sandbox path through the [file management API](../apis-core-file-kit/js-apis-file-fs.md#fsopen) or from the HAP resource through the [resource management API](../apis-localization-kit/js-apis-resource-manager.md#getrawfd9). The use case is as follows: The system triggers vibration according to the sequence set in a configuration file, based on the specified offset and length. For details about the storage format of the vibration sequence, see [Custom Vibration](../../device/sensor/vibrator-guidelines.md#custom-vibration). 1111 1112**System capability**: SystemCapability.Sensors.MiscDevice 1113 1114| Name | Type | Mandatory | Description | 1115| -------- | -------- |--------| ------------------------------| 1116| fd | number | Yes | FD of the custom vibration configuration file. | 1117| 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.| 1118| 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.| 1119 1120## VibratorEventType<sup>18+</sup> 1121 1122Vibration event type. 1123 1124**System capability**: SystemCapability.Sensors.MiscDevice 1125 1126| Name | Type | Mandatory| Description | 1127| ---------- | ------ | ---- | ----------------- | 1128| CONTINUOUS | number | Yes | The value **0** indicates long vibration.| 1129| TRANSIENT | number | Yes | The value **1** indicates short vibration.| 1130 1131## VibratorCurvePoint<sup>18+</sup> 1132 1133Defines the gain relative to the vibration intensity. 1134 1135**System capability**: SystemCapability.Sensors.MiscDevice 1136 1137| Name | Type | Mandatory| Description | 1138| --------- | ------ | ---- | ------------------------------------------------------------ | 1139| time | number | Yes | Start time offset. | 1140| intensity | number | No | Gain relative to the vibration intensity. This parameter is optional. The value range is [0, 1]. If this parameter is left empty, the default value is **1**.| 1141| frequency | number | No | Change relative to the vibration frequency. This parameter is optional. The value range is [-100, 100]. If this parameter is left empty, the default value is 0.| 1142 1143## VibratorEvent<sup>18+</sup> 1144 1145Vibration event. 1146 1147**System capability**: SystemCapability.Sensors.MiscDevice 1148 1149| Name | Type | Mandatory| Description | 1150| --------- | ------------------------------- | ---- | ------------------------------------------------------------ | 1151| eventType | VibratorEventType | Yes | Vibration event type. | 1152| time | number | Yes | Vibration start time. | 1153| duration | number | No | Vibration duration. This parameter is optional. The value range is [0, 5000]. The default value is **48** for short vibration and **1000** for long vibration.| 1154| intensity | number | No | Vibration intensity. This parameter is optional. The value range is [0, 100]. If this parameter is left empty, the default value is **100**.| 1155| frequency | number | No | Vibration frequency. This parameter is optional.The value range is [0, 100]. If this parameter is left empty, the default value is **50**. | 1156| index | number | No | Channel number. This parameter is optional. If this parameter is left empty, the default value is **0**. | 1157| points | Array<VibratorCurvePoint> | No | Adjustment points of the vibration curve. | 1158 1159## VibratorPattern<sup>18+</sup> 1160 1161Defines the vibration sequence. 1162 1163**System capability**: SystemCapability.Sensors.MiscDevice 1164 1165| Name | Type | Mandatory| Description | 1166| ------ | -------------------------- | ---- | ---------------------------------------------------- | 1167| time | number | Yes | Absolute vibration start time. | 1168| events | Array<VibratorEvent> | Yes | Vibration event array, which is the **VibratorPattern** object returned by **build() **.| 1169 1170## ContinuousParam<sup>18+</sup> 1171 1172Defines the parameters for continuous vibration. 1173 1174**System capability**: SystemCapability.Sensors.MiscDevice 1175 1176| Name | Type | Mandatory| Description | 1177| --------- | -------------------- | ---- | ------------------------------------------------------------ | 1178| intensity | number | No | Vibration intensity. This parameter is optional. The value range is [0, 100]. If this parameter is left empty, the default value is **100**.| 1179| frequency | number | No | Vibration frequency. This parameter is optional.The value range is [0, 100]. If this parameter is left empty, the default value is **50**. | 1180| points | VibratorCurvePoint[] | No | Adjustment points of the vibration curve. | 1181| index | number | No | Channel number. This parameter is optional. If this parameter is left empty, the default value is **0**. | 1182 1183## TransientParam<sup>18+</sup> 1184 1185Defines the parameters for transient vibration. 1186 1187**System capability**: SystemCapability.Sensors.MiscDevice 1188 1189| Name | Type | Mandatory| Description | 1190| --------- | ------ | ---- | ------------------------------------------- | 1191| intensity | number | No | Vibration intensity. This parameter is optional. If this parameter is left empty, the default value is **100**.| 1192| frequency | number | No | Vibration frequency. This parameter is optional. If this parameter is left empty, the default value is **50**. | 1193| index | number | No | Channel number. This parameter is optional. If this parameter is left empty, the default value is **0**. | 1194 1195## VibrateFromPattern<sup>18+</sup> 1196 1197Defines the custom vibration effect. 1198 1199**System capability**: SystemCapability.Sensors.MiscDevice 1200 1201| Name | Type | Mandatory| Description | 1202| ------- | --------------- | ---- | ---------------------------------------------------- | 1203| type | 'pattern' | Yes | If the value is **pattern**, the vibrator vibrates based on the specified pattern. | 1204| pattern | VibratorPattern | Yes | Vibration event array, which is the **VibratorPattern** object returned by **build() **.| 1205 1206## VibrateAttribute<sup>9+</sup> 1207 1208Describes the vibration attribute. 1209 1210**Atomic service API**: This API can be used in atomic services since API version 11. 1211 1212**System capability**: SystemCapability.Sensors.MiscDevice 1213 1214| Name | Type| Mandatory| Description | 1215| ----- | ------ | ---- | -------------- | 1216| id | number | No| Vibrator ID. The default value is **0**. | 1217| usage | [Usage](#usage9) | Yes| Vibration scenario.| 1218 1219## Usage<sup>9+</sup> 1220 1221type Usage = 'unknown' | 'alarm' | 'ring' | 'notification' | 'communication' | 'touch' | 'media' | 'physicalFeedback' | 'simulateReality' 1222 1223Enumerates the vibration scenarios. 1224 1225**Atomic service API**: This API can be used in atomic services since API version 11. 1226 1227**System capability**: SystemCapability.Sensors.MiscDevice 1228<!--RP1--> 1229 1230| Type | Description | 1231| ---------------- | ------------------------------ | 1232| 'unknown' | Unknown scenario, with the lowest priority. This parameter has a fixed value of **unknown**.| 1233| 'alarm' | Vibration for alarms. This parameter has a fixed value of **alarm**.| 1234| 'ring' | Vibration for ringing. This parameter has a fixed value of **ring**.| 1235| 'notification' | Vibration for notification. This parameter has a fixed value of **notification**.| 1236| 'communication' | Vibration for communication. This parameter has a fixed value of **communication**.| 1237| 'touch' | Vibration for touch. This parameter has a fixed value of **touch**.| 1238| 'media' | Vibration for media. This parameter has a fixed value of **media**.| 1239| 'physicalFeedback' | Vibration for physical feedback. This parameter has a fixed value of **physicalFeedback**.| 1240| 'simulateReality' | Vibration for simulated reality. This parameter has a fixed value of **simulateReality**.| 1241<!--RP1End--> 1242 1243## vibrator.vibrate<sup>(deprecated)</sup> 1244 1245vibrate(duration: number): Promise<void> 1246 1247Triggers vibration with the specified duration. This API uses a promise to return the result. 1248 1249This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9-1)<sup>9+</sup> instead. 1250 1251**Required permissions**: ohos.permission.VIBRATE 1252 1253**System capability**: SystemCapability.Sensors.MiscDevice 1254 1255**Parameters** 1256 1257| Name | Type | Mandatory| Description | 1258| -------- | ------ | ---- | ---------------------- | 1259| duration | number | Yes | Vibration duration, in ms.| 1260 1261**Return value** 1262 1263| Type | Description | 1264| ------------------- | -------------------------------------- | 1265| Promise<void> | Promise that returns no value.| 1266 1267**Example** 1268 1269```ts 1270import { vibrator } from '@kit.SensorServiceKit'; 1271import { BusinessError } from '@kit.BasicServicesKit'; 1272 1273vibrator.vibrate(1000).then(() => { 1274 console.info('Succeed in vibrating'); 1275}, (error: BusinessError) => { 1276 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 1277}); 1278``` 1279 1280## vibrator.vibrate<sup>(deprecated)</sup> 1281 1282vibrate(duration: number, callback?: AsyncCallback<void>): void 1283 1284Triggers vibration with the specified duration. This API uses an asynchronous callback to return the result. 1285 1286This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9)<sup>9+</sup> instead. 1287 1288**Required permissions**: ohos.permission.VIBRATE 1289 1290**System capability**: SystemCapability.Sensors.MiscDevice 1291 1292**Parameters** 1293 1294| Name | Type | Mandatory| Description | 1295| -------- | ------------------------- | ---- | ---------------------------------------------------------- | 1296| duration | number | Yes | Vibration duration, in ms. | 1297| callback | AsyncCallback<void> | No | Callback used to return the result. If the vibration starts, **err** is **undefined**; otherwise, **err** is an error object.| 1298 1299**Example** 1300 1301```ts 1302import { vibrator } from '@kit.SensorServiceKit'; 1303import { BusinessError } from '@kit.BasicServicesKit'; 1304 1305vibrator.vibrate(1000, (error: BusinessError) => { 1306 if (error) { 1307 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 1308 } else { 1309 console.info('Succeed in vibrating'); 1310 } 1311}) 1312``` 1313 1314 1315## vibrator.vibrate<sup>(deprecated)</sup> 1316 1317vibrate(effectId: EffectId): Promise<void> 1318 1319Triggers vibration with the specified effect. This API uses a promise to return the result. 1320 1321This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9-1)<sup>9+</sup> instead. 1322 1323**Required permissions**: ohos.permission.VIBRATE 1324 1325**System capability**: SystemCapability.Sensors.MiscDevice 1326 1327**Parameters** 1328 1329| Name | Type | Mandatory| Description | 1330| -------- | --------------------- | ---- | ------------------ | 1331| effectId | [EffectId](#effectid) | Yes | Preset vibration effect ID.| 1332 1333**Return value** 1334 1335| Type | Description | 1336| ------------------- | -------------------------------------- | 1337| Promise<void> | Promise that returns no value.| 1338 1339**Example** 1340 1341```ts 1342import { vibrator } from '@kit.SensorServiceKit'; 1343import { BusinessError } from '@kit.BasicServicesKit'; 1344 1345vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(() => { 1346 console.info('Succeed in vibrating'); 1347}, (error: BusinessError) => { 1348 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 1349}); 1350``` 1351 1352 1353## vibrator.vibrate<sup>(deprecated)</sup> 1354 1355vibrate(effectId: EffectId, callback?: AsyncCallback<void>): void 1356 1357Triggers vibration with the specified effect. This API uses an asynchronous callback to return the result. 1358 1359This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9)<sup>9+</sup> instead. 1360 1361**Required permissions**: ohos.permission.VIBRATE 1362 1363**System capability**: SystemCapability.Sensors.MiscDevice 1364 1365**Parameters** 1366 1367| Name | Type | Mandatory| Description | 1368| -------- | ------------------------- | ---- | ---------------------------------------------------------- | 1369| effectId | [EffectId](#effectid) | Yes | Preset vibration effect ID. | 1370| callback | AsyncCallback<void> | No | Callback used to return the result. If the vibration starts, **err** is **undefined**; otherwise, **err** is an error object.| 1371 1372**Example** 1373 1374```ts 1375import { vibrator } from '@kit.SensorServiceKit'; 1376import { BusinessError } from '@kit.BasicServicesKit'; 1377 1378vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => { 1379 if (error) { 1380 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 1381 } else { 1382 console.info('Succeed in vibrating'); 1383 } 1384}) 1385``` 1386 1387## vibrator.stop<sup>(deprecated)</sup> 1388 1389stop(stopMode: VibratorStopMode): Promise<void> 1390 1391Stops vibration in the specified mode. This API uses a promise to return the result. 1392 1393This API is deprecated since API version 9. You are advised to use [vibrator.stopVibration](#vibratorstopvibration9-1)<sup>9+</sup> instead. 1394 1395**Required permissions**: ohos.permission.VIBRATE 1396 1397**System capability**: SystemCapability.Sensors.MiscDevice 1398 1399**Parameters** 1400 1401| Name | Type | Mandatory| Description | 1402| -------- | ------------------------------------- | ---- | ------------------------ | 1403| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes | Mode to stop the vibration.| 1404 1405**Return value** 1406 1407| Type | Description | 1408| ------------------- | -------------------------------------- | 1409| Promise<void> | Promise that returns no value.| 1410 1411**Example** 1412 1413```ts 1414import { vibrator } from '@kit.SensorServiceKit'; 1415import { BusinessError } from '@kit.BasicServicesKit'; 1416 1417// Start vibration based on the specified effect ID. 1418vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => { 1419 if (error) { 1420 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 1421 } else { 1422 console.info('Succeed in vibrating'); 1423 } 1424}) 1425// Stop vibration in VIBRATOR_STOP_MODE_PRESET mode. 1426vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => { 1427 console.info('Succeed in stopping'); 1428}, (error: BusinessError) => { 1429 console.error(`Failed to stop. Code: ${error.code}, message: ${error.message}`); 1430}); 1431``` 1432 1433 1434## vibrator.stop<sup>(deprecated)</sup> 1435 1436stop(stopMode: VibratorStopMode, callback?: AsyncCallback<void>): void 1437 1438Stops vibration in the specified mode. This API uses an asynchronous callback to return the result. 1439 1440This API is deprecated since API version 9. You are advised to use [vibrator.stopVibration](#vibratorstopvibration9)<sup>9+</sup> instead. 1441 1442**Required permissions**: ohos.permission.VIBRATE 1443 1444**System capability**: SystemCapability.Sensors.MiscDevice 1445 1446**Parameters** 1447 1448| Name | Type | Mandatory| Description | 1449| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 1450| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes | Mode to stop the vibration. | 1451| callback | AsyncCallback<void> | No | Callback used to return the result. If the vibration stops, **err** is **undefined**; otherwise, **err** is an error object.| 1452 1453**Example** 1454 1455```ts 1456import { vibrator } from '@kit.SensorServiceKit'; 1457import { BusinessError } from '@kit.BasicServicesKit'; 1458 1459// Start vibration based on the specified effect ID. 1460vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => { 1461 if (error) { 1462 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 1463 } else { 1464 console.info('Succeed in vibrating'); 1465 } 1466}) 1467// Stop vibration in VIBRATOR_STOP_MODE_PRESET mode. 1468vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, (error: BusinessError) => { 1469 if (error) { 1470 console.error(`Failed to stop. Code: ${error.code}, message: ${error.message}`); 1471 } else { 1472 console.info('Succeed in stopping'); 1473 } 1474}) 1475``` 1476