1# @ohos.vibrator (振动) 2<!--Kit: Sensor Service Kit--> 3<!--Subsystem: Sensors--> 4<!--Owner: @dilligencer--> 5<!--Designer: @butterls--> 6<!--Tester: @murphy84--> 7<!--Adviser: @hu-zhiqiong--> 8 9vibrator模块提供控制设备马达振动的能力。包括启动指定时长、预置效果、自定义文件等模式的振动;停止指定时长、预置效果或所有模式的振动。 10 11> **说明:** 12> 13> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14 15 16## 导入模块 17 18```ts 19import { vibrator } from '@kit.SensorServiceKit'; 20``` 21 22## vibrator.startVibration<sup>9+</sup> 23 24startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: AsyncCallback<void>): void 25 26根据指定的振动效果和振动属性触发马达振动。使用callback异步回调。 27 28**需要权限**:ohos.permission.VIBRATE 29 30**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 31 32**系统能力**:SystemCapability.Sensors.MiscDevice 33 34**参数**: 35 36| 参数名 | 类型 | 必填 | 说明 | 37| --------- | -------------------------------------- | ---- | :----------------------------------------------------------- | 38| effect | [VibrateEffect](#vibrateeffect9) | 是 | 马达振动效果,支持四种:<br>1、[VibratePreset](#vibratepreset9):按照预置振动效果触发马达振动,适用于交互反馈类的短振场景(如点击长按,滑动,拖拽等),为确保与系统整体振感反馈体验风格一致,推荐使用此接口;<br>2、[VibrateFromFile](#vibratefromfile10):按照文件形式定制自定义振动效果触发马达振动,适用于匹配复杂场景效果的交互反馈(如表情包触发的拟真效果、游戏场景/操作反馈);<br>3、[VibrateTime](#vibratetime9):按照指定时长触发马达振动,仅对振动时长进行启动或停止控制,满足基础功能,无法对振动强度、频率等维度进行个性化设置,此种振动调节不够细腻,无法满足精致体验;<br/>4、[VibrateFromPattern<sup>18+</sup>](#vibratefrompattern18):按照自定义振动效果触发马达振动。使用场景和VibrateFromFile一致。VibrateFromFile是面向文件中提前定制好的效果,将具体的振动事件以文件描述符形式传递到接口中;VibrateFromPattern提供更加灵活的振动事件排列组合,将振动事件以振动事件数组的形式传递到接口中。<br/> | 39| attribute | [VibrateAttribute](#vibrateattribute9) | 是 | 马达振动属性。 | 40| callback | AsyncCallback<void> | 是 | 回调函数。当马达振动成功,err为undefined;否则为错误对象,包含错误码和错误信息。 | 41 42**错误码**: 43 44以下错误码的详细介绍请参见[振动错误码](errorcode-vibrator.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 45 46| 错误码ID | 错误信息 | 47| -------- | ------------------------------------------------------------ | 48| 201 | Permission denied. | 49| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 50| 801 | Capability not supported. | 51| 14600101 | Device operation failed. | 52 53**示例**: 54 551.按照预置振动效果触发马达振动: 56 57 ```ts 58 import { vibrator } from '@kit.SensorServiceKit'; 59 import { BusinessError } from '@kit.BasicServicesKit'; 60 61 // 使用try catch对可能出现的异常进行捕获 62 try { 63 // 查询是否支持'haptic.notice.success' 64 vibrator.isSupportEffect('haptic.notice.success', (err: BusinessError, state: boolean) => { 65 if (err) { 66 console.error(`Failed to query effect. Code: ${err.code}, message: ${err.message}`); 67 return; 68 } 69 console.info('Succeed in querying effect'); 70 if (state) { 71 try { 72 vibrator.startVibration({ 73 type: 'preset', 74 effectId: 'haptic.notice.success', 75 count: 1, 76 }, { 77 usage: 'notification' // 根据实际选择类型归属不同的开关管控 78 }, (error: BusinessError) => { 79 if (error) { 80 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 81 return; 82 } 83 console.info('Succeed in starting vibration'); 84 85 }); 86 } catch (err) { 87 let e: BusinessError = err as BusinessError; 88 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 89 } 90 } 91 }) 92 } catch (error) { 93 let e: BusinessError = error as BusinessError; 94 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 95 } 96 ``` 97 982.按照自定义振动配置文件触发马达振动: 99 100 ```ts 101 import { vibrator } from '@kit.SensorServiceKit'; 102 import { resourceManager } from '@kit.LocalizationKit'; 103 import { BusinessError } from '@kit.BasicServicesKit'; 104 105 const fileName: string = 'xxx.json'; 106 107 @Entry 108 @Component 109 struct Index { 110 uiContext = this.getUIContext(); 111 112 build() { 113 Row() { 114 Column() { 115 Button('alarm-file') 116 .onClick(() => { 117 let rawFd: resourceManager.RawFileDescriptor | undefined = this.uiContext.getHostContext()?.resourceManager.getRawFdSync(fileName); 118 if (rawFd != undefined) { 119 try { 120 vibrator.startVibration({ 121 type: "file", 122 hapticFd: { fd: rawFd.fd, offset: rawFd.offset, length: rawFd.length } 123 }, { 124 id: 0, 125 usage: 'alarm' // 根据实际选择类型归属不同的开关管控 126 }, (error: BusinessError) => { 127 if (error) { 128 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 129 return; 130 } 131 console.info('Succeed in starting vibration'); 132 }); 133 } catch (err) { 134 let e: BusinessError = err as BusinessError; 135 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 136 } finally { 137 vibrator.stopVibration(); 138 this.uiContext.getHostContext()?.resourceManager.closeRawFdSync(fileName); 139 } 140 } 141 }) 142 } 143 .width('100%') 144 } 145 .height('100%') 146 } 147 } 148 ``` 149 1503.按照指定时长触发马达振动: 151 152 ```ts 153 import { vibrator } from '@kit.SensorServiceKit'; 154 import { BusinessError } from '@kit.BasicServicesKit'; 155 156 try { 157 vibrator.startVibration({ 158 type: 'time', 159 duration: 1000, 160 }, { 161 id: 0, 162 usage: 'alarm' // 根据实际选择类型归属不同的开关管控 163 }, (error: BusinessError) => { 164 if (error) { 165 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 166 return; 167 } 168 console.info('Succeed in starting vibration'); 169 }); 170 } catch (err) { 171 let e: BusinessError = err as BusinessError; 172 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 173 } 174 ``` 175 176## vibrator.startVibration<sup>9+</sup> 177 178startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise<void> 179 180根据指定的振动效果和振动属性触发马达振动。使用promise异步回调。 181 182**需要权限**:ohos.permission.VIBRATE 183 184**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 185 186**系统能力**:SystemCapability.Sensors.MiscDevice 187 188**参数**: 189 190| 参数名 | 类型 | 必填 | 说明 | 191| --------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 192| effect | [VibrateEffect](#vibrateeffect9) | 是 | 马达振动效果,支持四种:<br/>1、[VibrateTime](#vibratetime9):按照预置振动效果触发马达振动,适用于交互反馈类的短振场景(如点击长按,滑动,拖拽等),为确保与系统整体振感反馈体验风格一致,推荐使用此接口;<br/>2、[VibratePreset](#vibratepreset9):按照文件形式定制自定义振动效果触发马达振动,适用于匹配复杂场景效果的交互反馈(如表情包触发的拟真效果、游戏场景/操作反馈);<br/>3、[VibrateFromFile](#vibratefromfile10):按照指定时长触发马达振动,仅对振动时长进行启动或停止控制,满足基础功能,无法对振动强度、频率等维度进行个性化设置,此种振动调节不够细腻,无法满足精致体验;<br/>4、[VibrateFromPattern<sup>18+</sup>](#vibratefrompattern18):按照自定义振动效果触发马达振动。使用场景和VibrateFromFile一致。VibrateFromFile是面向文件中提前定制好的效果,将具体的振动事件以文件描述符形式传递到接口中;VibrateFromPattern提供更加灵活的振动事件排列组合,将振动事件以振动事件数组的形式传递到接口中。 | 193| attribute | [VibrateAttribute](#vibrateattribute9) | 是 | 马达振动属性。 | 194 195**返回值**: 196 197| 类型 | 说明 | 198| ------------------- | ------------------------- | 199| Promise<void> | 无返回结果的Promise对象。 | 200 201**错误码**: 202 203以下错误码的详细介绍请参见[振动错误码](errorcode-vibrator.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 204 205| 错误码ID | 错误信息 | 206| -------- | ------------------------------------------------------------ | 207| 201 | Permission denied. | 208| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 209| 801 | Capability not supported. | 210| 14600101 | Device operation failed. | 211 212**示例**: 213 2141.按照预置振动效果触发马达振动: 215 216 ```ts 217 import { vibrator } from '@kit.SensorServiceKit'; 218 import { BusinessError } from '@kit.BasicServicesKit'; 219 220 // 使用try catch对可能出现的异常进行捕获 221 try { 222 // 查询是否支持'haptic.notice.success' 223 vibrator.isSupportEffect('haptic.notice.success', (err: BusinessError, state: boolean) => { 224 if (err) { 225 console.error(`Failed to query effect. Code: ${err.code}, message: ${err.message}`); 226 return; 227 } 228 console.info('Succeed in querying effect'); 229 if (state) { 230 try { 231 vibrator.startVibration({ 232 type: 'preset', 233 effectId: 'haptic.notice.success', 234 count: 1, 235 }, { 236 usage: 'notification' // 根据实际选择类型归属不同的开关管控 237 }, (error: BusinessError) => { 238 if (error) { 239 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 240 return; 241 } 242 console.info('Succeed in starting vibration'); 243 244 }); 245 } catch (err) { 246 let e: BusinessError = err as BusinessError; 247 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 248 } 249 } 250 }) 251 } catch (error) { 252 let e: BusinessError = error as BusinessError; 253 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 254 } 255 ``` 256 2572.按照自定义振动配置文件触发马达振动: 258 259 ```ts 260 import { vibrator } from '@kit.SensorServiceKit'; 261 import { resourceManager } from '@kit.LocalizationKit'; 262 import { BusinessError } from '@kit.BasicServicesKit'; 263 264 const fileName: string = 'xxx.json'; 265 266 @Entry 267 @Component 268 struct Index { 269 uiContext = this.getUIContext(); 270 271 build() { 272 Row() { 273 Column() { 274 Button('alarm-file') 275 .onClick(() => { 276 let rawFd: resourceManager.RawFileDescriptor | undefined = this.uiContext.getHostContext()?.resourceManager.getRawFdSync(fileName); 277 if (rawFd != undefined) { 278 try { 279 vibrator.startVibration({ 280 type: "file", 281 hapticFd: { fd: rawFd.fd, offset: rawFd.offset, length: rawFd.length } 282 }, { 283 id: 0, 284 usage: 'alarm' // 根据实际选择类型归属不同的开关管控 285 }, (error: BusinessError) => { 286 if (error) { 287 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 288 return; 289 } 290 console.info('Succeed in starting vibration'); 291 }); 292 } catch (err) { 293 let e: BusinessError = err as BusinessError; 294 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 295 } finally { 296 vibrator.stopVibration(); 297 this.uiContext.getHostContext()?.resourceManager.closeRawFdSync(fileName); 298 } 299 } 300 }) 301 } 302 .width('100%') 303 } 304 .height('100%') 305 } 306 } 307 ``` 308 3093.按照指定时长触发马达振动: 310 311 ```ts 312 import { vibrator } from '@kit.SensorServiceKit'; 313 import { BusinessError } from '@kit.BasicServicesKit'; 314 315 try { 316 vibrator.startVibration({ 317 type: 'time', 318 duration: 1000 319 }, { 320 id: 0, 321 usage: 'alarm' // 根据实际选择类型归属不同的开关管控 322 }).then(() => { 323 console.info('Succeed in starting vibration'); 324 }, (error: BusinessError) => { 325 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 326 }); 327 } catch (err) { 328 let e: BusinessError = err as BusinessError; 329 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 330 } 331 ``` 332 333## vibrator.stopVibration<sup>9+</sup> 334 335stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback<void>): void 336 337按照指定模式停止马达振动。使用callback异步回调。 338 339**需要权限**:ohos.permission.VIBRATE 340 341**系统能力**:SystemCapability.Sensors.MiscDevice 342 343**参数**: 344 345| 参数名 | 类型 | 必填 | 说明 | 346| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 347| stopMode | [VibratorStopMode](#vibratorstopmode) | 是 | 指定的停止振动模式,支持两种:<br>VIBRATOR_STOP_MODE_TIME:停止固定时长振动;<br>VIBRATOR_STOP_MODE_PRESET:停止预置振动。<br>此接口无法停止自定义振动,请使用[vibrator.stopVibration<sup>10+</sup>](#vibratorstopvibration10)。 | 348| callback | AsyncCallback<void> | 是 | 回调函数,当马达停止振动成功,err为undefined,否则为错误对象。 | 349 350**错误码**: 351 352以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 353 354| 错误码ID | 错误信息 | 355| -------- | ------------------------------------------------------------ | 356| 201 | Permission denied. | 357| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 358 359**示例**: 360 3611.停止指定时长振动: 362 363 ```ts 364 import { vibrator } from '@kit.SensorServiceKit'; 365 import { BusinessError } from '@kit.BasicServicesKit'; 366 367 // 使用try catch对可能出现的异常进行捕获 368 try { 369 // 按照指定时长振动 370 vibrator.startVibration({ 371 type: 'time', 372 duration: 1000, 373 }, { 374 id: 0, 375 usage: 'alarm' // 根据实际选择类型归属不同的开关管控 376 }, (error: BusinessError) => { 377 if (error) { 378 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 379 return; 380 } 381 console.info('Succeed in starting vibration'); 382 }); 383 } catch (err) { 384 let e: BusinessError = err as BusinessError; 385 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 386 } 387 388 try { 389 // 按照VIBRATOR_STOP_MODE_TIME模式停止振动 390 vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, (error: BusinessError) => { 391 if (error) { 392 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 393 return; 394 } 395 console.info('Succeed in stopping vibration'); 396 }) 397 } catch (err) { 398 let e: BusinessError = err as BusinessError; 399 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 400 } 401 ``` 402 4032.停止预置振动: 404 405 ```ts 406 import { vibrator } from '@kit.SensorServiceKit'; 407 import { BusinessError } from '@kit.BasicServicesKit'; 408 409 try { 410 // 按照预置效果振动 411 vibrator.startVibration({ 412 type: 'preset', 413 effectId: 'haptic.notice.success', 414 count: 1, 415 }, { 416 id: 0, 417 usage: 'notification' // 根据实际选择类型归属不同的开关管控 418 }, (error: BusinessError) => { 419 if (error) { 420 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 421 return; 422 } 423 console.info('Succeed in starting vibration'); 424 }); 425 } catch (err) { 426 let e: BusinessError = err as BusinessError; 427 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 428 } 429 430 try { 431 // 按照VIBRATOR_STOP_MODE_PRESET模式停止振动 432 vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, (error: BusinessError) => { 433 if (error) { 434 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 435 return; 436 } 437 console.info('Succeed in stopping vibration'); 438 }) 439 } catch (err) { 440 let e: BusinessError = err as BusinessError; 441 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 442 } 443 ``` 444 445## vibrator.stopVibration<sup>9+</sup> 446 447stopVibration(stopMode: VibratorStopMode): Promise<void> 448 449按照指定模式停止马达的振动。使用promise异步回调。 450 451**需要权限**:ohos.permission.VIBRATE 452 453**系统能力**:SystemCapability.Sensors.MiscDevice 454 455**参数**: 456 457| 参数名 | 类型 | 必填 | 说明 | 458| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 459| stopMode | [VibratorStopMode](#vibratorstopmode) | 是 | 支持停止两种指定的振动模式:<br>VIBRATOR_STOP_MODE_TIME:停止指定时长振动;<br>VIBRATOR_STOP_MODE_PRESET:停止预置振动。<br>此接口无法停止自定义振动,请使用[vibrator.stopVibration<sup>10+</sup>](#vibratorstopvibration10-1)。 | 460 461**返回值**: 462 463| 类型 | 说明 | 464| ------------------- | ------------- | 465| Promise<void> | Promise对象。 | 466 467**错误码**: 468 469以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 470 471| 错误码ID | 错误信息 | 472| -------- | ------------------------------------------------------------ | 473| 201 | Permission denied. | 474| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 475 476**示例**: 477 4781.停止指定时长振动: 479 480 ```ts 481 import { vibrator } from '@kit.SensorServiceKit'; 482 import { BusinessError } from '@kit.BasicServicesKit'; 483 484 // 使用try catch对可能出现的异常进行捕获 485 try { 486 // 按照指定时长振动 487 vibrator.startVibration({ 488 type: 'time', 489 duration: 1000, 490 }, { 491 id: 0, 492 usage: 'alarm' // 根据实际选择类型归属不同的开关管控 493 }).then(() => { 494 console.info('Succeed in starting vibration'); 495 }, (error: BusinessError) => { 496 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 497 }); 498 } catch (err) { 499 let e: BusinessError = err as BusinessError; 500 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 501 } 502 503 try { 504 // 按照VIBRATOR_STOP_MODE_TIME模式停止振动 505 vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME).then(() => { 506 console.info('Succeed in stopping vibration'); 507 }, (error: BusinessError) => { 508 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 509 }); 510 } catch (err) { 511 let e: BusinessError = err as BusinessError; 512 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 513 } 514 ``` 515 5162.停止预置振动: 517 518 ```ts 519 import { vibrator } from '@kit.SensorServiceKit'; 520 import { BusinessError } from '@kit.BasicServicesKit'; 521 522 try { 523 // 按照预置效果振动 524 vibrator.startVibration({ 525 type: 'preset', 526 effectId: 'haptic.notice.success', 527 count: 1, 528 }, { 529 id: 0, 530 usage: 'notification' // 根据实际选择类型归属不同的开关管控 531 }).then(() => { 532 console.info('Succeed in starting vibration'); 533 }, (error: BusinessError) => { 534 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 535 }); 536 } catch (err) { 537 let e: BusinessError = err as BusinessError; 538 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 539 } 540 541 try { 542 // 按照VIBRATOR_STOP_MODE_PRESET模式停止振动 543 vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => { 544 console.info('Succeed in stopping vibration'); 545 }, (error: BusinessError) => { 546 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 547 }); 548 } catch (err) { 549 let e: BusinessError = err as BusinessError; 550 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 551 } 552 ``` 553 554## vibrator.stopVibration<sup>10+</sup> 555 556stopVibration(callback: AsyncCallback<void>): void 557 558停止所有模式的马达振动。使用callback异步回调。 559 560**需要权限**:ohos.permission.VIBRATE 561 562**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 563 564**系统能力**:SystemCapability.Sensors.MiscDevice 565 566**参数**: 567 568| 参数名 | 类型 | 必填 | 说明 | 569| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 570| callback | AsyncCallback<void> | 是 | 回调函数,当马达停止振动成功,err为undefined,否则为错误对象。 | 571 572**错误码**: 573 574以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 575 576| 错误码ID | 错误信息 | 577| -------- | ------------------ | 578| 201 | Permission denied. | 579 580**示例**: 581 582 ```ts 583 import { vibrator } from '@kit.SensorServiceKit'; 584 import { BusinessError } from '@kit.BasicServicesKit'; 585 586 // 使用try catch对可能出现的异常进行捕获 587 try { 588 // 停止所有模式的马达振动 589 vibrator.stopVibration((error: BusinessError) => { 590 if (error) { 591 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 592 return; 593 } 594 console.info('Succeed in stopping vibration'); 595 }) 596 } catch (error) { 597 let e: BusinessError = error as BusinessError; 598 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 599 } 600 ``` 601 602## vibrator.stopVibration<sup>10+</sup> 603 604stopVibration(): Promise<void> 605 606停止所有模式的马达振动。使用promise异步回调。 607 608**需要权限**:ohos.permission.VIBRATE 609 610**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 611 612**系统能力**:SystemCapability.Sensors.MiscDevice 613 614**返回值**: 615 616| 类型 | 说明 | 617| ------------------- | ------------- | 618| Promise<void> | Promise对象。 | 619 620**错误码**: 621 622以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 623 624| 错误码ID | 错误信息 | 625| -------- | ------------------ | 626| 201 | Permission denied. | 627 628**示例**: 629 630 ```ts 631 import { vibrator } from '@kit.SensorServiceKit'; 632 import { BusinessError } from '@kit.BasicServicesKit'; 633 634 // 使用try catch对可能出现的异常进行捕获 635 try { 636 // 停止所有模式的马达振动 637 vibrator.stopVibration().then(() => { 638 console.info('Succeed in stopping vibration'); 639 }, (error: BusinessError) => { 640 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 641 }); 642 } catch (error) { 643 let e: BusinessError = error as BusinessError; 644 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 645 } 646 ``` 647 648## vibrator.stopVibration<sup>19+</sup> 649 650stopVibration(param?: VibratorInfoParam): Promise<void> 651 652不传参默认停止本地设备所有马达的振动,也可传递参数停止指定设备马达振动。使用promise异步回调。 653 654**需要权限**:ohos.permission.VIBRATE 655 656**系统能力**:SystemCapability.Sensors.MiscDevice 657 658**参数**: 659 660| 参数名 | 类型 | 必填 | 说明 | 661| -------- | ------------------------------------------------------------ | ---- |-----------------------------------| 662| param | [VibratorInfoParam](#vibratorinfoparam19) | 否 | 指出需要控制的设备和马达信息,不传参默认控制的为本地设备的全部马达 | 663 664**返回值**: 665 666| 类型 | 说明 | 667| ------------------- | ------------- | 668| Promise<void> | Promise对象。 | 669 670**错误码**: 671 672以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 673 674| 错误码ID | 错误信息 | 675| -------- | ------------------ | 676| 201 | Permission denied. | 677| 14600101 | Device operation failed. | 678 679**示例**: 680 681 ```ts 682 import { vibrator } from '@kit.SensorServiceKit'; 683 import { BusinessError } from '@kit.BasicServicesKit'; 684 685 function vibratorDemo() { 686 // 查询所有马达设备信息。 687 const vibratorInfoList: vibrator.VibratorInfo[] = vibrator.getVibratorInfoSync(); 688 // 根据实际业务逻辑获取目标马达, 例如查找本地马达,此处示例仅做展示,开发者需要自行调整筛选逻辑。 689 const targetVibrator = vibratorInfoList.find((vibrator: vibrator.VibratorInfo) => { 690 return vibrator.isLocalVibrator; 691 }); 692 if (!targetVibrator) { 693 return; 694 } 695 // 调用 vibrator.startVibration 开始振动。 696 // ... 697 698 // 使用try catch对可能出现的异常进行捕获。 699 try { 700 // 根据实际业务场景停止马达振动。 701 vibrator.stopVibration({ deviceId: targetVibrator.deviceId, vibratorId: targetVibrator.vibratorId }).then(() => { 702 console.info('Succeed in stopping vibration'); 703 }, (error: BusinessError) => { 704 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 705 }); 706 } catch (error) { 707 let e: BusinessError = error as BusinessError; 708 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 709 } 710 } 711 ``` 712 713## vibrator.stopVibrationSync<sup>12+</sup> 714 715stopVibrationSync(): void 716 717停止任何形式的马达振动。 718 719**需要权限**:ohos.permission.VIBRATE 720 721**原子化服务API**:从API Version 12开始,该接口支持在原子化服务中使用。 722 723**系统能力**:SystemCapability.Sensors.MiscDevice 724 725**错误码**: 726 727以下错误码的详细介绍请参见[振动错误码](errorcode-vibrator.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 728 729| 错误码ID | 错误信息 | 730| -------- | ------------------------ | 731| 201 | Permission denied. | 732| 14600101 | Device operation failed. | 733 734**示例**: 735 736 ```ts 737 import { vibrator } from '@kit.SensorServiceKit'; 738 import { BusinessError } from '@kit.BasicServicesKit'; 739 740 // 使用try catch对可能出现的异常进行捕获 741 try { 742 // 停止任何形式的马达振动 743 vibrator.stopVibrationSync() 744 console.info('Succeed in stopping vibration'); 745 } catch (error) { 746 let e: BusinessError = error as BusinessError; 747 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 748 } 749 ``` 750 751## vibrator.isSupportEffect<sup>10+</sup> 752 753isSupportEffect(effectId: string, callback: AsyncCallback<boolean>): void 754 755查询是否支持传入参数effectId。使用callback异步回调。 756 757**系统能力**:SystemCapability.Sensors.MiscDevice 758 759**参数**: 760 761| 参数名 | 类型 | 必填 | 说明 | 762| -------- | ---------------------------- | ---- | ----------------------------------------------------------- | 763| effectId | string | 是 | 待确认的预置振动效果,字符串最大长度64,超出截取64。| 764| callback | AsyncCallback<boolean> | 是 | 回调函数,当返回true则表示支持该effectId,返回false不支持。 | 765 766**错误码**: 767 768以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 769 770| 错误码ID | 错误信息 | 771| -------- | ------------------------------------------------------------ | 772| 201 | Permission denied. | 773| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 774 775**示例**: 776 777 ```ts 778 import { vibrator } from '@kit.SensorServiceKit'; 779 import { BusinessError } from '@kit.BasicServicesKit'; 780 781 // 使用try catch对可能出现的异常进行捕获 782 try { 783 // 查询是否支持'haptic.notice.success' 784 vibrator.isSupportEffect('haptic.notice.success', (err: BusinessError, state: boolean) => { 785 if (err) { 786 console.error(`Failed to query effect. Code: ${err.code}, message: ${err.message}`); 787 return; 788 } 789 console.info('Succeed in querying effect'); 790 if (state) { 791 try { 792 // 使用startVibration需要添加ohos.permission.VIBRATE权限 793 vibrator.startVibration({ 794 type: 'preset', 795 effectId: 'haptic.notice.success', 796 count: 1, 797 }, { 798 usage: 'unknown' // 根据实际选择类型归属不同的开关管控 799 }, (error: BusinessError) => { 800 if (error) { 801 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 802 } else { 803 console.info('Succeed in starting vibration'); 804 } 805 }); 806 } catch (error) { 807 let e: BusinessError = error as BusinessError; 808 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 809 } 810 } 811 }) 812 } catch (error) { 813 let e: BusinessError = error as BusinessError; 814 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 815 } 816 ``` 817 818## vibrator.isSupportEffect<sup>10+</sup> 819 820isSupportEffect(effectId: string): Promise<boolean> 821 822查询是否支持传入参数effectId。使用promise异步回调。 823 824**系统能力**:SystemCapability.Sensors.MiscDevice 825 826**参数**: 827 828| 参数名 | 类型 | 必填 | 说明 | 829| -------- | ------ | ---- | ---------------------- | 830| effectId | string | 是 | 待确认的预置振动效果,字符串最大长度64,超出截取64。 | 831 832**返回值**: 833 834| 类型 | 说明 | 835| ---------------------- | ------------------------------------------------------------ | 836| Promise<boolean> | Promise对象。当返回true则表示支持该effectId,返回false不支持。 | 837 838**错误码**: 839 840以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 841 842| 错误码ID | 错误信息 | 843| -------- | ------------------------------------------------------------ | 844| 201 | Permission denied. | 845| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 846 847**示例**: 848 849 ```ts 850 import { vibrator } from '@kit.SensorServiceKit'; 851 import { BusinessError } from '@kit.BasicServicesKit'; 852 853 // 使用try catch对可能出现的异常进行捕获 854 try { 855 // 查询是否支持'haptic.notice.success' 856 vibrator.isSupportEffect('haptic.notice.success').then((state: boolean) => { 857 console.info(`The query result is ${state}`); 858 if (state) { 859 try { 860 vibrator.startVibration({ 861 type: 'preset', 862 effectId: 'haptic.notice.success', 863 count: 1, 864 }, { 865 usage: 'unknown' // 根据实际选择类型归属不同的开关管控 866 }).then(() => { 867 console.info('Succeed in starting vibration'); 868 }).catch((error: BusinessError) => { 869 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 870 }); 871 } catch (error) { 872 let e: BusinessError = error as BusinessError; 873 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 874 } 875 } 876 }, (error: BusinessError) => { 877 console.error(`Failed to query effect. Code: ${error.code}, message: ${error.message}`); 878 }) 879 } catch (error) { 880 let e: BusinessError = error as BusinessError; 881 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 882 } 883 ``` 884 885## vibrator.isSupportEffectSync<sup>12+</sup> 886 887isSupportEffectSync(effectId: string): boolean 888 889查询是否支持预设的振动效果。 890 891**系统能力**:SystemCapability.Sensors.MiscDevice 892 893**参数**: 894 895| 参数名 | 类型 | 必填 | 说明 | 896| -------- | ------ | ---- | ---------------------- | 897| effectId | string | 是 | 待确认的预置振动效果,字符串最大长度64,超出截取64。 | 898 899**返回值**: 900 901| 类型 | 说明 | 902| ------- | ----------------------------------------------------------- | 903| boolean | 返回对象。当返回true则表示支持该effectId,返回false不支持。 | 904 905**错误码**: 906 907以下错误码的详细介绍请参见[振动错误码](errorcode-vibrator.md)和[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 908 909| 错误码ID | 错误信息 | 910| -------- | ------------------------------------------------------------ | 911| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 912| 14600101 | Device operation failed. | 913 914**示例**: 915 916 ```ts 917 import { vibrator } from '@kit.SensorServiceKit'; 918 import { BusinessError } from '@kit.BasicServicesKit'; 919 920 // 使用try catch对可能出现的异常进行捕获 921 try { 922 // 查询是否支持预设'haptic.notice.success' 923 let ret = vibrator.isSupportEffectSync('haptic.notice.success'); 924 console.info(`The query result is ${ret}`); 925 } catch (error) { 926 let e: BusinessError = error as BusinessError; 927 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 928 } 929 ``` 930 931## vibrator.getEffectInfoSync<sup>19+</sup> 932 933getEffectInfoSync(effectId: string, param?: VibratorInfoParam): EffectInfo 934 935通过设备ID和可控马达ID获取预置振动效果信息,用于判断该预置振动效果是否受支持。 936 937**系统能力**:SystemCapability.Sensors.MiscDevice 938 939**参数**: 940 941| 参数名 | 类型 | 必填 | 说明 | 942| -------- | ------------------------------------------------------------ | ---- |-----------------------------| 943| effectId | string | 是 | 待确认的预置振动效果,字符串最大长度64,超出截取64。 | 944| param | [VibratorInfoParam](#vibratorinfoparam19) | 否 | 指出需要查询的设备和马达信息,默认查询的是本地设备。 | 945 946**错误码**: 947 948以下错误码的详细介绍请参见[振动错误码](errorcode-vibrator.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 949 950| 错误码ID | 错误信息 | 951| -------- | ------------------------ | 952| 14600101 | Device operation failed. | 953 954**返回值**: 955 956| 类型 | 说明 | 957| ------- | --------------------------------------------------------- | 958| [EffectInfo](#effectinfo19) | 该信息包括指示是否支持该效果。 | 959 960 961**示例**: 962 963 ```ts 964 import { vibrator } from '@kit.SensorServiceKit'; 965 import { BusinessError } from '@kit.BasicServicesKit'; 966 967 // 使用try catch对可能出现的异常进行捕获 968 try { 969 const effectInfo: vibrator.EffectInfo = vibrator.getEffectInfoSync('haptic.clock.timer', { deviceId: 1, vibratorId: 3}); 970 console.log(`isEffectSupported: ${effectInfo.isEffectSupported}`); 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.getVibratorInfoSync<sup>19+</sup> 979 980getVibratorInfoSync(param?: VibratorInfoParam): Array<VibratorInfo>; 981 982查询一个或所有设备的马达信息列表。 983 984**系统能力**:SystemCapability.Sensors.MiscDevice 985 986**参数**: 987 988| 参数名 | 类型 | 必填 | 说明 | 989| -------- |-----------------------------------------| ---- |-----------------------------------| 990| param | [VibratorInfoParam](#vibratorinfoparam19) | 否 | 指出需要控制的设备和马达信息,不传参默认查询所有设备所有马达的信息 | 991 992**返回值**: 993 994| 类型 | 说明 | 995|-------------------------------| --------------------------------------------------------- | 996| Array<[VibratorInfo](#vibratorinfo19)> | 马达设备的信息。 | 997 998 999**示例**: 1000 1001 ```ts 1002 import { vibrator } from '@kit.SensorServiceKit'; 1003 import { BusinessError } from '@kit.BasicServicesKit'; 1004 1005 try { 1006 const vibratorInfoList: vibrator.VibratorInfo[] = vibrator.getVibratorInfoSync({ deviceId: 1, vibratorId: 3 }); 1007 console.log(`vibratorInfoList: ${JSON.stringify(vibratorInfoList)}`); 1008 } catch (error) { 1009 let e: BusinessError = error as BusinessError; 1010 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 1011 } 1012 ``` 1013 1014 1015## vibrator.on<sup>19+</sup> 1016 1017on(type: 'vibratorStateChange', callback: Callback<VibratorStatusEvent>): void 1018 1019注册一个回调函数,在马达上线或下线时触发回调。 1020 1021**系统能力**:SystemCapability.Sensors.MiscDevice 1022 1023**参数**: 1024 1025| 参数名 | 类型 | 必填 | 说明 | 1026| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 1027| type | 'vibratorStateChange' | 是 | 监听类型,该值固定为vibratorStateChange。 | 1028| callback | Callback<[VibratorStatusEvent](#vibratorstatusevent19)> | 是 | 回调函数,回调参数数据为VibratorStatusEvent。 | 1029 1030**错误码**: 1031 1032以下错误码的详细介绍请参见[振动错误码](errorcode-vibrator.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1033 1034| 错误码ID | 错误信息 | 1035| -------- | ------------------------ | 1036| 14600101 | Device operation failed. | 1037 1038 1039**示例**: 1040 1041 ```ts 1042 import { vibrator } from '@kit.SensorServiceKit'; 1043 import { BusinessError } from '@kit.BasicServicesKit'; 1044 1045 // 回调函数 1046 const vibratorStateChangeCallback = (data: vibrator.VibratorStatusEvent) => { 1047 console.log('vibrator state callback info:', JSON.stringify(data)); 1048 } 1049 1050 // 使用try catch对可能出现的异常进行捕获 1051 try { 1052 // 订阅 vibratorStateChange事件 1053 vibrator.on('vibratorStateChange', vibratorStateChangeCallback); 1054 } catch (error) { 1055 let e: BusinessError = error as BusinessError; 1056 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 1057 } 1058 ``` 1059 1060 1061## vibrator.off<sup>19+</sup> 1062 1063off(type: 'vibratorStateChange', callback?: Callback<VibratorStatusEvent>): void 1064 1065注销马达上线或下线事件的回调函数。 1066 1067**系统能力**:SystemCapability.Sensors.MiscDevice 1068 1069**参数**: 1070 1071| 参数名 | 类型 | 必填 | 说明 | 1072| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 1073| type | 'vibratorStateChange' | 是 | 监听类型,该值固定为vibratorStateChange。 | 1074| callback | Callback<[VibratorStatusEvent](#vibratorstatusevent19)> | 否 | 回调函数,回调参数数据为VibratorStatusEvent,不填此参数则为注销所有callback | 1075 1076**错误码**: 1077 1078以下错误码的详细介绍请参见[振动错误码](errorcode-vibrator.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1079 1080| 错误码ID | 错误信息 | 1081| -------- | ------------------------ | 1082| 14600101 | Device operation failed. | 1083 1084 1085**示例**: 1086 1087 ```ts 1088 import { vibrator } from '@kit.SensorServiceKit'; 1089 import { BusinessError } from '@kit.BasicServicesKit'; 1090 1091 // 回调函数 1092 const vibratorStateChangeCallback = (data: vibrator.VibratorStatusEvent) => { 1093 console.log('vibrator state callback info:', JSON.stringify(data)); 1094 } 1095 // 使用try catch对可能出现的异常进行捕获 1096 try { 1097 // 取消订阅 vibratorStateChange事件 1098 vibrator.off('vibratorStateChange', vibratorStateChangeCallback); 1099 // 取消订阅所有 vibratorStateChange事件 1100 // vibrator.off('vibratorStateChange'); 1101 } catch (error) { 1102 let e: BusinessError = error as BusinessError; 1103 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 1104 } 1105 ``` 1106 1107 1108## VibratorStatusEvent<sup>19+</sup> 1109 1110振动设备上线、下线状态事件信息。 1111 1112**系统能力**:SystemCapability.Sensors.MiscDevice 1113 1114 1115| 名称 | 类型 | 只读 | 可选 | 说明 | 1116|------------------|---------|----|----|----------------------------------| 1117| timestamp | number | 是 | 否 | 报告事件的时间戳。 | 1118| deviceId | number | 是 | 否 | 设备的ID。 | 1119| vibratorCount | number | 是 | 否 | 设备上的马达的数量。 | 1120| isVibratorOnline | boolean | 是 | 否 | 指示设备的上线和下线状态,true表示上线,false表示下线。 | 1121 1122 1123## VibratorInfoParam<sup>19+</sup> 1124 1125设备上马达的参数。默认情况下,VibratorInfoParam默认为查询或控制本地全部马达。 1126 1127**系统能力**:SystemCapability.Sensors.MiscDevice 1128 1129 1130| 名称 | 类型 | 只读 | 可选 | 说明 | 1131| ---- | ------ | ---- | ---- |------------------------------------------------------------| 1132| deviceId | number | 否 | 是 | 设备的ID:默认值为-1,控制的为本地设备,其它设备Id需使用[getEffectInfoSync](#vibratorgeteffectinfosync19)查询。 | 1133| vibratorId | number | 否 | 是 | 马达ID:默认值为-1,控制的是该设备的全部马达,其它马达Id需使用[getEffectInfoSync](#vibratorgeteffectinfosync19)查询。 | 1134 1135 1136 1137## EffectInfo<sup>19+</sup> 1138 1139查询的预制效果信息。 1140 1141**系统能力**:SystemCapability.Sensors.MiscDevice 1142 1143 1144| 名称 | 类型 | 只读 | 可选 | 说明 | 1145|-------------------|---------|----|----|-------------------------------| 1146| isEffectSupported | boolean | 是 | 否 | 预制效果是否支持,true表示支持,false表示不支持。 | 1147 1148 1149## VibratorInfo<sup>19+</sup> 1150 1151表示查询的马达信息。 1152 1153**系统能力**:SystemCapability.Sensors.MiscDevice 1154 1155| 名称 | 类型 | 只读 | 可选 | 说明 | 1156|---------------------|---------|----|----|-----------| 1157| deviceId | number | 是 | 否 | 设备ID。 | 1158| vibratorId | number | 是 | 否 | 马达ID。 | 1159| deviceName | string | 是 | 否 | 设备名称。 | 1160| isHdHapticSupported | boolean | 是 | 否 | 是否支持高清振动。 | 1161| isLocalVibrator | boolean | 是 | 否 | 是否为本地设备。 | 1162 1163 1164## vibrator.isHdHapticSupported<sup>12+</sup> 1165 1166isHdHapticSupported(): boolean 1167 1168查询是否支持高清振动。 1169 1170**系统能力**:SystemCapability.Sensors.MiscDevice 1171 1172**返回值**: 1173 1174| 类型 | 说明 | 1175| ------- | --------------------------------------------------------- | 1176| boolean | 返回对象,当返回true表示支持高清振动,返回false不支持。。 | 1177 1178**错误码**: 1179 1180以下错误码的详细介绍请参见[振动错误码](errorcode-vibrator.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1181 1182| 错误码ID | 错误信息 | 1183| -------- | ------------------------ | 1184| 14600101 | Device operation failed. | 1185 1186**示例**: 1187 1188 ```ts 1189 import { vibrator } from '@kit.SensorServiceKit'; 1190 import { BusinessError } from '@kit.BasicServicesKit'; 1191 1192 // 使用try catch对可能出现的异常进行捕获 1193 try { 1194 // 查询是否支持高清振动 1195 let ret = vibrator.isHdHapticSupported(); 1196 console.info(`The query result is ${ret}`); 1197 } catch (error) { 1198 let e: BusinessError = error as BusinessError; 1199 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 1200 } 1201 ``` 1202 1203## VibratorPatternBuilder<sup>18+</sup> 1204 1205### vibrator('addContinuousEvent')<sup>18+</sup> 1206 1207addContinuousEvent(time: number, duration: number, options?: ContinuousParam): VibratorPatternBuilder; 1208 1209添加长振事件的方法成VibratorPattern对象。 1210 1211**系统能力**:SystemCapability.Sensors.MiscDevice 1212 1213**参数**: 1214 1215| 参数名 | 类型 | 必填 | 说明 | 1216| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 1217| time | number | 是 | 长期振动的起始时间。单位ms,取值范围(0,1800000)区间内所有整数。 | 1218| duration | number | 是 | 长期振动的持续时间。单位ms,取值范围(0,5000]区间内所有整数。 | 1219| options | [ContinuousParam](#continuousparam18) | 否 | 可选参数,可选参数对象。 | 1220 1221**返回值**: 1222 1223| 类型 | 说明 | 1224| ---------------------- | ---------------------------------------------------- | 1225| VibratorPatternBuilder | 返回已添加连续振动事件的VibratorPatternBuilder对象。 | 1226 1227**错误码**: 1228 1229以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1230 1231| 错误码ID | 错误信息 | 1232| -------- | ---------------- | 1233| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1234 1235**示例**: 1236 1237 ```ts 1238 import { vibrator } from '@kit.SensorServiceKit'; 1239 import { BusinessError } from '@kit.BasicServicesKit'; 1240 1241 let builder = new vibrator.VibratorPatternBuilder(); 1242 // 使用try catch对可能出现的异常进行捕获 1243 try { 1244 let pointsMe: vibrator.VibratorCurvePoint[] = [ 1245 { time: 0, intensity: 0, frequency: -7 }, 1246 { time: 42, intensity: 1, frequency: -6 }, 1247 { time: 128, intensity: 0.94, frequency: -4 }, 1248 { time: 217, intensity: 0.63, frequency: -14 }, 1249 { time: 763, intensity: 0.48, frequency: -14 }, 1250 { time: 1125, intensity: 0.53, frequency: -10 }, 1251 { time: 1503, intensity: 0.42, frequency: -14 }, 1252 { time: 1858, intensity: 0.39, frequency: -14 }, 1253 { time: 2295, intensity: 0.34, frequency: -17 }, 1254 { time: 2448, intensity: 0.21, frequency: -14 }, 1255 { time: 2468, intensity: 0, frequency: -21 } 1256 ] // VibratorCurvePoint参数最少设置4个,最大设置16个 1257 let param: vibrator.ContinuousParam = { 1258 intensity: 97, 1259 frequency: 34, 1260 points:pointsMe, 1261 index: 0 1262 } 1263 builder.addContinuousEvent(0, 2468, param); 1264 console.info(`addContinuousEvent builder is ${builder.build()}`); 1265 } catch(error) { 1266 let e: BusinessError = error as BusinessError; 1267 console.error(`Exception. Code ${e.code}`); 1268 } 1269 ``` 1270 1271### vibrator('addTransientEvent')<sup>18+</sup> 1272 1273addTransientEvent(time: number, options?: TransientParam): VibratorPatternBuilder; 1274 1275添加短振事件的方法成VibratorPattern对象。 1276 1277**系统能力**:SystemCapability.Sensors.MiscDevice 1278 1279**参数**: 1280 1281| 参数名 | 类型 | 必填 | 说明 | 1282| ------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 1283| time | number | 是 | 长期振动的起始时间。单位ms,取值范围(0,1800000)区间内所有整数。 | 1284| options | [TransientParam](#transientparam18) | 否 | 可选参数,可选参数对象。 | 1285 1286**返回值**: 1287 1288| 类型 | 说明 | 1289| ---------------------- | ------------------------------------------------ | 1290| VibratorPatternBuilder | 返回已添加短振事件的VibratorPatternBuilder对象。 | 1291 1292**错误码**: 1293 1294以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。错误码和错误信息会以异常的形式抛出,调用接口时需要使用try catch对可能出现的异常进行捕获操作。 1295 1296| 错误码ID | 错误信息 | 1297| -------- | ---------------- | 1298| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 1299 1300**示例**: 1301 1302 ```ts 1303 import { vibrator } from '@kit.SensorServiceKit'; 1304 import { BusinessError } from '@kit.BasicServicesKit'; 1305 1306 let builder = new vibrator.VibratorPatternBuilder(); 1307 // 使用try catch对可能出现的异常进行捕获 1308 try { 1309 let param: vibrator.TransientParam = { 1310 intensity: 80, 1311 frequency: 70, 1312 index: 0 1313 } 1314 builder.addTransientEvent(0, param); 1315 console.log(`addTransientEvent builder is ${builder.build()}`); 1316 } catch(error) { 1317 let e: BusinessError = error as BusinessError; 1318 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 1319 } 1320 ``` 1321 1322### vibrator('build')<sup>18+</sup> 1323 1324build(): VibratorPattern; 1325 1326构造组合短事件或长事件的振动序列的方法。 1327 1328**系统能力**:SystemCapability.Sensors.MiscDevice 1329 1330**返回值**: 1331 1332| 类型 | 说明 | 1333| ------------------------------------- | ---------------------------------- | 1334| [VibratorPattern](#vibratorpattern18) | 构造组合短振或长振的振动序列方法。 | 1335 1336**示例**: 1337 1338 ```ts 1339 import { vibrator } from '@kit.SensorServiceKit'; 1340 import { BusinessError } from '@kit.BasicServicesKit'; 1341 1342 let builder = new vibrator.VibratorPatternBuilder(); 1343 try { 1344 let param: vibrator.TransientParam = { 1345 intensity: 80, 1346 frequency: 70, 1347 index: 0 1348 } 1349 builder.addTransientEvent(0, param); 1350 console.log(`addTransientEvent builder is ${builder.build()}`); 1351 } catch(error) { 1352 let e: BusinessError = error as BusinessError; 1353 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 1354 } 1355 try { 1356 vibrator.startVibration({ 1357 type: "pattern", 1358 pattern: builder.build() 1359 }, { 1360 usage: "alarm", // 根据实际选择类型归属不同的开关管控 1361 }, (error) => { 1362 if (error) { 1363 let e: BusinessError = error as BusinessError; 1364 console.error(`Vibrate fail. Code: ${e.code}, message: ${e.message}`); 1365 } else { 1366 console.info(`vibrate success`); 1367 } 1368 }); 1369 } catch(error) { 1370 let e: BusinessError = error as BusinessError; 1371 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 1372 } 1373 ``` 1374 1375## EffectId 1376 1377预置的振动效果。在调用[vibrator.startVibration9+](#vibratorstartvibration9)或[vibrator.stopVibration9+](#vibratorstopvibration9-1)接口下发[VibratePreset](#vibratepreset9)形式振动的时候需要使用此参数类型。此参数值种类多样,'haptic.clock.timer'为其中一种。[HapticFeedback<sup>12+</sup>](#hapticfeedback12)展示了几种常用的EffectId值。 1378 1379> **说明:** 1380> 1381> 由于设备存在多样性,不同的设备可能预置不同的效果,建议使用预置效果前先使用[vibrator.isSupportEffect](#vibratorissupporteffect10-1)<sup>10+</sup>接口查询当前设备是否支持该预置效果。 1382 1383**系统能力**:SystemCapability.Sensors.MiscDevice 1384 1385| 名称 | 值 | 说明 | 1386| ----------- | -------------------- | ---------------------------- | 1387| EFFECT_CLOCK_TIMER | 'haptic.clock.timer' | 描述用户调整计时器时的振动效果。 | 1388 1389## HapticFeedback<sup>12+</sup> 1390 1391简单而通用的振动效果。根据各设备的马达器件不同,同一振动效果的频率会有差异,但效果的频率趋向是统一的。这几种振动效果是EffectId参数的具体值,使用方法参考[vibrator.startVibration9+](#vibratorstartvibration9)或[vibrator.stopVibration9+](#vibratorstopvibration9-1)接口下发[VibratePreset](#vibratepreset9)形式振动的示例代码。 1392 1393**系统能力**:SystemCapability.Sensors.MiscDevice 1394 1395| 名称 | 值 | 说明 | 1396| ----------------------------------- | ----------------------- | ---------------------------- | 1397| EFFECT_SOFT | 'haptic.effect.soft' | 较松散的振动效果,频率偏低。 | 1398| EFFECT_HARD | 'haptic.effect.hard' | 较沉重的振动效果,频率居中。 | 1399| EFFECT_SHARP | 'haptic.effect.sharp' | 较尖锐的振动效果,频率偏高。 | 1400| EFFECT_NOTICE_SUCCESS<sup>18+</sup> | 'haptic.notice.success' | 表达成功通知的振动效果。 | 1401| EFFECT_NOTICE_FAILURE<sup>18+</sup> | 'haptic.notice.fail' | 表达失败通知的振动效果。 | 1402| EFFECT_NOTICE_WARNING<sup>18+</sup> | 'haptic.notice.warning' | 表达警告通知的振动效果。 | 1403 1404## VibratorStopMode 1405 1406停止振动的模式。在调用[vibrator.stopVibration9+](#vibratorstopvibration9)或[vibrator.stopVibration9+](#vibratorstopvibration9-1)接口时,需要使用此参数类型指定停止的振动模式。停止模式和[VibrateEffect9+](#vibrateeffect9)中下发的模式为对应关系。 1407 1408**系统能力**:SystemCapability.Sensors.MiscDevice 1409 1410| 名称 | 值 | 说明 | 1411| ------------------------- | -------- | ------------------------ | 1412| VIBRATOR_STOP_MODE_TIME | 'time' | 停止duration模式的振动。 | 1413| VIBRATOR_STOP_MODE_PRESET | 'preset' | 停止预置EffectId的振动。 | 1414 1415## VibrateEffect<sup>9+</sup> 1416 1417马达振动效果,支持以下四种。在调用[vibrator.startVibration9+](#vibratorstartvibration9)或[vibrator.startVibration9+](#vibratorstartvibration9-1)接口时,此参数的四种类型表示以四种不同的形式触发振动。 1418 1419**系统能力**:SystemCapability.Sensors.MiscDevice 1420 1421| 类型 | 说明 | 1422| ------------------------------------- | ------------------------------------------------------------ | 1423| [VibrateTime](#vibratetime9) | 按照指定时长触发马达振动。<br/>**原子化服务API:** 从API Version 11开始,该接口支持在原子化服务中使用。 | 1424| [VibratePreset](#vibratepreset9) | 按照预置振动类型触发马达振动。 | 1425| [VibrateFromFile](#vibratefromfile10) | 按照自定义振动配置文件触发马达振动。 | 1426| [VibrateFromPattern<sup>18+</sup>](#vibratefrompattern18) | 按照自定义振动效果触发马达振动。 | 1427 1428## VibrateTime<sup>9+</sup> 1429 1430指定时长振动类型。 1431 1432**原子化服务API**:从API Version 11开始,该接口在支持原子化服务中使用。 1433 1434**系统能力**:SystemCapability.Sensors.MiscDevice 1435 1436| 名称 | 类型 | 必填 | 说明 | 1437| -------- | ------ | ---- | ----------------------------------------------------------- | 1438| type | 'time' | 是 | 值为'time',按照指定时长触发马达振动。 | 1439| duration | number | 是 | 马达持续振动时长, 单位ms。取值范围(0,1800000]区间内所有整数 | 1440 1441## VibratePreset<sup>9+</sup> 1442 1443预置振动类型。当调用[vibrator.startVibration9+](#vibratorstartvibration9)或[vibrator.startVibration9+](#vibratorstartvibration9-1)时,[VibrateEffect9+](#vibrateeffect9)参数的值可以为VibratePreset,表示触发预置振动类型。 1444 1445**系统能力**:SystemCapability.Sensors.MiscDevice 1446 1447| 名称 | 类型 | 必填 | 说明 | 1448| ----------------------- | -------- | ---- | ------------------------------------------------------------ | 1449| type | 'preset' | 是 | 值为'preset',按照预置振动效果触发马达振动。 | 1450| effectId | string | 是 | 预置的振动效果ID,字符串最大长度64,超出截取64。 | 1451| count | number | 否 | 可选参数,振动的重复次数,默认值为1。 | 1452| intensity<sup>12+</sup> | number | 否 | 可选参数,振动调节强度,取值范围(0,100]内所有整数,默认值为100。若振动效果不支持强度调节或设备不支持时,则按默认强度振动。 | 1453 1454## VibrateFromFile<sup>10+</sup> 1455 1456自定义振动类型。仅部分设备支持。当设备不支持此振动类型时,返回设备不支持错误码。当调用[vibrator.startVibration9+](#vibratorstartvibration9)或[vibrator.startVibration9+](#vibratorstartvibration9-1)时,[VibrateEffect9+](#vibrateeffect9)参数的值可以为VibrateFromFile,表示触发自定义振动类型。 1457 1458**系统能力**:SystemCapability.Sensors.MiscDevice 1459 1460| 名称 | 类型 | 必填 | 说明 | 1461| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------ | 1462| type | 'file' | 是 | 值为'file',按照振动配置文件触发马达振动。 | 1463| hapticFd | [HapticFileDescriptor](#hapticfiledescriptor10)<sup>10+</sup> | 是 | 振动配置文件的描述符。 | 1464 1465## HapticFileDescriptor<sup>10+</sup> 1466 1467自定义振动配置文件的描述符,必须确认资源文件可用,其参数可通过[文件管理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#振动效果说明)。 1468 1469**系统能力**:SystemCapability.Sensors.MiscDevice 1470 1471| 名称 | 类型 | 必填 | 说明 | 1472| ------ | ------ | ---- | ------------------------------------------------------------ | 1473| fd | number | 是 | 资源文件描述符。 | 1474| offset | number | 否 | 距文件起始位置的偏移量,单位为字节,默认为文件起始位置,不可超出文件有效范围。 | 1475| length | number | 否 | 资源长度,单位为字节,默认值为从偏移位置至文件结尾的长度,不可超出文件有效范围。 | 1476 1477## VibratorEventType<sup>18+</sup> 1478 1479振动事件类型。 1480 1481**系统能力**:SystemCapability.Sensors.MiscDevice 1482 1483| 名称 | 类型 | 必填 | 说明 | 1484| ---------- | ------ | ---- | ----------------- | 1485| CONTINUOUS | number | 是 | 值为0,表示长振。 | 1486| TRANSIENT | number | 是 | 值为1,表示短振。 | 1487 1488## VibratorCurvePoint<sup>18+</sup> 1489 1490相对事件振动强度的增益。 1491 1492**系统能力**:SystemCapability.Sensors.MiscDevice 1493 1494| 名称 | 类型 | 必填 | 说明 | 1495| --------- | ------ | ---- | ------------------------------------------------------------ | 1496| time | number | 是 | 起始时间偏移。 | 1497| intensity | number | 否 | 可选参数,相对事件振动强度增益,取值范围[0,100%],省略时默认值为1。 | 1498| frequency | number | 否 | 可选参数,相对事件振动频率变化,取值范围[-100,100]内所有整数,省略时默认值为0。 | 1499 1500## VibratorEvent<sup>18+</sup> 1501 1502振动事件。 1503 1504**系统能力**:SystemCapability.Sensors.MiscDevice 1505 1506| 名称 | 类型 | 必填 | 说明 | 1507| --------- | ------------------------------- | ---- | ------------------------------------------------------------ | 1508| eventType | VibratorEventType | 是 | 振动事件类型。 | 1509| time | number | 是 | 振动起始时间,单位ms。取值范围[0,1800000]区间内所有整数。 | 1510| duration | number | 否 | 可选参数,表示振动持续时间,取值范围(0,5000]区间所有整数,短振默认值为48,长振默认值为1000 | 1511| intensity | number | 否 | 可选参数,表示振动强度,取值范围[0,100]区间所有整数,省略时默认值为100。 | 1512| frequency | number | 否 | 可选参数,表示振动频率,取值范围[0,100]区间内所有整数,省略时默认值为50。 | 1513| index | number | 否 | 可选参数,表示通道编号,取值范围[0,2]区间内所有整数,省略时默认值为0。 | 1514| points | Array<[VibratorCurvePoint](#vibratorcurvepoint18)> | 否 | 可选参数,表示振动调节曲线数组。 | 1515 1516## VibratorPattern<sup>18+</sup> 1517 1518马达振动序列,每个events代表一个振动事件。 1519 1520**系统能力**:SystemCapability.Sensors.MiscDevice 1521 1522| 名称 | 类型 | 必填 | 说明 | 1523| ------ | -------------------------- | ---- | ---------------------------------------------------- | 1524| time | number | 是 | 振动绝对起始时间。 | 1525| events | Array<[VibratorEvent](#vibratorevent18)> | 是 | 振动事件数组,build()方法返回的VibratorPattern对象。 | 1526 1527## ContinuousParam<sup>18+</sup> 1528 1529连续振动参数。 1530 1531**系统能力**:SystemCapability.Sensors.MiscDevice 1532 1533| 名称 | 类型 | 必填 | 说明 | 1534| --------- | -------------------- | ---- | ------------------------------------------------------------ | 1535| intensity | number | 否 | 可选参数,表示振动强度,取值范围[0,100]内所有整数,省略时默认值为100。 | 1536| frequency | number | 否 | 可选参数,表示振动频率,取值范围[0,100]内所有整数,省略时默认值为50。 | 1537| points | [VibratorCurvePoint](#vibratorcurvepoint18)[] | 否 | 可选参数,表示振动调节曲线数组。 | 1538| index | number | 否 | 可选参数,表示通道编号,取值范围[0,2]区间内所有整数,省略时默认值为0。 | 1539 1540## TransientParam<sup>18+</sup> 1541 1542瞬态振动参数。 1543 1544**系统能力**:SystemCapability.Sensors.MiscDevice 1545 1546| 名称 | 类型 | 必填 | 说明 | 1547| --------- | ------ | ---- | ------------------------------------------------------------ | 1548| intensity | number | 否 | 可选参数,表示振动强度,取值范围[0,100]内所有整数,省略时默认值为100。 | 1549| frequency | number | 否 | 可选参数,表示振动频率,取值范围[0,100]内所有整数,省略时默认值为50。 | 1550| index | number | 否 | 可选参数,表示通道编号,取值范围[0,2]区间内所有整数,省略时默认值为0。 | 1551 1552## VibrateFromPattern<sup>18+</sup> 1553 1554自定义振动效果触发马达振动。 1555 1556**系统能力**:SystemCapability.Sensors.MiscDevice 1557 1558| 名称 | 类型 | 必填 | 说明 | 1559| ------- | --------------- | ---- | ---------------------------------------------------- | 1560| type | 'pattern' | 是 | 值为“pattern”,根据组合模式触发电机振动。 | 1561| pattern | VibratorPattern | 是 | 振动事件数组,build()方法返回的VibratorPattern对象。 | 1562 1563## VibrateAttribute<sup>9+</sup> 1564 1565马达振动属性。 1566 1567**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 1568 1569**系统能力**:SystemCapability.Sensors.MiscDevice 1570 1571| 名称 | 类型 | 必填 | 说明 | 1572| ---------------------- | ---------------- | ---- | ------------------------------------------------------------ | 1573| id | number | 否 | 马达ID, 默认值为0。 | 1574| deviceId<sup>19+</sup> | number | 否 | 设备ID,默认值为-1,控制的为本地设备,其它设备Id需使用[getEffectInfoSync](#vibratorgeteffectinfosync19)查询。 <br/>**原子化服务API**:从API Version 19开始,该接口支持在原子化服务中使用。 | 1575| usage | [Usage](#usage9) | 是 | 马达振动的使用场景。默认值为'unknown',取值范围只允许在[Usage](#usage9)提供的类型中选取。 | 1576 1577## Usage<sup>9+</sup> 1578 1579type Usage = 'unknown' | 'alarm' | 'ring' | 'notification' | 'communication' | 'touch' | 'media' | 'physicalFeedback' | 'simulateReality' 1580 1581振动使用场景。 1582 1583**原子化服务API**:从API Version 11开始,该接口支持在原子化服务中使用。 1584 1585**系统能力**:SystemCapability.Sensors.MiscDevice 1586<!--RP1--> 1587 1588| 类型 | 说明 | 1589| ------------------ | ------------------------------------------------------- | 1590| 'unknown' | 没有明确使用场景,最低优先级,值固定为'unknown'字符串。 | 1591| 'alarm' | 用于警报场景,值固定为'alarm'字符串。 | 1592| 'ring' | 用于铃声场景,值固定为'ring'字符串。 | 1593| 'notification' | 用于通知场景,值固定为'notification'字符串。 | 1594| 'communication' | 用于通信场景,值固定为'communication'字符串。 | 1595| 'touch' | 用于触摸场景,值固定为'touch'字符串。 | 1596| 'media' | 用于多媒体场景,值固定为'media'字符串。 | 1597| 'physicalFeedback' | 用于物理反馈场景,值固定为'physicalFeedback'字符串。 | 1598| 'simulateReality' | 用于模拟现实场景,值固定为'simulateReality'字符串。 | 1599 1600<!--RP1End--> 1601 1602## vibrator.vibrate<sup>(deprecated)</sup> 1603 1604vibrate(duration: number): Promise<void> 1605 1606按照指定持续时间触发马达振动。 1607 1608从API version 9 开始不再维护,建议使用 [vibrator.startVibration](#vibratorstartvibration9-1)<sup>9+</sup>代替。 1609 1610**需要权限**:ohos.permission.VIBRATE 1611 1612**系统能力**:SystemCapability.Sensors.MiscDevice 1613 1614**参数**: 1615 1616| 参数名 | 类型 | 必填 | 说明 | 1617| -------- | ------ | ---- | ------------------------------------------------------------ | 1618| duration | number | 是 | 马达振动时长, 单位ms;取值范围是(0,1800000]区间的所有整数。 | 1619 1620**返回值**: 1621 1622| 类型 | 说明 | 1623| ------------------- | ------------- | 1624| Promise<void> | Promise对象。 | 1625 1626**示例**: 1627 1628 ```ts 1629 import { vibrator } from '@kit.SensorServiceKit'; 1630 import { BusinessError } from '@kit.BasicServicesKit'; 1631 1632 vibrator.vibrate(1000).then(() => { 1633 console.info('Succeed in vibrating'); 1634 }, (error: BusinessError) => { 1635 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 1636 }); 1637 ``` 1638 1639## vibrator.vibrate<sup>(deprecated)</sup> 1640 1641vibrate(duration: number, callback?: AsyncCallback<void>): void 1642 1643按照指定持续时间触发马达振动。 1644 1645从API version 9 开始不再维护,建议使用 [vibrator.startVibration](#vibratorstartvibration9)<sup>9+</sup>代替。 1646 1647**需要权限**:ohos.permission.VIBRATE 1648 1649**系统能力**:SystemCapability.Sensors.MiscDevice 1650 1651**参数**: 1652 1653| 参数名 | 类型 | 必填 | 说明 | 1654| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 1655| duration | number | 是 | 马达振动时长, 单位ms。取值范围是(0,1800000]区间的所有整数。 | 1656| callback | AsyncCallback<void> | 否 | 回调函数,当马达振动成功,err为undefined,否则为错误对象。 | 1657 1658**示例**: 1659 1660 ```ts 1661 import { vibrator } from '@kit.SensorServiceKit'; 1662 import { BusinessError } from '@kit.BasicServicesKit'; 1663 1664 vibrator.vibrate(1000, (error: BusinessError) => { 1665 if (error) { 1666 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 1667 } else { 1668 console.info('Succeed in vibrating'); 1669 } 1670 }) 1671 ``` 1672 1673 1674## vibrator.vibrate<sup>(deprecated)</sup> 1675 1676vibrate(effectId: EffectId): Promise<void> 1677 1678按照预置振动效果触发马达振动。 1679 1680从API version 9 开始不再维护,建议使用 [vibrator.startVibration](#vibratorstartvibration9-1)<sup>9+</sup>代替。 1681 1682**需要权限**:ohos.permission.VIBRATE 1683 1684**系统能力**:SystemCapability.Sensors.MiscDevice 1685 1686**参数**: 1687 1688| 参数名 | 类型 | 必填 | 说明 | 1689| -------- | --------------------- | ---- | ------------------ | 1690| effectId | [EffectId](#effectid) | 是 | 预置的振动效果ID,字符串最大长度64,超出截取64,建议先查询是否支持。 | 1691 1692**返回值**: 1693 1694| 类型 | 说明 | 1695| ------------------- | ------------- | 1696| Promise<void> | Promise对象。 | 1697 1698**示例**: 1699 1700 ```ts 1701 import { vibrator } from '@kit.SensorServiceKit'; 1702 import { BusinessError } from '@kit.BasicServicesKit'; 1703 1704 vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(() => { 1705 console.info('Succeed in vibrating'); 1706 }, (error: BusinessError) => { 1707 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 1708 }); 1709 ``` 1710 1711 1712## vibrator.vibrate<sup>(deprecated)</sup> 1713 1714vibrate(effectId: EffectId, callback?: AsyncCallback<void>): void 1715 1716按照指定振动效果触发马达振动。 1717 1718从API version 9 开始不再维护,建议使用 [vibrator.startVibration](#vibratorstartvibration9)<sup>9+</sup>代替。 1719 1720**需要权限**:ohos.permission.VIBRATE 1721 1722**系统能力**:SystemCapability.Sensors.MiscDevice 1723 1724**参数**: 1725 1726| 参数名 | 类型 | 必填 | 说明 | 1727| -------- | ------------------------- | ---- | ---------------------------------------------------------- | 1728| effectId | [EffectId](#effectid) | 是 | 预置的振动效果ID,字符串最大长度64,超出截取64,建议先查询是否支持。 | 1729| callback | AsyncCallback<void> | 否 | 回调函数,当马达振动成功,err为undefined,否则为错误对象。 | 1730 1731**示例**: 1732 1733 ```ts 1734 import { vibrator } from '@kit.SensorServiceKit'; 1735 import { BusinessError } from '@kit.BasicServicesKit'; 1736 1737 vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => { 1738 if (error) { 1739 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 1740 } else { 1741 console.info('Succeed in vibrating'); 1742 } 1743 }) 1744 ``` 1745 1746## vibrator.stop<sup>(deprecated)</sup> 1747 1748stop(stopMode: VibratorStopMode): Promise<void> 1749 1750按照指定模式停止马达的振动。 1751 1752从API version 9 开始不再维护,建议使用 [vibrator.stopVibration](#vibratorstopvibration9-1)<sup>9+</sup>代替。 1753 1754**需要权限**:ohos.permission.VIBRATE 1755 1756**系统能力**:SystemCapability.Sensors.MiscDevice 1757 1758**参数**: 1759 1760| 参数名 | 类型 | 必填 | 说明 | 1761| -------- | ------------------------------------- | ---- | ------------------------ | 1762| stopMode | [VibratorStopMode](#vibratorstopmode) | 是 | 马达停止指定的振动模式。 | 1763 1764**返回值**: 1765 1766| 类型 | 说明 | 1767| ------------------- | ------------- | 1768| Promise<void> | Promise对象。 | 1769 1770**示例**: 1771 1772 ```ts 1773 import { vibrator } from '@kit.SensorServiceKit'; 1774 import { BusinessError } from '@kit.BasicServicesKit'; 1775 1776 // 按照effectId类型启动振动 1777 vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => { 1778 if (error) { 1779 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 1780 } else { 1781 console.info('Succeed in vibrating'); 1782 } 1783 }) 1784 // 使用VIBRATOR_STOP_MODE_PRESET模式停止振动 1785 vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => { 1786 console.info('Succeed in stopping'); 1787 }, (error: BusinessError) => { 1788 console.error(`Failed to stop. Code: ${error.code}, message: ${error.message}`); 1789 }); 1790 ``` 1791 1792 1793## vibrator.stop<sup>(deprecated)</sup> 1794 1795stop(stopMode: VibratorStopMode, callback?: AsyncCallback<void>): void 1796 1797按照指定模式停止马达的振动。 1798 1799从API version 9 开始不再维护,建议使用 [vibrator.stopVibration](#vibratorstopvibration9)<sup>9+</sup>代替。 1800 1801**需要权限**:ohos.permission.VIBRATE 1802 1803**系统能力**:SystemCapability.Sensors.MiscDevice 1804 1805**参数**: 1806 1807| 参数名 | 类型 | 必填 | 说明 | 1808| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 1809| stopMode | [VibratorStopMode](#vibratorstopmode) | 是 | 马达停止指定的振动模式。 | 1810| callback | AsyncCallback<void> | 否 | 回调函数,当马达停止振动成功,err为undefined,否则为错误对象。 | 1811 1812**示例**: 1813 1814 ```ts 1815 import { vibrator } from '@kit.SensorServiceKit'; 1816 import { BusinessError } from '@kit.BasicServicesKit'; 1817 1818 // 按照effectId类型启动振动 1819 vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => { 1820 if (error) { 1821 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 1822 } else { 1823 console.info('Succeed in vibrating'); 1824 } 1825 }) 1826 // 使用VIBRATOR_STOP_MODE_PRESET模式停止振动 1827 vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, (error: BusinessError) => { 1828 if (error) { 1829 console.error(`Failed to stop. Code: ${error.code}, message: ${error.message}`); 1830 } else { 1831 console.info('Succeed in stopping'); 1832 } 1833 }) 1834 ```