1# @ohos.vibrator (振动) 2 3vibrator模块提供控制马达振动启、停的能力。 4 5> **说明:** 6> 7> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9 10## 导入模块 11 12```ts 13import vibrator from '@ohos.vibrator'; 14``` 15 16## vibrator.startVibration<sup>9+</sup> 17 18startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: AsyncCallback<void>): void 19 20根据指定振动效果和振动属性触发马达振动。 21 22**需要权限**:ohos.permission.VIBRATE 23 24**系统能力**:SystemCapability.Sensors.MiscDevice 25 26**参数:** 27 28| 参数名 | 类型 | 必填 | 说明 | 29| --------- | -------------------------------------- | ---- | :--------------------------------------------------------- | 30| effect | [VibrateEffect](#vibrateeffect9) | 是 | 马达振动效果。 | 31| attribute | [VibrateAttribute](#vibrateattribute9) | 是 | 马达振动属性。 | 32| callback | AsyncCallback<void> | 是 | 回调函数,当马达振动成功,err为undefined,否则为错误对象。 | 33 34**错误码**: 35 36以下错误码的详细介绍请参见 [ohos.vibrator错误码](../errorcodes/errorcode-vibrator.md) 37 38| 错误码ID | 错误信息 | 39| -------- | ------------------------ | 40| 14600101 | Device operation failed. | 41 42示例: 43 44```ts 45import vibrator from '@ohos.vibrator'; 46import { BusinessError } from '@ohos.base'; 47 48try { 49 vibrator.startVibration({ 50 type: 'time', 51 duration: 1000, 52 }, { 53 id: 0, 54 usage: 'alarm' 55 }, (error: BusinessError) => { 56 if (error) { 57 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 58 return; 59 } 60 console.info('Succeed in starting vibration'); 61 }); 62} catch (err) { 63 let e: BusinessError = err as BusinessError; 64 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 65} 66``` 67 68## vibrator.startVibration<sup>9+</sup> 69 70startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise<void> 71 72根据指定振动效果和振动属性触发马达振动。 73 74**需要权限**:ohos.permission.VIBRATE 75 76**系统能力**:SystemCapability.Sensors.MiscDevice 77 78**参数:** 79 80| 参数名 | 类型 | 必填 | 说明 | 81| --------- | -------------------------------------- | ---- | -------------- | 82| effect | [VibrateEffect](#vibrateeffect9) | 是 | 马达振动效果。 | 83| attribute | [VibrateAttribute](#vibrateattribute9) | 是 | 马达振动属性。 | 84 85**返回值:** 86 87| 类型 | 说明 | 88| ------------------- | -------------------------------------- | 89| Promise<void> | Promise对象。 | 90 91**错误码**: 92 93以下错误码的详细介绍请参见 [ohos.vibrator错误码](../errorcodes/errorcode-vibrator.md) 94 95| 错误码ID | 错误信息 | 96| -------- | ------------------------ | 97| 14600101 | Device operation failed. | 98 99**示例:** 100 101```ts 102import vibrator from '@ohos.vibrator'; 103import { BusinessError } from '@ohos.base'; 104 105try { 106 vibrator.startVibration({ 107 type: 'time', 108 duration: 1000 109 }, { 110 id: 0, 111 usage: 'alarm' 112 }).then(() => { 113 console.info('Succeed in starting vibration'); 114 }, (error: BusinessError) => { 115 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 116 }); 117} catch (err) { 118 let e: BusinessError = err as BusinessError; 119 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 120} 121``` 122 123## vibrator.stopVibration<sup>9+</sup> 124 125stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback<void>): void 126 127按照指定模式停止马达的振动。 128 129**需要权限**:ohos.permission.VIBRATE 130 131**系统能力**:SystemCapability.Sensors.MiscDevice 132 133**参数:** 134 135| 参数名 | 类型 | 必填 | 说明 | 136| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 137| stopMode | [VibratorStopMode](#vibratorstopmode) | 是 | 指定的停止振动模式。 | 138| callback | AsyncCallback<void> | 是 | 回调函数,当马达停止振动成功,err为undefined,否则为错误对象。 | 139 140**示例:** 141 142```ts 143import vibrator from '@ohos.vibrator'; 144import { BusinessError } from '@ohos.base'; 145 146try { 147 // 按照固定时长振动 148 vibrator.startVibration({ 149 type: 'time', 150 duration: 1000, 151 }, { 152 id: 0, 153 usage: 'alarm' 154 }, (error: BusinessError) => { 155 if (error) { 156 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 157 return; 158 } 159 console.info('Succeed in starting vibration'); 160 }); 161} catch (err) { 162 let e: BusinessError = err as BusinessError; 163 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 164} 165 166try { 167 // 按照VIBRATOR_STOP_MODE_TIME模式停止振动 168 vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, (error: BusinessError) => { 169 if (error) { 170 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 171 return; 172 } 173 console.info('Succeed in stopping vibration'); 174 }) 175} catch (err) { 176 let e: BusinessError = err as BusinessError; 177 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 178} 179``` 180 181## vibrator.stopVibration<sup>9+</sup> 182 183stopVibration(stopMode: VibratorStopMode): Promise<void> 184 185按照指定模式停止马达的振动。 186 187**需要权限**:ohos.permission.VIBRATE 188 189**系统能力**:SystemCapability.Sensors.MiscDevice 190 191**参数:** 192 193| 参数名 | 类型 | 必填 | 说明 | 194| -------- | ------------------------------------- | ---- | ------------------------ | 195| stopMode | [VibratorStopMode](#vibratorstopmode) | 是 | 马达停止指定的振动模式。 | 196 197**返回值:** 198 199| 类型 | 说明 | 200| ------------------- | -------------------------------------- | 201| Promise<void> | Promise对象。 | 202 203**示例:** 204 205```ts 206import vibrator from '@ohos.vibrator'; 207import { BusinessError } from '@ohos.base'; 208 209try { 210 // 按照固定时长振动 211 vibrator.startVibration({ 212 type: 'time', 213 duration: 1000, 214 }, { 215 id: 0, 216 usage: 'alarm' 217 }).then(() => { 218 console.info('Succeed in starting vibration'); 219 }, (error: BusinessError) => { 220 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 221 }); 222} catch (err) { 223 let e: BusinessError = err as BusinessError; 224 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 225} 226 227try { 228 // 按照VIBRATOR_STOP_MODE_TIME模式停止振动 229 vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME).then(() => { 230 console.info('Succeed in stopping vibration'); 231 }, (error: BusinessError) => { 232 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 233 }); 234} catch (err) { 235 let e: BusinessError = err as BusinessError; 236 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 237} 238``` 239 240## vibrator.stopVibration<sup>10+</sup> 241 242stopVibration(callback: AsyncCallback<void>): void 243 244停止所有模式的马达振动。 245 246**需要权限**:ohos.permission.VIBRATE 247 248**系统能力**:SystemCapability.Sensors.MiscDevice 249 250**参数:** 251 252| 参数名 | 类型 | 必填 | 说明 | 253| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 254| callback | AsyncCallback<void> | 是 | 回调函数,当马达停止振动成功,err为undefined,否则为错误对象。 | 255 256**示例:** 257 258```ts 259import vibrator from '@ohos.vibrator'; 260import { BusinessError } from '@ohos.base'; 261 262try { 263 // 按照固定时长振动 264 vibrator.startVibration({ 265 type: 'time', 266 duration: 1000, 267 }, { 268 id: 0, 269 usage: 'alarm' 270 }, (error: BusinessError) => { 271 if (error) { 272 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 273 return; 274 } 275 console.info('Succeed in starting vibration'); 276 }); 277} catch (error) { 278 let e: BusinessError = error as BusinessError; 279 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 280} 281 282try { 283 // 停止所有模式的马达振动 284 vibrator.stopVibration((error: BusinessError) => { 285 if (error) { 286 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 287 return; 288 } 289 console.info('Succeed in stopping vibration'); 290 }) 291} catch (error) { 292 let e: BusinessError = error as BusinessError; 293 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 294} 295``` 296 297## vibrator.stopVibration<sup>10+</sup> 298 299stopVibration(): Promise<void> 300 301停止所有模式的马达振动。 302 303**需要权限**:ohos.permission.VIBRATE 304 305**系统能力**:SystemCapability.Sensors.MiscDevice 306 307**返回值:** 308 309| 类型 | 说明 | 310| ------------------- | ------------- | 311| Promise<void> | Promise对象。 | 312 313**示例:** 314 315```ts 316import vibrator from '@ohos.vibrator'; 317import { BusinessError } from '@ohos.base'; 318 319try { 320 // 按照固定时长振动 321 vibrator.startVibration({ 322 type: 'time', 323 duration: 1000, 324 }, { 325 id: 0, 326 usage: 'alarm' 327 }).then(() => { 328 console.info('Succeed in starting vibration'); 329 }, (error: BusinessError) => { 330 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 331 }); 332} catch (error) { 333 let e: BusinessError = error as BusinessError; 334 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 335} 336 337try { 338 // 停止所有模式的马达振动 339 vibrator.stopVibration().then(() => { 340 console.info('Succeed in stopping vibration'); 341 }, (error: BusinessError) => { 342 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 343 }); 344} catch (error) { 345 let e: BusinessError = error as BusinessError; 346 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 347} 348``` 349 350## vibrator.isSupportEffect<sup>10+</sup> 351 352isSupportEffect(effectId: string, callback: AsyncCallback<boolean>): void 353 354查询是否支持传入的参数effectId。 355 356**系统能力**:SystemCapability.Sensors.MiscDevice 357 358**参数:** 359 360| 参数名 | 类型 | 必填 | 说明 | 361| -------- | ---------------------------- | ---- | ------------------------------------------------------ | 362| effectId | string | 是 | 振动效果id | 363| callback | AsyncCallback<boolean> | 是 | 回调函数,当返回true则表示支持该effectId,否则不支持。 | 364 365**示例:** 366 367```ts 368import vibrator from '@ohos.vibrator'; 369import { BusinessError } from '@ohos.base'; 370 371try { 372 // 查询是否支持'haptic.clock.timer' 373 vibrator.isSupportEffect('haptic.clock.timer', (err: BusinessError, state: boolean) => { 374 if (err) { 375 console.error(`Failed to query effect. Code: ${err.code}, message: ${err.message}`); 376 return; 377 } 378 console.info('Succeed in querying effect'); 379 if (state) { 380 try { 381 // 使用startVibration需要添加ohos.permission.VIBRATE权限 382 vibrator.startVibration({ 383 type: 'preset', 384 effectId: 'haptic.clock.timer', 385 count: 1, 386 }, { 387 usage: 'unknown' 388 }, (error: BusinessError) => { 389 if (error) { 390 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 391 } else { 392 console.info('Succeed in starting vibration'); 393 } 394 }); 395 } catch (error) { 396 let e: BusinessError = error as BusinessError; 397 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 398 } 399 } 400 }) 401} catch (error) { 402 let e: BusinessError = error as BusinessError; 403 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 404} 405``` 406 407## vibrator.isSupportEffect<sup>10+</sup> 408 409isSupportEffect(effectId: string): Promise<boolean> 410 411查询是否支持传入的参数effectId。 412 413**系统能力**:SystemCapability.Sensors.MiscDevice 414 415**参数:** 416 417| 参数名 | 类型 | 必填 | 说明 | 418| -------- | ------ | ---- | ------------ | 419| effectId | string | 是 | 振动效果id。 | 420 421**返回值:** 422 423| 类型 | 说明 | 424| ---------------------- | --------------------------------------------------------- | 425| Promise<boolean> | Promise对象。当返回true则表示支持该effectId,否则不支持。 | 426 427**示例:** 428 429```ts 430import vibrator from '@ohos.vibrator'; 431import { BusinessError } from '@ohos.base'; 432 433try { 434 // 查询是否支持'haptic.clock.timer' 435 vibrator.isSupportEffect('haptic.clock.timer').then((state: boolean) => { 436 console.info(`The query result is ${state}`); 437 if (state) { 438 try { 439 vibrator.startVibration({ 440 type: 'preset', 441 effectId: 'haptic.clock.timer', 442 count: 1, 443 }, { 444 usage: 'unknown' 445 }).then(() => { 446 console.info('Succeed in starting vibration'); 447 }).catch((error: BusinessError) => { 448 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 449 }); 450 } catch (error) { 451 let e: BusinessError = error as BusinessError; 452 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 453 } 454 } 455 }, (error: BusinessError) => { 456 console.error(`Failed to query effect. Code: ${error.code}, message: ${error.message}`); 457 }) 458} catch (error) { 459 let e: BusinessError = error as BusinessError; 460 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 461} 462``` 463 464## EffectId 465 466预置的振动效果。 467 468**系统能力**:SystemCapability.Sensors.MiscDevice 469 470| 名称 | 值 | 说明 | 471| ------------------ | -------------------- | -------------------------------- | 472| EFFECT_CLOCK_TIMER | "haptic.clock.timer" | 描述用户调整计时器时的振动效果。 | 473 474 475## VibratorStopMode 476 477停止的振动模式。 478 479**系统能力**:SystemCapability.Sensors.MiscDevice 480 481| 名称 | 值 | 说明 | 482| ------------------------- | -------- | ------------------------------ | 483| VIBRATOR_STOP_MODE_TIME | "time" | 停止模式为duration模式的振动。 | 484| VIBRATOR_STOP_MODE_PRESET | "preset" | 停止模式为预置EffectId的振动。 | 485 486## VibrateEffect<sup>9+</sup> 487 488马达振动效果。 489 490**系统能力**:SystemCapability.Sensors.MiscDevice 491 492| 类型 | 说明 | 493| -------------------------------- | ------------------------------ | 494| [VibrateTime](#vibratetime9) | 按照指定持续时间触发马达振动。 | 495| [VibratePreset](#vibratepreset9) | 按照预置振动类型触发马达振动。 | 496| [VibrateFromFile<sup>10+</sup>](#vibratefromfile10) | 按照自定义振动配置文件触发马达振动。 | 497 498## VibrateTime<sup>9+</sup> 499 500马达振动时长。 501 502**系统能力**:SystemCapability.Sensors.MiscDevice 503 504| 名称 | 类型 | 必填 | 说明 | 505| -------- | ------ | ----- | ------------------------------ | 506| type | string | 是 | 值为"time",按照指定持续时间触发马达振动。 | 507| duration | number | 是 | 马达持续振动时长, 单位ms。 | 508 509## VibratePreset<sup>9+</sup> 510 511马达预置振动类型。 512 513**系统能力**:SystemCapability.Sensors.MiscDevice 514 515| 名称 | 类型 | 必填 | 说明 | 516| -------- | -------- | ---- |------------------------------ | 517| type | string | 是 | 值为"preset",按照预置振动效果触发马达振动。 | 518| effectId | string | 是 | 预置的振动效果ID。 | 519| count | number | 是 | 重复振动的次数。 | 520 521## VibrateFromFile<sup>10+</sup> 522 523自定义振动类型,仅部分设备支持,当设备不支持此振动类型时,返回[设备不支持错误码](../errorcodes/errorcode-universal.md)。 524 525**系统能力**:SystemCapability.Sensors.MiscDevice 526 527| 名称 | 类型 | 必填 | 说明 | 528| -------- | -------- | ---- | ------------------------------ | 529| type | string | 是 | 值为"file",按照振动配置文件触发马达振动。 | 530| hapticFd | [HapticFileDescriptor](#hapticfiledescriptor10) | 是 | 振动配置文件的描述符。| 531 532## HapticFileDescriptor<sup>10+</sup> 533 534自定义振动配置文件的描述符,必须确认资源文件可用,其参数可通过[文件管理API](js-apis-file-fs.md#fsopen)从沙箱路径获取或者通过[资源管理API](js-apis-resource-manager.md#getrawfd9)从HAP资源获取。使用场景:振动序列被存储在一个文件中,需要根据偏移量和长度进行振动,振动序列存储格式,请参考[自定义振动格式](../../device/vibrator-guidelines.md#自定义振动格式)。 535 536**系统能力**:SystemCapability.Sensors.MiscDevice 537 538| 名称 | 类型 | 必填 | 说明 | 539| -------- | -------- |--------| ------------------------------| 540| fd | number | 是 | 资源文件描述符。 | 541| offset | number | 否 | 距文件起始位置的偏移量,单位为字节,默认为文件起始位置,不可超出文件有效范围。| 542| length | number | 否 | 资源长度,单位为字节,默认值为从偏移位置至文件结尾的长度,不可超出文件有效范围。| 543 544## VibrateAttribute<sup>9+</sup> 545 546马达振动属性。 547 548**系统能力**:SystemCapability.Sensors.MiscDevice 549 550| 名称 | 类型 | 必填 | 说明 | 551| ----- | ------ | ---- | -------------- | 552| id | number | 否 | 默认值为0,振动器id。 | 553| usage | [Usage](#usage9) | 是 | 马达振动的使用场景。 | 554 555## Usage<sup>9+</sup> 556 557振动使用场景。 558 559**系统能力**:SystemCapability.Sensors.MiscDevice 560 561| 名称 | 类型 | 说明 | 562| ---------------- | ------ | ------------------------------ | 563| unknown | string | 没有明确使用场景,最低优先级。 | 564| alarm | string | 用于警报场景。 | 565| ring | string | 用于铃声场景。 | 566| notification | string | 用于通知场景。 | 567| communication | string | 用于通信场景。 | 568| touch | string | 用于触摸场景。 | 569| media | string | 用于多媒体场景。 | 570| physicalFeedback | string | 用于物理反馈场景。 | 571| simulateReality | string | 用于模拟现实场景。 | 572 573## vibrator.vibrate<sup>(deprecated)</sup> 574 575vibrate(duration: number): Promise<void> 576 577按照指定持续时间触发马达振动。 578 579从API version 9 开始不再维护,建议使用 [vibrator.startVibration](#vibratorstartvibration9-1) 代替。 580 581**需要权限**:ohos.permission.VIBRATE 582 583**系统能力**:SystemCapability.Sensors.MiscDevice 584 585**参数:** 586 587| 参数名 | 类型 | 必填 | 说明 | 588| -------- | ------ | ---- | ---------------------- | 589| duration | number | 是 | 马达振动时长, 单位ms。 | 590 591**返回值:** 592 593| 类型 | 说明 | 594| ------------------- | -------------------------------------- | 595| Promise<void> | Promise对象。 | 596 597**示例:** 598 599```ts 600import vibrator from '@ohos.vibrator'; 601import { BusinessError } from '@ohos.base'; 602 603vibrator.vibrate(1000).then(() => { 604 console.info('Succeed in vibrating'); 605}, (error: BusinessError) => { 606 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 607}); 608``` 609 610## vibrator.vibrate<sup>(deprecated)</sup> 611 612vibrate(duration: number, callback?: AsyncCallback<void>): void 613 614按照指定持续时间触发马达振动。 615 616从API version 9 开始不再维护,建议使用 [vibrator.startVibration](#vibratorstartvibration9) 代替。 617 618**需要权限**:ohos.permission.VIBRATE 619 620**系统能力**:SystemCapability.Sensors.MiscDevice 621 622**参数:** 623 624| 参数名 | 类型 | 必填 | 说明 | 625| -------- | ------------------------- | ---- | ---------------------------------------------------------- | 626| duration | number | 是 | 马达振动时长, 单位ms。 | 627| callback | AsyncCallback<void> | 否 | 回调函数,当马达振动成功,err为undefined,否则为错误对象。 | 628 629**示例:** 630 631```ts 632import vibrator from '@ohos.vibrator'; 633import { BusinessError } from '@ohos.base'; 634 635vibrator.vibrate(1000, (error: BusinessError) => { 636 if (error) { 637 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 638 } else { 639 console.info('Succeed in vibrating'); 640 } 641}) 642``` 643 644 645## vibrator.vibrate<sup>(deprecated)</sup> 646 647vibrate(effectId: EffectId): Promise<void> 648 649按照预置振动效果触发马达振动。 650 651从API version 9 开始不再维护,建议使用 [vibrator.startVibration](#vibratorstartvibration9-1) 代替。 652 653**需要权限**:ohos.permission.VIBRATE 654 655**系统能力**:SystemCapability.Sensors.MiscDevice 656 657**参数:** 658 659| 参数名 | 类型 | 必填 | 说明 | 660| -------- | --------------------- | ---- | ------------------ | 661| effectId | [EffectId](#effectid) | 是 | 预置的振动效果ID。 | 662 663**返回值:** 664 665| 类型 | 说明 | 666| ------------------- | -------------------------------------- | 667| Promise<void> | Promise对象。 | 668 669**示例:** 670 671```ts 672import vibrator from '@ohos.vibrator'; 673import { BusinessError } from '@ohos.base'; 674 675vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(() => { 676 console.info('Succeed in vibrating'); 677}, (error: BusinessError) => { 678 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 679}); 680``` 681 682 683## vibrator.vibrate<sup>(deprecated)</sup> 684 685vibrate(effectId: EffectId, callback?: AsyncCallback<void>): void 686 687按照指定振动效果触发马达振动。 688 689从API version 9 开始不再维护,建议使用 [vibrator.startVibration](#vibratorstartvibration9) 代替。 690 691**需要权限**:ohos.permission.VIBRATE 692 693**系统能力**:SystemCapability.Sensors.MiscDevice 694 695**参数:** 696 697| 参数名 | 类型 | 必填 | 说明 | 698| -------- | ------------------------- | ---- | ---------------------------------------------------------- | 699| effectId | [EffectId](#effectid) | 是 | 预置的振动效果ID。 | 700| callback | AsyncCallback<void> | 否 | 回调函数,当马达振动成功,err为undefined,否则为错误对象。 | 701 702**示例:** 703 704```ts 705import vibrator from '@ohos.vibrator'; 706import { BusinessError } from '@ohos.base'; 707 708vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => { 709 if (error) { 710 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 711 } else { 712 console.info('Succeed in vibrating'); 713 } 714}) 715``` 716 717## vibrator.stop<sup>(deprecated)</sup> 718 719stop(stopMode: VibratorStopMode): Promise<void> 720 721按照指定模式停止马达的振动。 722 723从API version 9 开始不再维护,建议使用 [vibrator.stopVibration](#vibratorstopvibration9-1) 代替。 724 725**需要权限**:ohos.permission.VIBRATE 726 727**系统能力**:SystemCapability.Sensors.MiscDevice 728 729**参数:** 730 731| 参数名 | 类型 | 必填 | 说明 | 732| -------- | ------------------------------------- | ---- | ------------------------ | 733| stopMode | [VibratorStopMode](#vibratorstopmode) | 是 | 马达停止指定的振动模式。 | 734 735**返回值:** 736 737| 类型 | 说明 | 738| ------------------- | -------------------------------------- | 739| Promise<void> | Promise对象。 | 740 741**示例:** 742 743```ts 744import vibrator from '@ohos.vibrator'; 745import { BusinessError } from '@ohos.base'; 746 747// 按照effectId类型启动振动 748vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => { 749 if (error) { 750 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 751 } else { 752 console.info('Succeed in vibrating'); 753 } 754}) 755// 使用VIBRATOR_STOP_MODE_PRESET模式停止振动 756vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => { 757 console.info('Succeed in stopping'); 758}, (error: BusinessError) => { 759 console.error(`Failed to stop. Code: ${error.code}, message: ${error.message}`); 760}); 761``` 762 763 764## vibrator.stop<sup>(deprecated)</sup> 765 766stop(stopMode: VibratorStopMode, callback?: AsyncCallback<void>): void 767 768按照指定模式停止马达的振动。 769 770从API version 9 开始不再维护,建议使用 [vibrator.stopVibration](#vibratorstopvibration9) 代替。 771 772**需要权限**:ohos.permission.VIBRATE 773 774**系统能力**:SystemCapability.Sensors.MiscDevice 775 776**参数:** 777 778| 参数名 | 类型 | 必填 | 说明 | 779| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 780| stopMode | [VibratorStopMode](#vibratorstopmode) | 是 | 马达停止指定的振动模式。 | 781| callback | AsyncCallback<void> | 否 | 回调函数,当马达停止振动成功,err为undefined,否则为错误对象。 | 782 783**示例:** 784 785```ts 786import vibrator from '@ohos.vibrator'; 787import { BusinessError } from '@ohos.base'; 788 789// 按照effectId类型启动振动 790vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => { 791 if (error) { 792 console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); 793 } else { 794 console.info('Succeed in vibrating'); 795 } 796}) 797// 使用VIBRATOR_STOP_MODE_PRESET模式停止振动 798vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, (error: BusinessError) => { 799 if (error) { 800 console.error(`Failed to stop. Code: ${error.code}, message: ${error.message}`); 801 } else { 802 console.info('Succeed in stopping'); 803 } 804}) 805``` 806