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