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 options are as follows:<br>- [VibrateTime](#vibratetime9): vibration with the specified duration.<br>- [VibratePreset](#vibratepreset9): vibration with a preset effect.<br>- [VibrateFromFile](#vibratefromfile10): vibration according to a custom vibration configuration file.| 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' 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' 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 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<void> 138 139Starts vibration with the specified effect and attribute. This API uses a promise to return the result. 140 141**Required permissions**: ohos.permission.VIBRATE 142 143**Atomic service API**: This API can be used in atomic services since API version 11. 144 145**System capability**: SystemCapability.Sensors.MiscDevice 146 147**Parameters** 148 149| Name | Type | Mandatory| Description | 150| --------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 151| effect | [VibrateEffect](#vibrateeffect9) | Yes | Vibration effect. The options are as follows:<br>- [VibrateTime](#vibratetime9): vibration with the specified duration.<br>- [VibratePreset](#vibratepreset9): vibration with a preset effect.<br>- [VibrateFromFile](#vibratefromfile10): vibration according to a custom vibration configuration file.| 152| attribute | [VibrateAttribute](#vibrateattribute9) | Yes | Vibration attribute. | 153 154**Return value** 155 156| Type | Description | 157| ------------------- | -------------------------------------- | 158| Promise<void> | Promise that returns no value.| 159 160**Error codes** 161 162For details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md) and [Universal Error Codes](../errorcode-universal.md). 163 164| ID| Error Message | 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**Example** 172 173Trigger vibration with the specified duration. 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 197Trigger vibration with a preset effect. 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 222Trigger vibration according to a custom vibration configuration file. 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<void>): void 256 257Stops vibration in the specified mode. This API uses an asynchronous callback to return the result. 258 259**Required permissions**: ohos.permission.VIBRATE 260 261**System capability**: SystemCapability.Sensors.MiscDevice 262 263**Parameters** 264 265| Name | Type | Mandatory| Description | 266| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 267| 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). | 268| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the vibration stops, **err** is **undefined**; otherwise, **err** is an error object.| 269 270**Error codes** 271 272For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 273 274| ID| Error Message | 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**Example** 280 281Stop fixed-duration vibration. 282 283```ts 284import { vibrator } from '@kit.SensorServiceKit'; 285import { BusinessError } from '@kit.BasicServicesKit'; 286 287try { 288 // Start vibration at a fixed duration. 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 // Stop vibration in VIBRATOR_STOP_MODE_TIME mode. 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 322Stop preset vibration. 323 324```ts 325import { vibrator } from '@kit.SensorServiceKit'; 326import { BusinessError } from '@kit.BasicServicesKit'; 327 328try { 329 // Start vibration with a preset effect. 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 // Stop vibration in VIBRATOR_STOP_MODE_PRESET mode. 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<void> 367 368Stops vibration in the specified mode. This API uses a promise to return the result. 369 370**Required permissions**: ohos.permission.VIBRATE 371 372**System capability**: SystemCapability.Sensors.MiscDevice 373 374**Parameters** 375 376| Name | Type | Mandatory| Description | 377| -------- | ------------------------------------- | ---- | ------------------------ | 378| 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).| 379 380**Return value** 381 382| Type | Description | 383| ------------------- | -------------------------------------- | 384| Promise<void> | Promise that returns no value.| 385 386**Error codes** 387 388For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 389 390| ID| Error Message | 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**Example** 396 397Stop fixed-duration vibration. 398 399```ts 400import { vibrator } from '@kit.SensorServiceKit'; 401import { BusinessError } from '@kit.BasicServicesKit'; 402 403try { 404 // Start vibration at a fixed duration. 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 // Stop vibration in VIBRATOR_STOP_MODE_TIME mode. 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 434Stop preset vibration. 435 436```ts 437import { vibrator } from '@kit.SensorServiceKit'; 438import { BusinessError } from '@kit.BasicServicesKit'; 439 440try { 441 // Start vibration with a preset effect. 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 // Stop vibration in VIBRATOR_STOP_MODE_PRESET mode. 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<void>): void 475 476Stops vibration in all modes. This API uses an asynchronous callback to return the result. 477 478**Required permissions**: ohos.permission.VIBRATE 479 480**Atomic service API**: This API can be used in atomic services since API version 11. 481 482**System capability**: SystemCapability.Sensors.MiscDevice 483 484**Parameters** 485 486| Name | Type | Mandatory| Description | 487| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 488| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the vibration stops, **err** is **undefined**; otherwise, **err** is an error object.| 489 490**Error codes** 491 492For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 493 494| ID| Error Message | 495| -------- | ------------------ | 496| 201 | Permission denied. | 497 498**Example** 499 500```ts 501import { vibrator } from '@kit.SensorServiceKit'; 502import { BusinessError } from '@kit.BasicServicesKit'; 503 504try { 505 // Stop vibration in all modes. 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<void> 522 523Stops vibration in all modes. This API uses a promise to return the result. 524 525**Required permissions**: ohos.permission.VIBRATE 526 527**Atomic service API**: This API can be used in atomic services since API version 11. 528 529**System capability**: SystemCapability.Sensors.MiscDevice 530 531**Return value** 532 533| Type | Description | 534| ------------------- | ------------- | 535| Promise<void> | Promise that returns no value.| 536 537**Error codes** 538 539For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 540 541| ID| Error Message | 542| -------- | ------------------ | 543| 201 | Permission denied. | 544 545**Example** 546 547```ts 548import { vibrator } from '@kit.SensorServiceKit'; 549import { BusinessError } from '@kit.BasicServicesKit'; 550 551try { 552 // Stop vibration in all modes. 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 568Stops any form of motor vibration. 569 570**Required permissions**: ohos.permission.VIBRATE 571 572**Atomic service API**: This API can be used in atomic services since API version 12. 573 574**System capability**: SystemCapability.Sensors.MiscDevice 575 576**Error codes** 577 578For details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md) and [Universal Error Codes](../errorcode-universal.md). 579 580| ID| Error Message | 581| -------- | ------------------------ | 582| 201 | Permission denied. | 583| 14600101 | Device operation failed. | 584 585**Example** 586 587```ts 588import { vibrator } from '@kit.SensorServiceKit'; 589import { BusinessError } from '@kit.BasicServicesKit'; 590 591try { 592 // Stop any form of motor vibration. 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<boolean>): void 604 605Checks whether an effect ID is supported. This API uses an asynchronous callback to return the result. 606 607**System capability**: SystemCapability.Sensors.MiscDevice 608 609**Parameters** 610 611| Name | Type | Mandatory| Description | 612| -------- | ---------------------------- | ---- | ------------------------------------------------------ | 613| effectId | string | Yes | Vibration effect ID. | 614| 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.| 615 616**Error codes** 617 618For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 619 620| ID| Error Message | 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**Example** 626 627```ts 628import { vibrator } from '@kit.SensorServiceKit'; 629import { BusinessError } from '@kit.BasicServicesKit'; 630 631try { 632 // Check whether 'haptic.clock.timer' is supported. 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 // To use startVibration, you must configure the ohos.permission.VIBRATE permission. 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<boolean> 670 671Checks whether an effect ID is supported. This API uses a promise to return the result. 672 673**System capability**: SystemCapability.Sensors.MiscDevice 674 675**Parameters** 676 677| Name | Type | Mandatory| Description | 678| -------- | ------ | ---- | ------------ | 679| effectId | string | Yes | Vibration effect ID.| 680 681**Return value** 682 683| Type | Description | 684| ---------------------- | --------------------------------------------------------- | 685| Promise<boolean> | Promise that returns the result. The value **true** means that the effect ID is supported, and **false** means the opposite.| 686 687**Error codes** 688 689For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 690 691| ID| Error Message | 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**Example** 697 698```ts 699import { vibrator } from '@kit.SensorServiceKit'; 700import { BusinessError } from '@kit.BasicServicesKit'; 701 702try { 703 // Check whether 'haptic.clock.timer' is supported. 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 737Checks whether the preset vibration effect is supported. 738 739**System capability**: SystemCapability.Sensors.MiscDevice 740 741**Parameters** 742 743| Name | Type | Mandatory| Description | 744| -------- | ------ | ---- | -------------------- | 745| effectId | string | Yes | ID of the preset vibration effect.| 746 747**Return value** 748 749| Type | Description | 750| ------- | ------------------------------------------------------ | 751| boolean | Returned object. The value **true** means that the effect ID is supported, and **false** means the opposite.| 752 753**Error codes** 754 755For details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md) and [Universal Error Codes](../errorcode-universal.md). 756 757| ID| Error Message | 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**Example** 763 764```ts 765import { vibrator } from '@kit.SensorServiceKit'; 766import { BusinessError } from '@kit.BasicServicesKit'; 767 768try { 769 // Check whether the preset 'haptic.clock.timer' is supported. 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 782Checks whether HD vibration is supported. 783 784**System capability**: SystemCapability.Sensors.MiscDevice 785 786**Return value** 787 788| Type | Description | 789| ------- | ---------- | 790| boolean | Returned object.| 791 792**Error codes** 793 794For details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md). 795 796| ID| Error Message | 797| -------- | ------------------------ | 798| 14600101 | Device operation failed. | 799 800**Example** 801 802```ts 803import { vibrator } from '@kit.SensorServiceKit'; 804import { BusinessError } from '@kit.BasicServicesKit'; 805 806try { 807 // Check whether HD vibration is supported. 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 818Enumerates the preset vibration effect IDs. 819 820**System capability**: SystemCapability.Sensors.MiscDevice 821 822| Name | Value | Description | 823| ------------------ | -------------------- | -------------------------------- | 824| EFFECT_CLOCK_TIMER | 'haptic.clock.timer' | Vibration effect when a user adjusts the timer.| 825 826## HapticFeedback<sup>12+</sup> 827 828Defines the vibration effect. 829 830**System capability**: SystemCapability.Sensors.MiscDevice 831 832| Name | Value | Description | 833| ------------ | --------------------- | ---------------------------- | 834| EFFECT_SOFT | 'haptic.effect.soft' | Soft vibration, low frequency.| 835| EFFECT_HARD | 'haptic.effect.hard' | Hard vibration, medium frequency.| 836| EFFECT_SHARP | 'haptic.effect.sharp' | Sharp vibration, high frequency.| 837 838## VibratorStopMode 839 840Enumerates the modes available to stop the vibration. 841 842**System capability**: SystemCapability.Sensors.MiscDevice 843 844| Name | Value | Description | 845| ------------------------- | -------- | ------------------------------ | 846| VIBRATOR_STOP_MODE_TIME | 'time' | The vibration to stop is in **duration** mode.| 847| VIBRATOR_STOP_MODE_PRESET | 'preset' | The vibration to stop is in **EffectId** mode.| 848 849## VibrateEffect<sup>9+</sup> 850 851Describes the vibration effect. 852 853**System capability**: SystemCapability.Sensors.MiscDevice 854 855| Type | Description | 856| -------------------------------- | ------------------------------ | 857| [VibrateTime](#vibratetime9) | Vibration with the specified duration.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 858| [VibratePreset](#vibratepreset9) | Vibration with a preset effect.| 859| [VibrateFromFile](#vibratefromfile10) | Vibration according to a custom vibration configuration file.| 860 861## VibrateTime<sup>9+</sup> 862 863Describes the fixed-duration vibration. 864 865**Atomic service API**: This API can be used in atomic services since API version 11. 866 867**System capability**: SystemCapability.Sensors.MiscDevice 868 869| Name | Type | Mandatory| Description | 870| -------- | ------ | ----- | ------------------------------ | 871| type | 'time' | Yes | The value **time** means vibration with the specified duration.| 872| duration | number | Yes | Vibration duration, in ms. | 873 874## VibratePreset<sup>9+</sup> 875 876Describes the preset vibration. 877 878**System capability**: SystemCapability.Sensors.MiscDevice 879 880| Name | Type | Mandatory| Description | 881| -------- | -------- | ---- |------------------------------ | 882| type | 'preset' | Yes | The value **preset** means vibration with the specified effect.| 883| effectId | string | Yes | Preset vibration effect ID. | 884| count | number | No | Number of repeated vibrations. The default value is **1**. This parameter is optional.| 885| intensity<sup>12+</sup> | number | No| Vibration intensity. The value ranges from 0 to 100. The default value is **100**. This parameter is optional. If vibration intensity adjustment is not supported, the default vibration intensity will be used.| 886 887## VibrateFromFile<sup>10+</sup> 888 889Describes 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. 890 891**System capability**: SystemCapability.Sensors.MiscDevice 892 893| Name | Type | Mandatory| Description | 894| -------- | -------- | ---- | ------------------------------ | 895| type | 'file' | Yes | The value **file** means vibration according to a vibration configuration file.| 896| hapticFd | [HapticFileDescriptor](#hapticfiledescriptor10)<sup>10+</sup> | Yes| File descriptor (FD) of the vibration configuration file.| 897 898## HapticFileDescriptor<sup>10+</sup> 899 900Describes 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). 901 902**System capability**: SystemCapability.Sensors.MiscDevice 903 904| Name | Type | Mandatory | Description | 905| -------- | -------- |--------| ------------------------------| 906| fd | number | Yes | FD of the custom vibration configuration file. | 907| 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.| 908| 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.| 909 910## VibrateAttribute<sup>9+</sup> 911 912Describes the vibration attribute. 913 914**Atomic service API**: This API can be used in atomic services since API version 11. 915 916**System capability**: SystemCapability.Sensors.MiscDevice 917 918| Name | Type| Mandatory| Description | 919| ----- | ------ | ---- | -------------- | 920| id | number | No| Vibrator ID. The default value is **0**. | 921| usage | [Usage](#usage9) | Yes| Vibration scenario.| 922 923## Usage<sup>9+</sup> 924 925type Usage = 'unknown' | 'alarm' | 'ring' | 'notification' | 'communication' | 'touch' | 'media' | 'physicalFeedback' | 'simulateReality' 926 927Enumerates the vibration scenarios. 928 929**Atomic service API**: This API can be used in atomic services since API version 11. 930 931**System capability**: SystemCapability.Sensors.MiscDevice 932<!--RP1--> 933 934| Type | Description | 935| ---------------- | ------------------------------ | 936| 'unknown' | Unknown scenario, with the lowest priority. This parameter has a fixed value of **unknown**.| 937| 'alarm' | Vibration for alarms. This parameter has a fixed value of **alarm**.| 938| 'ring' | Vibration for ringing. This parameter has a fixed value of **ring**.| 939| 'notification' | Vibration for notification. This parameter has a fixed value of **notification**.| 940| 'communication' | Vibration for communication. This parameter has a fixed value of **communication**.| 941| 'touch' | Vibration for touch. This parameter has a fixed value of **touch**.| 942| 'media' | Vibration for media. This parameter has a fixed value of **media**.| 943| 'physicalFeedback' | Vibration for physical feedback. This parameter has a fixed value of **physicalFeedback**.| 944| 'simulateReality' | Vibration for simulated reality. This parameter has a fixed value of **simulateReality**.| 945<!--RP1End--> 946 947## vibrator.vibrate<sup>(deprecated)</sup> 948 949vibrate(duration: number): Promise<void> 950 951Triggers vibration with the specified duration. This API uses a promise to return the result. 952 953This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9-1)<sup>9+</sup> instead. 954 955**Required permissions**: ohos.permission.VIBRATE 956 957**System capability**: SystemCapability.Sensors.MiscDevice 958 959**Parameters** 960 961| Name | Type | Mandatory| Description | 962| -------- | ------ | ---- | ---------------------- | 963| duration | number | Yes | Vibration duration, in ms.| 964 965**Return value** 966 967| Type | Description | 968| ------------------- | -------------------------------------- | 969| Promise<void> | Promise that returns no value.| 970 971**Example** 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<void>): void 987 988Triggers vibration with the specified duration. This API uses an asynchronous callback to return the result. 989 990This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9)<sup>9+</sup> instead. 991 992**Required permissions**: ohos.permission.VIBRATE 993 994**System capability**: SystemCapability.Sensors.MiscDevice 995 996**Parameters** 997 998| Name | Type | Mandatory| Description | 999| -------- | ------------------------- | ---- | ---------------------------------------------------------- | 1000| duration | number | Yes | Vibration duration, in ms. | 1001| callback | AsyncCallback<void> | No | Callback used to return the result. If the vibration starts, **err** is **undefined**; otherwise, **err** is an error object.| 1002 1003**Example** 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<void> 1022 1023Triggers vibration with the specified effect. This API uses a promise to return the result. 1024 1025This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9-1)<sup>9+</sup> instead. 1026 1027**Required permissions**: ohos.permission.VIBRATE 1028 1029**System capability**: SystemCapability.Sensors.MiscDevice 1030 1031**Parameters** 1032 1033| Name | Type | Mandatory| Description | 1034| -------- | --------------------- | ---- | ------------------ | 1035| effectId | [EffectId](#effectid) | Yes | Preset vibration effect ID.| 1036 1037**Return value** 1038 1039| Type | Description | 1040| ------------------- | -------------------------------------- | 1041| Promise<void> | Promise that returns no value.| 1042 1043**Example** 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<void>): void 1060 1061Triggers vibration with the specified effect. This API uses an asynchronous callback to return the result. 1062 1063This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9)<sup>9+</sup> instead. 1064 1065**Required permissions**: ohos.permission.VIBRATE 1066 1067**System capability**: SystemCapability.Sensors.MiscDevice 1068 1069**Parameters** 1070 1071| Name | Type | Mandatory| Description | 1072| -------- | ------------------------- | ---- | ---------------------------------------------------------- | 1073| effectId | [EffectId](#effectid) | Yes | Preset vibration effect ID. | 1074| callback | AsyncCallback<void> | No | Callback used to return the result. If the vibration starts, **err** is **undefined**; otherwise, **err** is an error object.| 1075 1076**Example** 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<void> 1094 1095Stops vibration in the specified mode. This API uses a promise to return the result. 1096 1097This API is deprecated since API version 9. You are advised to use [vibrator.stopVibration](#vibratorstopvibration9-1)<sup>9+</sup> instead. 1098 1099**Required permissions**: ohos.permission.VIBRATE 1100 1101**System capability**: SystemCapability.Sensors.MiscDevice 1102 1103**Parameters** 1104 1105| Name | Type | Mandatory| Description | 1106| -------- | ------------------------------------- | ---- | ------------------------ | 1107| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes | Mode to stop the vibration.| 1108 1109**Return value** 1110 1111| Type | Description | 1112| ------------------- | -------------------------------------- | 1113| Promise<void> | Promise that returns no value.| 1114 1115**Example** 1116 1117```ts 1118import { vibrator } from '@kit.SensorServiceKit'; 1119import { BusinessError } from '@kit.BasicServicesKit'; 1120 1121// Start vibration based on the specified effect ID. 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// Stop vibration in VIBRATOR_STOP_MODE_PRESET mode. 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<void>): void 1141 1142Stops vibration in the specified mode. This API uses an asynchronous callback to return the result. 1143 1144This API is deprecated since API version 9. You are advised to use [vibrator.stopVibration](#vibratorstopvibration9)<sup>9+</sup> instead. 1145 1146**Required permissions**: ohos.permission.VIBRATE 1147 1148**System capability**: SystemCapability.Sensors.MiscDevice 1149 1150**Parameters** 1151 1152| Name | Type | Mandatory| Description | 1153| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 1154| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes | Mode to stop the vibration. | 1155| callback | AsyncCallback<void> | No | Callback used to return the result. If the vibration stops, **err** is **undefined**; otherwise, **err** is an error object.| 1156 1157**Example** 1158 1159```ts 1160import { vibrator } from '@kit.SensorServiceKit'; 1161import { BusinessError } from '@kit.BasicServicesKit'; 1162 1163// Start vibration based on the specified effect ID. 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// Stop vibration in VIBRATOR_STOP_MODE_PRESET mode. 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