1# @ohos.vibrator (Vibrator) 2 3The **vibrator** module allows precise control over the vibration of device vibrators. With the APIs provided by this module, you can start vibration in various modes such as specified duration, preset effect, and custom effect and stop any or all of them. 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>1. [VibratePreset](#vibratepreset9): triggers vibration according to preset vibration effects. This mode is suitable for short vibration scenarios in interactive feedback (such as tapping, long-pressing, sliding, dragging, etc.). This API is recommended to maintain consistency with the system's overall vibration feedback experience.<br>2. [VibrateFromFile](#vibratefromfile10): triggers vibration according to custom vibration configuration file. This mode is suitable for interactive feedback in complex scenarios requiring precise vibration patterns (such as realistic effects triggered by emoji packs, or feedback for in-game actions/mechanics).<br>3. [VibrateTime](#vibratetime9): triggers vibration of the specified duration, providing basic control over the start and stop of vibration. This mode does not support customization of vibration intensity, frequency, or other parameters. As a result, the vibration adjustment is relatively coarse and not suitable for delivering a refined experience.<br>- [VibrateFromPattern<sup>18+</sup>](#vibratefrompattern18): starts vibration according to a custom vibration pattern. The usage scenario is the same as **VibrateFromFile**. **VibrateFromFile** utilizes predefined effects in a custom configuration file, passing specific vibration events to the API via file descriptors. By contrast, **VibrateFromPattern** enables more flexible vibration event combinations, delivering them to the API as a vibration event array.<br>| 33| attribute | [VibrateAttribute](#vibrateattribute9) | Yes | Vibration attribute. | 34| callback | AsyncCallback<void> | Yes | Callback used to return the operation result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object, which contains the error code and error information.| 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 491. Start vibration based on the preset effect. 50 51 ```ts 52 import { vibrator } from '@kit.SensorServiceKit'; 53 import { BusinessError } from '@kit.BasicServicesKit'; 54 55 try { 56 // Check whether 'haptic.notice.success' is supported. 57 vibrator.isSupportEffect('haptic.notice.success', (err: BusinessError, state: boolean) => { 58 if (err) { 59 console.error(`Failed to query effect. Code: ${err.code}, message: ${err.message}`); 60 return; 61 } 62 console.info('Succeed in querying effect'); 63 if (state) { 64 try { 65 vibrator.startVibration({ 66 type: 'preset', 67 effectId: 'haptic.notice.success', 68 count: 1, 69 }, { 70 usage: 'notification' // The switch control is subject to the selected type. 71 }, (error: BusinessError) => { 72 if (error) { 73 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 74 return; 75 } 76 console.info('Succeed in starting vibration'); 77 78 }); 79 } catch (err) { 80 let e: BusinessError = err as BusinessError; 81 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 82 } 83 } 84 }) 85 } catch (error) { 86 let e: BusinessError = error as BusinessError; 87 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 88 } 89 ``` 90 912. Start vibration according to the custom vibration configuration file. 92 93 ```ts 94 import { vibrator } from '@kit.SensorServiceKit'; 95 import { resourceManager } from '@kit.LocalizationKit'; 96 import { BusinessError } from '@kit.BasicServicesKit'; 97 98 const fileName: string = 'xxx.json'; 99 100 @Entry 101 @Component 102 struct Index { 103 uiContext = this.getUIContext(); 104 105 build() { 106 Row() { 107 Column() { 108 Button('alarm-file') 109 .onClick(() => { 110 let rawFd: resourceManager.RawFileDescriptor | undefined = this.uiContext.getHostContext()?.resourceManager.getRawFdSync(fileName); 111 if (rawFd != undefined) { 112 try { 113 vibrator.startVibration({ 114 type: "file", 115 hapticFd: { fd: rawFd.fd, offset: rawFd.offset, length: rawFd.length } 116 }, { 117 id: 0, 118 usage: 'alarm' // The switch control is subject to the selected type. 119 }, (error: BusinessError) => { 120 if (error) { 121 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 122 return; 123 } 124 console.info('Succeed in starting vibration'); 125 }); 126 } catch (err) { 127 let e: BusinessError = err as BusinessError; 128 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 129 } 130 } 131 this.uiContext.getHostContext()?.resourceManager.closeRawFdSync(fileName); 132 }) 133 } 134 .width('100%') 135 } 136 .height('100%') 137 } 138 } 139 ``` 140 1413. Start vibration of the specified duration. 142 143 ```ts 144 import { vibrator } from '@kit.SensorServiceKit'; 145 import { BusinessError } from '@kit.BasicServicesKit'; 146 147 try { 148 vibrator.startVibration({ 149 type: 'time', 150 duration: 1000, 151 }, { 152 id: 0, 153 usage: 'alarm' // The switch control is subject to the selected type. 154 }, (error: BusinessError) => { 155 if (error) { 156 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 157 return; 158 } 159 console.info('Succeed in starting vibration'); 160 }); 161 } catch (err) { 162 let e: BusinessError = err as BusinessError; 163 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 164 } 165 ``` 166 167## vibrator.startVibration<sup>9+</sup> 168 169startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise<void> 170 171Starts vibration with the specified effect and attribute. This API uses a promise to return the result. 172 173**Required permissions**: ohos.permission.VIBRATE 174 175**Atomic service API**: This API can be used in atomic services since API version 11. 176 177**System capability**: SystemCapability.Sensors.MiscDevice 178 179**Parameters** 180 181| Name | Type | Mandatory| Description | 182| --------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 183| effect | [VibrateEffect](#vibrateeffect9) | Yes | Vibration effect. The following options are supported:<br>- [VibratePreset](#vibratepreset9): triggers vibration according to preset vibration effects. This mode is suitable for short vibration scenarios in interactive feedback (such as tapping, long-pressing, sliding, dragging, etc.). This API is recommended to maintain consistency with the system's overall vibration feedback experience.<br>- [VibrateFromFile](#vibratefromfile10): triggers vibration according to custom vibration configuration file. This mode is suitable for interactive feedback in complex scenarios requiring precise vibration patterns (such as realistic effects triggered by emoji packs, or feedback for in-game actions/mechanics).<br>- [VibrateTime](#vibratetime9): triggers vibration of the specified duration, providing basic control over the start and stop of vibration. This mode does not support customization of vibration intensity, frequency, or other parameters. As a result, the vibration adjustment is relatively coarse and not suitable for delivering a refined experience.<br>- [VibrateFromPattern<sup>18+</sup>](#vibratefrompattern18): starts vibration according to a custom vibration pattern. The usage scenario is the same as **VibrateFromFile**. **VibrateFromFile** utilizes predefined effects in a custom configuration file, passing specific vibration events to the API via file descriptors. By contrast, **VibrateFromPattern** enables more flexible vibration event combinations, delivering them to the API as a vibration event array.| 184| attribute | [VibrateAttribute](#vibrateattribute9) | Yes | Vibration attribute. | 185 186**Return value** 187 188| Type | Description | 189| ------------------- | ------------------------- | 190| Promise<void> | Promise that returns no value.| 191 192**Error codes** 193 194For details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md) and [Universal Error Codes](../errorcode-universal.md). 195 196| ID| Error Message | 197| -------- | ------------------------------------------------------------ | 198| 201 | Permission denied. | 199| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 200| 801 | Capability not supported. | 201| 14600101 | Device operation failed. | 202 203**Example** 204 2051. Start vibration based on the preset effect. 206 207 ```ts 208 import { vibrator } from '@kit.SensorServiceKit'; 209 import { BusinessError } from '@kit.BasicServicesKit'; 210 211 try { 212 // Check whether 'haptic.notice.success' is supported. 213 vibrator.isSupportEffect('haptic.notice.success', (err: BusinessError, state: boolean) => { 214 if (err) { 215 console.error(`Failed to query effect. Code: ${err.code}, message: ${err.message}`); 216 return; 217 } 218 console.info('Succeed in querying effect'); 219 if (state) { 220 try { 221 vibrator.startVibration({ 222 type: 'preset', 223 effectId: 'haptic.notice.success', 224 count: 1, 225 }, { 226 usage: 'notification' // The switch control is subject to the selected type. 227 }, (error: BusinessError) => { 228 if (error) { 229 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 230 return; 231 } 232 console.info('Succeed in starting vibration'); 233 234 }); 235 } catch (err) { 236 let e: BusinessError = err as BusinessError; 237 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 238 } 239 } 240 }) 241 } catch (error) { 242 let e: BusinessError = error as BusinessError; 243 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 244 } 245 ``` 246 2472. Start vibration according to the custom vibration configuration file. 248 249 ```ts 250 import { vibrator } from '@kit.SensorServiceKit'; 251 import { resourceManager } from '@kit.LocalizationKit'; 252 import { BusinessError } from '@kit.BasicServicesKit'; 253 254 const fileName: string = 'xxx.json'; 255 256 @Entry 257 @Component 258 struct Index { 259 uiContext = this.getUIContext(); 260 261 build() { 262 Row() { 263 Column() { 264 Button('alarm-file') 265 .onClick(() => { 266 let rawFd: resourceManager.RawFileDescriptor | undefined = this.uiContext.getHostContext()?.resourceManager.getRawFdSync(fileName); 267 if (rawFd != undefined) { 268 try { 269 vibrator.startVibration({ 270 type: "file", 271 hapticFd: { fd: rawFd.fd, offset: rawFd.offset, length: rawFd.length } 272 }, { 273 id: 0, 274 usage: 'alarm' // The switch control is subject to the selected type. 275 }, (error: BusinessError) => { 276 if (error) { 277 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 278 return; 279 } 280 console.info('Succeed in starting vibration'); 281 }); 282 } catch (err) { 283 let e: BusinessError = err as BusinessError; 284 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 285 } 286 } 287 this.uiContext.getHostContext()?.resourceManager.closeRawFdSync(fileName); 288 }) 289 } 290 .width('100%') 291 } 292 .height('100%') 293 } 294 } 295 ``` 296 2973. Start vibration of the specified duration. 298 299 ```ts 300 import { vibrator } from '@kit.SensorServiceKit'; 301 import { BusinessError } from '@kit.BasicServicesKit'; 302 303 try { 304 vibrator.startVibration({ 305 type: 'time', 306 duration: 1000 307 }, { 308 id: 0, 309 usage: 'alarm' // The switch control is subject to the selected type. 310 }).then(() => { 311 console.info('Succeed in starting vibration'); 312 }, (error: BusinessError) => { 313 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 314 }); 315 } catch (err) { 316 let e: BusinessError = err as BusinessError; 317 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 318 } 319 ``` 320 321## vibrator.stopVibration<sup>9+</sup> 322 323stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback<void>): void 324 325Stops vibration in the specified mode. This API uses an asynchronous callback to return the result. 326 327**Required permissions**: ohos.permission.VIBRATE 328 329**System capability**: SystemCapability.Sensors.MiscDevice 330 331**Parameters** 332 333| Name | Type | Mandatory| Description | 334| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 335| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes | Mode to stop the vibration. The options are as follows:<br>- **VIBRATOR_STOP_MODE_TIME**: used to stop vibration of the specified duration.<br>- **VIBRATOR_STOP_MODE_PRESET**: used to stop vibration of the preset effect.<br>To stop custom vibration, use [vibrator.stopVibration<sup>10+</sup>](#vibratorstopvibration10).| 336| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the vibration stops, **err** is **undefined**; otherwise, **err** is an error object.| 337 338**Error codes** 339 340For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 341 342| ID| Error Message | 343| -------- | ------------------------------------------------------------ | 344| 201 | Permission denied. | 345| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 346 347**Example** 348 3491. Stop vibration of the specified duration. 350 351 ```ts 352 import { vibrator } from '@kit.SensorServiceKit'; 353 import { BusinessError } from '@kit.BasicServicesKit'; 354 355 try { 356 // Start vibration of the specified duration. 357 vibrator.startVibration({ 358 type: 'time', 359 duration: 1000, 360 }, { 361 id: 0, 362 usage: 'alarm' // The switch control is subject to the selected type. 363 }, (error: BusinessError) => { 364 if (error) { 365 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 366 return; 367 } 368 console.info('Succeed in starting vibration'); 369 }); 370 } catch (err) { 371 let e: BusinessError = err as BusinessError; 372 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 373 } 374 375 try { 376 // Stop vibration in VIBRATOR_STOP_MODE_TIME mode. 377 vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, (error: BusinessError) => { 378 if (error) { 379 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 380 return; 381 } 382 console.info('Succeed in stopping vibration'); 383 }) 384 } catch (err) { 385 let e: BusinessError = err as BusinessError; 386 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 387 } 388 ``` 389 3902. Stop vibration with the preset effect. 391 392 ```ts 393 import { vibrator } from '@kit.SensorServiceKit'; 394 import { BusinessError } from '@kit.BasicServicesKit'; 395 396 try { 397 // Start vibration with a preset effect. 398 vibrator.startVibration({ 399 type: 'preset', 400 effectId: 'haptic.notice.success', 401 count: 1, 402 }, { 403 id: 0, 404 usage: 'notification' // The switch control is subject to the selected type. 405 }, (error: BusinessError) => { 406 if (error) { 407 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 408 return; 409 } 410 console.info('Succeed in starting vibration'); 411 }); 412 } catch (err) { 413 let e: BusinessError = err as BusinessError; 414 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 415 } 416 417 try { 418 // Stop vibration in VIBRATOR_STOP_MODE_PRESET mode. 419 vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, (error: BusinessError) => { 420 if (error) { 421 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 422 return; 423 } 424 console.info('Succeed in stopping vibration'); 425 }) 426 } catch (err) { 427 let e: BusinessError = err as BusinessError; 428 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 429 } 430 ``` 431 432## vibrator.stopVibration<sup>9+</sup> 433 434stopVibration(stopMode: VibratorStopMode): Promise<void> 435 436Stops vibration in the specified mode. This API uses a promise to return the result. 437 438**Required permissions**: ohos.permission.VIBRATE 439 440**System capability**: SystemCapability.Sensors.MiscDevice 441 442**Parameters** 443 444| Name | Type | Mandatory| Description | 445| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 446| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes | Vibration stop mode:<br>- **VIBRATOR_STOP_MODE_TIME**: used to stop vibration of the specified duration.<br>- **VIBRATOR_STOP_MODE_PRESET**: used to stop vibration of the preset effect.<br>To stop custom vibration, use [vibrator.stopVibration<sup>10+</sup>](#vibratorstopvibration10-1).| 447 448**Return value** 449 450| Type | Description | 451| ------------------- | ------------- | 452| Promise<void> | Promise that returns no value.| 453 454**Error codes** 455 456For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 457 458| ID| Error Message | 459| -------- | ------------------------------------------------------------ | 460| 201 | Permission denied. | 461| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 462 463**Example** 464 4651. Stop vibration of the specified duration. 466 467 ```ts 468 import { vibrator } from '@kit.SensorServiceKit'; 469 import { BusinessError } from '@kit.BasicServicesKit'; 470 471 try { 472 // Start vibration of the specified duration. 473 vibrator.startVibration({ 474 type: 'time', 475 duration: 1000, 476 }, { 477 id: 0, 478 usage: 'alarm' // The switch control is subject to the selected type. 479 }).then(() => { 480 console.info('Succeed in starting vibration'); 481 }, (error: BusinessError) => { 482 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 483 }); 484 } catch (err) { 485 let e: BusinessError = err as BusinessError; 486 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 487 } 488 489 try { 490 // Stop vibration in VIBRATOR_STOP_MODE_TIME mode. 491 vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME).then(() => { 492 console.info('Succeed in stopping vibration'); 493 }, (error: BusinessError) => { 494 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 495 }); 496 } catch (err) { 497 let e: BusinessError = err as BusinessError; 498 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 499 } 500 ``` 501 5022. Stop vibration with the preset effect. 503 504 ```ts 505 import { vibrator } from '@kit.SensorServiceKit'; 506 import { BusinessError } from '@kit.BasicServicesKit'; 507 508 try { 509 // Start vibration with a preset effect. 510 vibrator.startVibration({ 511 type: 'preset', 512 effectId: 'haptic.notice.success', 513 count: 1, 514 }, { 515 id: 0, 516 usage: 'notification' // The switch control is subject to the selected type. 517 }).then(() => { 518 console.info('Succeed in starting vibration'); 519 }, (error: BusinessError) => { 520 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 521 }); 522 } catch (err) { 523 let e: BusinessError = err as BusinessError; 524 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 525 } 526 527 try { 528 // Stop vibration in VIBRATOR_STOP_MODE_PRESET mode. 529 vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => { 530 console.info('Succeed in stopping vibration'); 531 }, (error: BusinessError) => { 532 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 533 }); 534 } catch (err) { 535 let e: BusinessError = err as BusinessError; 536 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 537 } 538 ``` 539 540## vibrator.stopVibration<sup>10+</sup> 541 542stopVibration(callback: AsyncCallback<void>): void 543 544Stops vibration in all modes. This API uses an asynchronous callback to return the result. 545 546**Required permissions**: ohos.permission.VIBRATE 547 548**Atomic service API**: This API can be used in atomic services since API version 11. 549 550**System capability**: SystemCapability.Sensors.MiscDevice 551 552**Parameters** 553 554| Name | Type | Mandatory| Description | 555| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 556| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the vibration stops, **err** is **undefined**; otherwise, **err** is an error object.| 557 558**Error codes** 559 560For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 561 562| ID| Error Message | 563| -------- | ------------------ | 564| 201 | Permission denied. | 565 566**Example** 567 568 ```ts 569 import { vibrator } from '@kit.SensorServiceKit'; 570 import { BusinessError } from '@kit.BasicServicesKit'; 571 572 try { 573 // Stop vibration in all modes. 574 vibrator.stopVibration((error: BusinessError) => { 575 if (error) { 576 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 577 return; 578 } 579 console.info('Succeed in stopping vibration'); 580 }) 581 } catch (error) { 582 let e: BusinessError = error as BusinessError; 583 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 584 } 585 ``` 586 587## vibrator.stopVibration<sup>10+</sup> 588 589stopVibration(): Promise<void> 590 591Stops vibration in all modes. This API uses a promise to return the result. 592 593**Required permissions**: ohos.permission.VIBRATE 594 595**Atomic service API**: This API can be used in atomic services since API version 11. 596 597**System capability**: SystemCapability.Sensors.MiscDevice 598 599**Return value** 600 601| Type | Description | 602| ------------------- | ------------- | 603| Promise<void> | Promise that returns no value.| 604 605**Error codes** 606 607For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 608 609| ID| Error Message | 610| -------- | ------------------ | 611| 201 | Permission denied. | 612 613**Example** 614 615 ```ts 616 import { vibrator } from '@kit.SensorServiceKit'; 617 import { BusinessError } from '@kit.BasicServicesKit'; 618 619 try { 620 // Stop vibration in all modes. 621 vibrator.stopVibration().then(() => { 622 console.info('Succeed in stopping vibration'); 623 }, (error: BusinessError) => { 624 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 625 }); 626 } catch (error) { 627 let e: BusinessError = error as BusinessError; 628 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 629 } 630 ``` 631 632## vibrator.stopVibration<sup>19+</sup> 633 634stopVibration(param?: VibratorInfoParam): Promise<void> 635 636Stops vibration based on the specified vibrator parameters. If no parameters are passed, this API stops all vibrators of the local device by default. This API uses a promise to return the result. 637 638**Required permissions**: ohos.permission.VIBRATE 639 640**System capability**: SystemCapability.Sensors.MiscDevice 641 642**Parameters** 643 644| Name | Type | Mandatory| Description | 645| -------- | ------------------------------------------------------------ | ---- |-----------------------------------| 646| param | [VibratorInfoParam](#vibratorinfoparam19) | No | Vibrator parameters, such as the specified device and vibrator. If this parameter is left unspecified, this API applies to all vibrators of the local device by default.| 647 648**Return value** 649 650| Type | Description | 651| ------------------- | ------------- | 652| Promise<void> | Promise that returns no value.| 653 654**Error codes** 655 656For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 657 658| ID| Error Message | 659| -------- | ------------------ | 660| 201 | Permission denied. | 661| 14600101 | Device operation failed. | 662 663**Example** 664 665 ```ts 666 import { vibrator } from '@kit.SensorServiceKit'; 667 import { BusinessError } from '@kit.BasicServicesKit'; 668 669 try { 670 vibrator.stopVibration({ deviceId: 1, vibratorId: 3 }).then(() => { 671 console.info('Succeed in stopping vibration'); 672 }, (error: BusinessError) => { 673 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 674 }); 675 } catch (error) { 676 let e: BusinessError = error as BusinessError; 677 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 678 } 679 ``` 680 681## vibrator.stopVibrationSync<sup>12+</sup> 682 683stopVibrationSync(): void 684 685Stops any form of motor vibration. 686 687**Required permissions**: ohos.permission.VIBRATE 688 689**Atomic service API**: This API can be used in atomic services since API version 12. 690 691**System capability**: SystemCapability.Sensors.MiscDevice 692 693**Error codes** 694 695For details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md) and [Universal Error Codes](../errorcode-universal.md). 696 697| ID| Error Message | 698| -------- | ------------------------ | 699| 201 | Permission denied. | 700| 14600101 | Device operation failed. | 701 702**Example** 703 704 ```ts 705 import { vibrator } from '@kit.SensorServiceKit'; 706 import { BusinessError } from '@kit.BasicServicesKit'; 707 708 try { 709 // Stop any form of motor vibration. 710 vibrator.stopVibrationSync() 711 console.info('Succeed in stopping vibration'); 712 } catch (error) { 713 let e: BusinessError = error as BusinessError; 714 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 715 } 716 ``` 717 718## vibrator.isSupportEffect<sup>10+</sup> 719 720isSupportEffect(effectId: string, callback: AsyncCallback<boolean>): void 721 722Checks whether an effect ID is supported. This API uses an asynchronous callback to return the result. 723 724**System capability**: SystemCapability.Sensors.MiscDevice 725 726**Parameters** 727 728| Name | Type | Mandatory| Description | 729| -------- | ---------------------------- | ---- | ----------------------------------------------------------- | 730| effectId | string | Yes | ID of the preset vibration effect. | 731| callback | AsyncCallback<boolean> | Yes | Callback used to return the result. The value **true** means that the effect ID is supported, and the value **false** means the opposite.| 732 733**Error codes** 734 735For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 736 737| ID| Error Message | 738| -------- | ------------------------------------------------------------ | 739| 201 | Permission denied. | 740| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 741 742**Example** 743 744 ```ts 745 import { vibrator } from '@kit.SensorServiceKit'; 746 import { BusinessError } from '@kit.BasicServicesKit'; 747 748 try { 749 // Check whether 'haptic.notice.success' is supported. 750 vibrator.isSupportEffect('haptic.notice.success', (err: BusinessError, state: boolean) => { 751 if (err) { 752 console.error(`Failed to query effect. Code: ${err.code}, message: ${err.message}`); 753 return; 754 } 755 console.info('Succeed in querying effect'); 756 if (state) { 757 try { 758 // To use startVibration, you must configure the ohos.permission.VIBRATE permission. 759 vibrator.startVibration({ 760 type: 'preset', 761 effectId: 'haptic.notice.success', 762 count: 1, 763 }, { 764 usage: 'unknown' // The switch control is subject to the selected type. 765 }, (error: BusinessError) => { 766 if (error) { 767 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 768 } else { 769 console.info('Succeed in starting vibration'); 770 } 771 }); 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 } catch (error) { 779 let e: BusinessError = error as BusinessError; 780 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 781 } 782 ``` 783 784## vibrator.isSupportEffect<sup>10+</sup> 785 786isSupportEffect(effectId: string): Promise<boolean> 787 788Checks whether an effect ID is supported. This API uses a promise to return the result. 789 790**System capability**: SystemCapability.Sensors.MiscDevice 791 792**Parameters** 793 794| Name | Type | Mandatory| Description | 795| -------- | ------ | ---- | ---------------------- | 796| effectId | string | Yes | ID of the preset vibration effect.| 797 798**Return value** 799 800| Type | Description | 801| ---------------------- | ------------------------------------------------------------ | 802| Promise<boolean> | Promise that returns the result. The value **true** means that the effect ID is supported, and the value **false** means the opposite.| 803 804**Error codes** 805 806For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 807 808| ID| Error Message | 809| -------- | ------------------------------------------------------------ | 810| 201 | Permission denied. | 811| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 812 813**Example** 814 815 ```ts 816 import { vibrator } from '@kit.SensorServiceKit'; 817 import { BusinessError } from '@kit.BasicServicesKit'; 818 819 try { 820 // Check whether 'haptic.notice.success' is supported. 821 vibrator.isSupportEffect('haptic.notice.success').then((state: boolean) => { 822 console.info(`The query result is ${state}`); 823 if (state) { 824 try { 825 vibrator.startVibration({ 826 type: 'preset', 827 effectId: 'haptic.notice.success', 828 count: 1, 829 }, { 830 usage: 'unknown' // The switch control is subject to the selected type. 831 }).then(() => { 832 console.info('Succeed in starting vibration'); 833 }).catch((error: BusinessError) => { 834 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 835 }); 836 } catch (error) { 837 let e: BusinessError = error as BusinessError; 838 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 839 } 840 } 841 }, (error: BusinessError) => { 842 console.error(`Failed to query effect. Code: ${error.code}, message: ${error.message}`); 843 }) 844 } catch (error) { 845 let e: BusinessError = error as BusinessError; 846 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 847 } 848 ``` 849 850## vibrator.isSupportEffectSync<sup>12+</sup> 851 852isSupportEffectSync(effectId: string): boolean 853 854Checks whether the preset vibration effect is supported. 855 856**System capability**: SystemCapability.Sensors.MiscDevice 857 858**Parameters** 859 860| Name | Type | Mandatory| Description | 861| -------- | ------ | ---- | ---------------------- | 862| effectId | string | Yes | ID of the preset vibration effect.| 863 864**Return value** 865 866| Type | Description | 867| ------- | ----------------------------------------------------------- | 868| boolean | Returned object. The value **true** means that the effect ID is supported, and the value **false** means the opposite.| 869 870**Error codes** 871 872For details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md) and [Universal Error Codes](../errorcode-universal.md). 873 874| ID| Error Message | 875| -------- | ------------------------------------------------------------ | 876| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 877| 14600101 | Device operation failed. | 878 879**Example** 880 881 ```ts 882 import { vibrator } from '@kit.SensorServiceKit'; 883 import { BusinessError } from '@kit.BasicServicesKit'; 884 885 try { 886 // Check whether the preset 'haptic.notice.success' is supported. 887 let ret = vibrator.isSupportEffectSync('haptic.notice.success'); 888 console.info(`The query result is ${ret}`); 889 } catch (error) { 890 let e: BusinessError = error as BusinessError; 891 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 892 } 893 ``` 894 895## vibrator.getEffectInfoSync<sup>19+</sup> 896 897getEffectInfoSync(effectId: string, param?: VibratorInfoParam): EffectInfo; 898 899Obtains the preset vibration effect based on the device ID and vibrator ID to determine whether the preset vibration effect is supported. 900 901**System capability**: SystemCapability.Sensors.MiscDevice 902 903**Parameters** 904 905| Name | Type | Mandatory| Description | 906| -------- | ------------------------------------------------------------ | ---- |-----------------------------| 907| effectId | string | Yes | ID of the preset vibration effect. | 908| param | [VibratorInfoParam](#vibratorinfoparam19) | No | Device ID and vibrator ID. If this parameter is left unspecified, this API applies to the local device by default.| 909 910**Error codes** 911 912For details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md). 913 914| ID| Error Message | 915| -------- | ------------------------ | 916| 14600101 | Device operation failed. | 917 918**Return value** 919 920| Type | Description | 921| ------- | --------------------------------------------------------- | 922| [EffectInfo](#effectinfo19) | Whether the preset vibration effect is supported.| 923 924 925**Example** 926 927 ```ts 928 import { vibrator } from '@kit.SensorServiceKit'; 929 import { BusinessError } from '@kit.BasicServicesKit'; 930 931 try { 932 const effectInfo: vibrator.EffectInfo = vibrator.getEffectInfoSync('haptic.clock.timer', { deviceId: 1, vibratorId: 3}); 933 console.log(`isEffectSupported: ${effectInfo.isEffectSupported}`); 934 } catch (error) { 935 let e: BusinessError = error as BusinessError; 936 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 937 } 938 ``` 939 940 941## vibrator.getVibratorInfoSync<sup>19+</sup> 942 943getVibratorInfoSync(param?: VibratorInfoParam): Array<VibratorInfo>; 944 945Queries the vibrator list of one or all devices. 946 947**System capability**: SystemCapability.Sensors.MiscDevice 948 949**Parameters** 950 951| Name | Type | Mandatory| Description | 952| -------- |-----------------------------------------| ---- |-----------------------------------| 953| param | [VibratorInfoParam](#vibratorinfoparam19) | No | Vibrator parameters, such as the specified device and vibrator. If this parameter is left unspecified, this API applies to all vibrators of the local device by default.| 954 955**Return value** 956 957| Type | Description | 958|-------------------------------| --------------------------------------------------------- | 959| [VibratorInfo](#vibratorinfo19) | Vibrator information.| 960 961 962**Example** 963 964 ```ts 965 import { vibrator } from '@kit.SensorServiceKit'; 966 import { BusinessError } from '@kit.BasicServicesKit'; 967 968 try { 969 const vibratorInfoList: vibrator.VibratorInfo[] = vibrator.getVibratorInfoSync({ deviceId: 1, vibratorId: 3 }); 970 console.log(`vibratorInfoList: ${JSON.stringify(vibratorInfoList)}`); 971 } catch (error) { 972 let e: BusinessError = error as BusinessError; 973 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 974 } 975 ``` 976 977 978## vibrator.on<sup>19+</sup> 979 980on(type: 'vibratorStateChange', callback: Callback<VibratorStatusEvent>): void 981 982Enables listening for vibrator status changes. 983 984**System capability**: SystemCapability.Sensors.MiscDevice 985 986**Parameters** 987 988| Name | Type | Mandatory| Description | 989| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 990| type | 'vibratorStateChange' | Yes | Event type. The value **vibratorStateChange** indicates a vibrator online/offline event. | 991| callback | Callback<[VibratorStatusEvent](#vibratorstatusevent19)> | Yes | Callback used to return the vibrator status change event.| 992 993**Error codes** 994 995For details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md). 996 997| ID| Error Message | 998| -------- | ------------------------ | 999| 14600101 | Device operation failed. | 1000 1001 1002**Example** 1003 1004 ```ts 1005 import { vibrator } from '@kit.SensorServiceKit'; 1006 import { BusinessError } from '@kit.BasicServicesKit'; 1007 1008 // Callback 1009 const vibratorStateChangeCallback = (data: vibrator.VibratorStatusEvent) => { 1010 console.log('vibrator state callback info:', JSON.stringify(data)); 1011 } 1012 1013 try { 1014 // Subscribe to vibratorStateChange events. 1015 vibrator.on('vibratorStateChange', vibratorStateChangeCallback); 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 1023## vibrator.off<sup>19+</sup> 1024 1025off(type: 'vibratorStateChange', callback?: Callback<VibratorStatusEvent>): void 1026 1027Disables listening for vibrator status changes. 1028 1029**System capability**: SystemCapability.Sensors.MiscDevice 1030 1031**Parameters** 1032 1033| Name | Type | Mandatory| Description | 1034| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 1035| type | 'vibratorStateChange' | Yes | Event type. The value **vibratorStateChange** indicates a vibrator online/offline event. | 1036| callback | Callback<[VibratorStatusEvent](#vibratorstatusevent19)> | No | Callback used to return the vibrator status change event. If this parameter is not specified, all callbacks of vibrator status change events will be unregistered.| 1037 1038**Error codes** 1039 1040For details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md). 1041 1042| ID| Error Message | 1043| -------- | ------------------------ | 1044| 14600101 | Device operation failed. | 1045 1046 1047**Example** 1048 1049 ```ts 1050 import { vibrator } from '@kit.SensorServiceKit'; 1051 import { BusinessError } from '@kit.BasicServicesKit'; 1052 1053 // Callback 1054 const vibratorStateChangeCallback = (data: vibrator.VibratorStatusEvent) => { 1055 console.log('vibrator state callback info:', JSON.stringify(data)); 1056 } 1057 try { 1058 // Unsubscribe from specified vibratorStateChange events. 1059 vibrator.off('vibratorStateChange', vibratorStateChangeCallback); 1060 // Unsubscribe from all vibratorStateChange events. 1061 // vibrator.off('vibratorStateChange'); 1062 } catch (error) { 1063 let e: BusinessError = error as BusinessError; 1064 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 1065 } 1066 ``` 1067 1068 1069## VibratorStatusEvent<sup>19+</sup> 1070 1071Defines the vibrator status change event. 1072 1073**System capability**: SystemCapability.Sensors.MiscDevice 1074 1075 1076| Name| Type | Description | 1077| ---- | ------ |----------------------------------| 1078| timestamp | number | Event timestamp. | 1079| deviceId | number | Device ID. | 1080| vibratorCount | number | Number of vibrators on the device. | 1081| isVibratorOnline | boolean | Vibrator status. The value **true** indicates that the device is online, and the value **false** indicates the opposite.| 1082 1083 1084## VibratorInfoParam<sup>19+</sup> 1085 1086Defines the vibrator parameters. If **VibratorInfoParam** is left unspecified, an API applies to all vibrators of the local device by default. 1087 1088**System capability**: SystemCapability.Sensors.MiscDevice 1089 1090 1091| Name| Type | Read-Only| Optional| Description | 1092| ---- | ------ | ---- | ---- |------------------------------------------------------------| 1093| deviceId | number | No | Yes | Device ID. The default value is **-1**, which indicates the local device. You can use [getEffectInfoSync](#vibratorgeteffectinfosync19) to query other device IDs.| 1094| vibratorId | number | No | Yes | Vibrator ID. The default value is **-1**, which indicates all vibrator of the local device. You can use [getEffectInfoSync](#vibratorgeteffectinfosync19) to query other vibrator IDs. | 1095 1096 1097 1098## EffectInfo<sup>19+</sup> 1099 1100Defines the preset effect. 1101 1102**System capability**: SystemCapability.Sensors.MiscDevice 1103 1104 1105| Name| Type | Description | 1106| ---- | ------ |------------| 1107| isEffectSupported | boolean | Whether the preset effect is supported. The value **true** indicates that the preset effect is supported, and the value **false** indicates the opposite.| 1108 1109 1110## VibratorInfo<sup>19+</sup> 1111 1112Defines the vibrator information. 1113 1114| Name| Type | Description | 1115| ---- | ------ |-----------| 1116| deviceId | number | Device ID. | 1117| vibratorId | number | Vibrator ID. | 1118| deviceName | string | Device name. | 1119| isHdHapticSupported | boolean | Whether HD vibration is supported.| 1120| isLocalVibrator | boolean | Whether the device is a local device. | 1121 1122 1123## vibrator.isHdHapticSupported<sup>12+</sup> 1124 1125isHdHapticSupported(): boolean 1126 1127Checks whether HD vibration is supported. 1128 1129**System capability**: SystemCapability.Sensors.MiscDevice 1130 1131**Return value** 1132 1133| Type | Description | 1134| ------- | --------------------------------------------------------- | 1135| 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.| 1136 1137**Error codes** 1138 1139For details about the error codes, see [Vibrator Error Codes](errorcode-vibrator.md). 1140 1141| ID| Error Message | 1142| -------- | ------------------------ | 1143| 14600101 | Device operation failed. | 1144 1145**Example** 1146 1147 ```ts 1148 import { vibrator } from '@kit.SensorServiceKit'; 1149 import { BusinessError } from '@kit.BasicServicesKit'; 1150 1151 try { 1152 // Check whether HD vibration is supported. 1153 let ret = vibrator.isHdHapticSupported(); 1154 console.info(`The query result is ${ret}`); 1155 } catch (error) { 1156 let e: BusinessError = error as BusinessError; 1157 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 1158 } 1159 ``` 1160 1161## VibratorPatternBuilder<sup>18+</sup> 1162 1163### vibrator('addContinuousEvent')<sup>18+</sup> 1164 1165addContinuousEvent(time: number, duration: number, options?: ContinuousParam): VibratorPatternBuilder; 1166 1167Adds a long vibration event as a **VibratorPattern** object. 1168 1169**System capability**: SystemCapability.Sensors.MiscDevice 1170 1171**Parameters** 1172 1173| Name | Type | Mandatory| Description | 1174| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 1175| time | number | Yes | Start time of the long vibration, in ms. The value range is (0,1800000).| 1176| duration | number | Yes | Duration of the long vibration, in ms. The value range is (0,5000].| 1177| options | [ContinuousParam](#continuousparam18) | No | Optional parameters. | 1178 1179**Return value** 1180 1181| Type | Description | 1182| ---------------------- | ---------------------------------------------------- | 1183| VibratorPatternBuilder | **VibratorPatternBuilder** object representing a long vibration event.| 1184 1185**Error codes** 1186 1187For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1188 1189| ID| Error Message | 1190| -------- | ---------------- | 1191| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1192 1193**Example** 1194 1195 ```ts 1196 import { vibrator } from '@kit.SensorServiceKit'; 1197 import { BusinessError } from '@kit.BasicServicesKit'; 1198 1199 let builder = new vibrator.VibratorPatternBuilder(); 1200 try { 1201 let pointsMe: vibrator.VibratorCurvePoint[] = [ 1202 { time: 0, intensity: 0, frequency: -7 }, 1203 { time: 42, intensity: 1, frequency: -6 }, 1204 { time: 128, intensity: 0.94, frequency: -4 }, 1205 { time: 217, intensity: 0.63, frequency: -14 }, 1206 { time: 763, intensity: 0.48, frequency: -14 }, 1207 { time: 1125, intensity: 0.53, frequency: -10 }, 1208 { time: 1503, intensity: 0.42, frequency: -14 }, 1209 { time: 1858, intensity: 0.39, frequency: -14 }, 1210 { time: 2295, intensity: 0.34, frequency: -17 }, 1211 { time: 2448, intensity: 0.21, frequency: -14 }, 1212 { time: 2468, intensity: 0, frequency: -21 } 1213 ] // No less than four VibratorCurvePoint objects must be set. The maximum value is 16. 1214 let param: vibrator.ContinuousParam = { 1215 intensity: 97, 1216 frequency: 34, 1217 points:pointsMe, 1218 index: 0 1219 } 1220 builder.addContinuousEvent(0, 2468, param); 1221 console.info(`addContinuousEvent builder is ${builder.build()}`); 1222 } catch(error) { 1223 let e: BusinessError = error as BusinessError; 1224 console.error(`Exception. Code ${e.code}`); 1225 } 1226 ``` 1227 1228### vibrator('addTransientEvent')<sup>18+</sup> 1229 1230addTransientEvent(time: number, options?: TransientParam): VibratorPatternBuilder; 1231 1232Adds a short vibration event as a **VibratorPattern** object. 1233 1234**System capability**: SystemCapability.Sensors.MiscDevice 1235 1236**Parameters** 1237 1238| Name | Type | Mandatory| Description | 1239| ------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 1240| time | number | Yes | Start time of the long vibration, in ms. The value range is (0,1800000).| 1241| options | [TransientParam](#transientparam18) | No | Optional parameters. | 1242 1243**Return value** 1244 1245| Type | Description | 1246| ---------------------- | ------------------------------------------------ | 1247| VibratorPatternBuilder | **VibratorPatternBuilder** object representing a short vibration event.| 1248 1249**Error codes** 1250 1251For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1252 1253| ID| Error Message | 1254| -------- | ---------------- | 1255| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1256 1257**Example** 1258 1259 ```ts 1260 import { vibrator } from '@kit.SensorServiceKit'; 1261 import { BusinessError } from '@kit.BasicServicesKit'; 1262 1263 let builder = new vibrator.VibratorPatternBuilder(); 1264 try { 1265 let param: vibrator.TransientParam = { 1266 intensity: 80, 1267 frequency: 70, 1268 index: 0 1269 } 1270 builder.addTransientEvent(0, param); 1271 console.log(`addTransientEvent builder is ${builder.build()}`); 1272 } catch(error) { 1273 let e: BusinessError = error as BusinessError; 1274 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 1275 } 1276 ``` 1277 1278### vibrator('build')<sup>18+</sup> 1279 1280build(): VibratorPattern; 1281 1282Constructor used to create a **VibratorPattern** object, which determines the vibration sequence of short or long events. 1283 1284**System capability**: SystemCapability.Sensors.MiscDevice 1285 1286**Return value** 1287 1288| Type | Description | 1289| ------------------------------------- | ---------------------------------- | 1290| [VibratorPattern](#vibratorpattern18) | **VibratorPattern** object.| 1291 1292**Example** 1293 1294 ```ts 1295 import { vibrator } from '@kit.SensorServiceKit'; 1296 import { BusinessError } from '@kit.BasicServicesKit'; 1297 1298 let builder = new vibrator.VibratorPatternBuilder(); 1299 try { 1300 let param: vibrator.TransientParam = { 1301 intensity: 80, 1302 frequency: 70, 1303 index: 0 1304 } 1305 builder.addTransientEvent(0, param); 1306 console.log(`addTransientEvent builder is ${builder.build()}`); 1307 } catch(error) { 1308 let e: BusinessError = error as BusinessError; 1309 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 1310 } 1311 try { 1312 vibrator.startVibration({ 1313 type: "pattern", 1314 pattern: builder.build() 1315 }, { 1316 usage: "alarm", // The switch control is subject to the selected type. 1317 }, (error) => { 1318 if (error) { 1319 let e: BusinessError = error as BusinessError; 1320 console.error(`Vibrate fail. Code: ${e.code}, message: ${e.message}`); 1321 } else { 1322 console.info(`vibrate success`); 1323 } 1324 }); 1325 } catch(error) { 1326 let e: BusinessError = error as BusinessError; 1327 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 1328 } 1329 ``` 1330 1331## EffectId 1332 1333Enumerates the preset vibration effect IDs. This parameter is needed when you call [vibrator.startVibration9+](#vibratorstartvibration9) or [vibrator.stopVibration9+](#vibratorstopvibration9-1) to deliver the vibration effect specified by [VibratePreset](#vibratepreset9). This parameter supports a variety of values, such as **haptic.clock.timer**. [HapticFeedback<sup>12+</sup>](#hapticfeedback12) provides several frequently used **EffectId** values. 1334 1335> **NOTE** 1336> 1337> Preset effects vary according to devices. You are advised to call [vibrator.isSupportEffect](#vibratorissupporteffect10-1)<sup>10+</sup> to check whether the device supports the preset effect before use. 1338 1339**System capability**: SystemCapability.Sensors.MiscDevice 1340 1341| Name | Value | Description | 1342| ----------- | -------------------- | ---------------------------- | 1343| EFFECT_CLOCK_TIMER | 'haptic.clock.timer' | Vibration effect when a user adjusts the timer.| 1344 1345## HapticFeedback<sup>12+</sup> 1346 1347Defines the vibration effect. The frequency of the same vibration effect may vary depending on the vibrator, but the frequency trend remains consistent. These vibration effects correspond to the specific **EffectId** values. For details, see the sample code that demonstrates how to use [vibrator.startVibration9+](#vibratorstartvibration9) or [vibrator.stopVibration9+](#vibratorstopvibration9-1) to deliver the vibration effect defined by [VibratePreset](#vibratepreset9). 1348 1349**System capability**: SystemCapability.Sensors.MiscDevice 1350 1351| Name | Value | Description | 1352| ----------------------------------- | ----------------------- | ---------------------------- | 1353| EFFECT_SOFT | 'haptic.effect.soft' | Soft vibration, low frequency.| 1354| EFFECT_HARD | 'haptic.effect.hard' | Hard vibration, medium frequency.| 1355| EFFECT_SHARP | 'haptic.effect.sharp' | Sharp vibration, high frequency.| 1356| EFFECT_NOTICE_SUCCESS<sup>18+</sup> | 'haptic.notice.success' | Vibration for a success notification. | 1357| EFFECT_NOTICE_FAILURE<sup>18+</sup> | 'haptic.notice.fail' | Vibration for a failure notification. | 1358| EFFECT_NOTICE_WARNING<sup>18+</sup> | 'haptic.notice.warning' | Vibration for an alert. | 1359 1360## VibratorStopMode 1361 1362Enumerates vibration stop modes. This parameter is required for [vibrator.stopVibration9+](#vibratorstopvibration9) or [vibrator.stopVibration9+](#vibratorstopvibration9-1). The stop mode must match that delivered in [VibrateEffect9+](#vibrateeffect9). 1363 1364**System capability**: SystemCapability.Sensors.MiscDevice 1365 1366| Name | Value | Description | 1367| ------------------------- | -------- | ------------------------ | 1368| VIBRATOR_STOP_MODE_TIME | 'time' | The vibration to stop is in **duration** mode.| 1369| VIBRATOR_STOP_MODE_PRESET | 'preset' | The vibration to stop is in **EffectId** mode.| 1370 1371## VibrateEffect<sup>9+</sup> 1372 1373Describes the vibration effect. This parameter is required for [vibrator.startVibration9+](#vibratorstartvibration9) or [vibrator.startVibration9+](#vibratorstartvibration9-1). 1374 1375**System capability**: SystemCapability.Sensors.MiscDevice 1376 1377| Type | Description | 1378| ------------------------------------- | ------------------------------------------------------------ | 1379| [VibrateTime](#vibratetime9) | Start vibration of the specified duration.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 1380| [VibratePreset](#vibratepreset9) | Vibration with a preset effect. | 1381| [VibrateFromFile](#vibratefromfile10) | Vibration according to a custom vibration configuration file. | 1382| VibrateFromPattern<sup>18+</sup> | Triggers vibration with the custom effect. This API uses an asynchronous callback to return the result. | 1383 1384## VibrateTime<sup>9+</sup> 1385 1386Represents vibration of the specified duration. 1387 1388**Atomic service API**: This API can be used in atomic services since API version 11. 1389 1390**System capability**: SystemCapability.Sensors.MiscDevice 1391 1392| Name | Type | Mandatory| Description | 1393| -------- | ------ | ---- | ----------------------------------------------------------- | 1394| type | 'time' | Yes | The value is **time**, indicating vibration of the specified duration. | 1395| duration | number | Yes | Vibration duration, in ms. The value range is (0,1800000].| 1396 1397## VibratePreset<sup>9+</sup> 1398 1399Represents the preset vibration effect. You can pass this value to [VibrateEffect9+](#vibrateeffect9) to specify a preset vibration effect when calling [vibrator.startVibration9+](#vibratorstartvibration9) or [vibrator.startVibration9+](#vibratorstartvibration9-1). 1400 1401**System capability**: SystemCapability.Sensors.MiscDevice 1402 1403| Name | Type | Mandatory| Description | 1404| ----------------------- | -------- | ---- | ------------------------------------------------------------ | 1405| type | 'preset' | Yes | The value **preset** means vibration with the specified effect. | 1406| effectId | string | Yes | Preset vibration effect ID. | 1407| count | number | No | Number of repeated vibrations. This parameter is optional. The default value is **1**. | 1408| 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.| 1409 1410## VibrateFromFile<sup>10+</sup> 1411 1412Represents a custom vibration pattern. It is supported only by certain devices. An error code will be returned if a device does not support this vibration mode. You can pass this value to [VibrateEffect9+](#vibrateeffect9) to specify a custom vibration pattern when calling [vibrator.startVibration9+](#vibratorstartvibration9) or [vibrator.startVibration9+](#vibratorstartvibration9-1). 1413 1414**System capability**: SystemCapability.Sensors.MiscDevice 1415 1416| Name | Type | Mandatory| Description | 1417| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------ | 1418| type | 'file' | Yes | The value **file** means vibration according to a vibration configuration file.| 1419| hapticFd | [HapticFileDescriptor](#hapticfiledescriptor10)<sup>10+</sup> | Yes | File descriptor (FD) of the vibration configuration file. | 1420 1421## HapticFileDescriptor<sup>10+</sup> 1422 1423Describes 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). 1424 1425**System capability**: SystemCapability.Sensors.MiscDevice 1426 1427| Name | Type | Mandatory| Description | 1428| ------ | ------ | ---- | ------------------------------------------------------------ | 1429| fd | number | Yes | FD of the custom vibration configuration file. | 1430| 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.| 1431| 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.| 1432 1433## VibratorEventType<sup>18+</sup> 1434 1435Vibration event type. 1436 1437**System capability**: SystemCapability.Sensors.MiscDevice 1438 1439| Name | Type | Mandatory| Description | 1440| ---------- | ------ | ---- | ----------------- | 1441| CONTINUOUS | number | Yes | The value **0** indicates long vibration.| 1442| TRANSIENT | number | Yes | The value **1** indicates short vibration.| 1443 1444## VibratorCurvePoint<sup>18+</sup> 1445 1446Defines the gain relative to the vibration intensity. 1447 1448**System capability**: SystemCapability.Sensors.MiscDevice 1449 1450| Name | Type | Mandatory| Description | 1451| --------- | ------ | ---- | ------------------------------------------------------------ | 1452| time | number | Yes | Start time offset. | 1453| intensity | number | No | Gain relative to the vibration intensity. This parameter is optional. The value range is [0,100%]. If this parameter is left empty, the default value is **1**.| 1454| 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**.| 1455 1456## VibratorEvent<sup>18+</sup> 1457 1458Vibration event. 1459 1460**System capability**: SystemCapability.Sensors.MiscDevice 1461 1462| Name | Type | Mandatory| Description | 1463| --------- | ------------------------------- | ---- | ------------------------------------------------------------ | 1464| eventType | VibratorEventType | Yes | Vibration event type. | 1465| time | number | Yes | Vibration start time, in ms. The value range is [0,1800000]. | 1466| 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.| 1467| 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**.| 1468| 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**.| 1469| index | number | No | Channel number. This parameter is optional. The value range is [0,2]. If this parameter is left empty, the default value is **0**. | 1470| points | Array<[VibratorCurvePoint](#vibratorcurvepoint18)> | No | Adjustment points of the vibration curve. | 1471 1472## VibratorPattern<sup>18+</sup> 1473 1474Defines the vibration sequence. 1475 1476**System capability**: SystemCapability.Sensors.MiscDevice 1477 1478| Name | Type | Mandatory| Description | 1479| ------ | -------------------------- | ---- | ---------------------------------------------------- | 1480| time | number | Yes | Absolute vibration start time. | 1481| events | Array<[VibratorEvent](#vibratorevent18)> | Yes | Vibration event array, which is the **VibratorPattern** object returned by **build() **.| 1482 1483## ContinuousParam<sup>18+</sup> 1484 1485Defines the parameters for continuous vibration. 1486 1487**System capability**: SystemCapability.Sensors.MiscDevice 1488 1489| Name | Type | Mandatory| Description | 1490| --------- | -------------------- | ---- | ------------------------------------------------------------ | 1491| 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**.| 1492| 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**.| 1493| points | [VibratorCurvePoint](#vibratorcurvepoint18)[] | No | Adjustment points of the vibration curve. | 1494| index | number | No | Channel number. This parameter is optional. The value range is [0,2]. If this parameter is left empty, the default value is **0**. | 1495 1496## TransientParam<sup>18+</sup> 1497 1498Defines the parameters for transient vibration. 1499 1500**System capability**: SystemCapability.Sensors.MiscDevice 1501 1502| Name | Type | Mandatory| Description | 1503| --------- | ------ | ---- | ------------------------------------------------------------ | 1504| 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**.| 1505| 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**.| 1506| index | number | No | Channel number. This parameter is optional. The value range is [0,2]. If this parameter is left empty, the default value is **0**. | 1507 1508## VibrateFromPattern<sup>18+</sup> 1509 1510Defines the custom vibration effect. 1511 1512**System capability**: SystemCapability.Sensors.MiscDevice 1513 1514| Name | Type | Mandatory| Description | 1515| ------- | --------------- | ---- | ---------------------------------------------------- | 1516| type | 'pattern' | Yes | If the value is **pattern**, the vibrator vibrates based on the specified pattern. | 1517| pattern | VibratorPattern | Yes | Vibration event array, which is the **VibratorPattern** object returned by **build() **.| 1518 1519## VibrateAttribute<sup>9+</sup> 1520 1521Describes the vibration attribute. 1522 1523**Atomic service API**: This API can be used in atomic services since API version 11. 1524 1525**System capability**: SystemCapability.Sensors.MiscDevice 1526 1527| Name | Type | Mandatory| Description | 1528|------------------------| ---------------- | ---- | ------------------------------------------------------------ | 1529| id | number | No | Vibrator ID. The default value is **0**. | 1530| deviceId<sup>19+</sup> | number | No | Device ID. | 1531| usage | [Usage](#usage9) | Yes | Vibration scenario. The default value is **unknown**. The value must be an enum defined in [Usage](#usage9).| 1532 1533## Usage<sup>9+</sup> 1534 1535type Usage = 'unknown' | 'alarm' | 'ring' | 'notification' | 'communication' | 'touch' | 'media' | 'physicalFeedback' | 'simulateReality' 1536 1537Enumerates the vibration scenarios. 1538 1539**Atomic service API**: This API can be used in atomic services since API version 11. 1540 1541**System capability**: SystemCapability.Sensors.MiscDevice 1542<!--RP1--> 1543 1544| Type | Description | 1545| ------------------ | ------------------------------------------------------- | 1546| 'unknown' | Unknown scenario, with the lowest priority. This parameter has a fixed value of **unknown**.| 1547| 'alarm' | Vibration for alarms. This parameter has a fixed value of **alarm**. | 1548| 'ring' | Vibration for ringing. This parameter has a fixed value of **ring**. | 1549| 'notification' | Vibration for notification. This parameter has a fixed value of **notification**. | 1550| 'communication' | Vibration for communication. This parameter has a fixed value of **communication**. | 1551| 'touch' | Vibration for touch. This parameter has a fixed value of **touch**. | 1552| 'media' | Vibration for media. This parameter has a fixed value of **media**. | 1553| 'physicalFeedback' | Vibration for physical feedback. This parameter has a fixed value of **physicalFeedback**. | 1554| 'simulateReality' | Vibration for simulated reality. This parameter has a fixed value of **simulateReality**. | 1555 1556<!--RP1End--> 1557 1558## vibrator.vibrate<sup>(deprecated)</sup> 1559 1560vibrate(duration: number): Promise<void> 1561 1562Triggers vibration with the specified duration. This API uses a promise to return the result. 1563 1564This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9-1)<sup>9+</sup> instead. 1565 1566**Required permissions**: ohos.permission.VIBRATE 1567 1568**System capability**: SystemCapability.Sensors.MiscDevice 1569 1570**Parameters** 1571 1572| Name | Type | Mandatory| Description | 1573| -------- | ------ | ---- | ------------------------------------------------------------ | 1574| duration | number | Yes | Vibration duration, in ms. The value range is (0,1800000].| 1575 1576**Return value** 1577 1578| Type | Description | 1579| ------------------- | ------------- | 1580| Promise<void> | Promise that returns no value.| 1581 1582**Example** 1583 1584 ```ts 1585 import { vibrator } from '@kit.SensorServiceKit'; 1586 import { BusinessError } from '@kit.BasicServicesKit'; 1587 1588 vibrator.vibrate(1000).then(() => { 1589 console.info('Succeed in vibrating'); 1590 }, (error: BusinessError) => { 1591 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 1592 }); 1593 ``` 1594 1595## vibrator.vibrate<sup>(deprecated)</sup> 1596 1597vibrate(duration: number, callback?: AsyncCallback<void>): void 1598 1599Triggers vibration with the specified duration. This API uses an asynchronous callback to return the result. 1600 1601This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9)<sup>9+</sup> instead. 1602 1603**Required permissions**: ohos.permission.VIBRATE 1604 1605**System capability**: SystemCapability.Sensors.MiscDevice 1606 1607**Parameters** 1608 1609| Name | Type | Mandatory| Description | 1610| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 1611| duration | number | Yes | Vibration duration, in ms. The value range is (0,1800000].| 1612| callback | AsyncCallback<void> | No | Callback used to return the result. If the vibration starts, **err** is **undefined**; otherwise, **err** is an error object. | 1613 1614**Example** 1615 1616 ```ts 1617 import { vibrator } from '@kit.SensorServiceKit'; 1618 import { BusinessError } from '@kit.BasicServicesKit'; 1619 1620 vibrator.vibrate(1000, (error: BusinessError) => { 1621 if (error) { 1622 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 1623 } else { 1624 console.info('Succeed in vibrating'); 1625 } 1626 }) 1627 ``` 1628 1629 1630## vibrator.vibrate<sup>(deprecated)</sup> 1631 1632vibrate(effectId: EffectId): Promise<void> 1633 1634Triggers vibration with the specified effect. This API uses a promise to return the result. 1635 1636This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9-1)<sup>9+</sup> instead. 1637 1638**Required permissions**: ohos.permission.VIBRATE 1639 1640**System capability**: SystemCapability.Sensors.MiscDevice 1641 1642**Parameters** 1643 1644| Name | Type | Mandatory| Description | 1645| -------- | --------------------- | ---- | ------------------ | 1646| effectId | [EffectId](#effectid) | Yes | Preset vibration effect ID.| 1647 1648**Return value** 1649 1650| Type | Description | 1651| ------------------- | ------------- | 1652| Promise<void> | Promise that returns no value.| 1653 1654**Example** 1655 1656 ```ts 1657 import { vibrator } from '@kit.SensorServiceKit'; 1658 import { BusinessError } from '@kit.BasicServicesKit'; 1659 1660 vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(() => { 1661 console.info('Succeed in vibrating'); 1662 }, (error: BusinessError) => { 1663 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 1664 }); 1665 ``` 1666 1667 1668## vibrator.vibrate<sup>(deprecated)</sup> 1669 1670vibrate(effectId: EffectId, callback?: AsyncCallback<void>): void 1671 1672Triggers vibration with the specified effect. This API uses an asynchronous callback to return the result. 1673 1674This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9)<sup>9+</sup> instead. 1675 1676**Required permissions**: ohos.permission.VIBRATE 1677 1678**System capability**: SystemCapability.Sensors.MiscDevice 1679 1680**Parameters** 1681 1682| Name | Type | Mandatory| Description | 1683| -------- | ------------------------- | ---- | ---------------------------------------------------------- | 1684| effectId | [EffectId](#effectid) | Yes | Preset vibration effect ID. | 1685| callback | AsyncCallback<void> | No | Callback used to return the result. If the vibration starts, **err** is **undefined**; otherwise, **err** is an error object.| 1686 1687**Example** 1688 1689 ```ts 1690 import { vibrator } from '@kit.SensorServiceKit'; 1691 import { BusinessError } from '@kit.BasicServicesKit'; 1692 1693 vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => { 1694 if (error) { 1695 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 1696 } else { 1697 console.info('Succeed in vibrating'); 1698 } 1699 }) 1700 ``` 1701 1702## vibrator.stop<sup>(deprecated)</sup> 1703 1704stop(stopMode: VibratorStopMode): Promise<void> 1705 1706Stops vibration in the specified mode. This API uses a promise to return the result. 1707 1708This API is deprecated since API version 9. You are advised to use [vibrator.stopVibration](#vibratorstopvibration9-1)<sup>9+</sup> instead. 1709 1710**Required permissions**: ohos.permission.VIBRATE 1711 1712**System capability**: SystemCapability.Sensors.MiscDevice 1713 1714**Parameters** 1715 1716| Name | Type | Mandatory| Description | 1717| -------- | ------------------------------------- | ---- | ------------------------ | 1718| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes | Mode to stop the vibration.| 1719 1720**Return value** 1721 1722| Type | Description | 1723| ------------------- | ------------- | 1724| Promise<void> | Promise that returns no value.| 1725 1726**Example** 1727 1728 ```ts 1729 import { vibrator } from '@kit.SensorServiceKit'; 1730 import { BusinessError } from '@kit.BasicServicesKit'; 1731 1732 // Start vibration based on the specified effect ID. 1733 vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => { 1734 if (error) { 1735 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 1736 } else { 1737 console.info('Succeed in vibrating'); 1738 } 1739 }) 1740 // Stop vibration in VIBRATOR_STOP_MODE_PRESET mode. 1741 vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => { 1742 console.info('Succeed in stopping'); 1743 }, (error: BusinessError) => { 1744 console.error(`Failed to stop. Code: ${error.code}, message: ${error.message}`); 1745 }); 1746 ``` 1747 1748 1749## vibrator.stop<sup>(deprecated)</sup> 1750 1751stop(stopMode: VibratorStopMode, callback?: AsyncCallback<void>): void 1752 1753Stops vibration in the specified mode. This API uses an asynchronous callback to return the result. 1754 1755This API is deprecated since API version 9. You are advised to use [vibrator.stopVibration](#vibratorstopvibration9)<sup>9+</sup> instead. 1756 1757**Required permissions**: ohos.permission.VIBRATE 1758 1759**System capability**: SystemCapability.Sensors.MiscDevice 1760 1761**Parameters** 1762 1763| Name | Type | Mandatory| Description | 1764| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 1765| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes | Mode to stop the vibration. | 1766| callback | AsyncCallback<void> | No | Callback used to return the result. If the vibration stops, **err** is **undefined**; otherwise, **err** is an error object.| 1767 1768**Example** 1769 1770 ```ts 1771 import { vibrator } from '@kit.SensorServiceKit'; 1772 import { BusinessError } from '@kit.BasicServicesKit'; 1773 1774 // Start vibration based on the specified effect ID. 1775 vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => { 1776 if (error) { 1777 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 1778 } else { 1779 console.info('Succeed in vibrating'); 1780 } 1781 }) 1782 // Stop vibration in VIBRATOR_STOP_MODE_PRESET mode. 1783 vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, (error: BusinessError) => { 1784 if (error) { 1785 console.error(`Failed to stop. Code: ${error.code}, message: ${error.message}`); 1786 } else { 1787 console.info('Succeed in stopping'); 1788 } 1789 }) 1790 ``` 1791