js-apis-vibrator.md 25.5 KB
Newer Older
1
# @ohos.vibrator (Vibrator)
W
wusongqing 已提交
2

3
The **vibrator** module provides APIs for starting or stopping vibration.
W
wusongqing 已提交
4 5 6

> **NOTE**
>
W
wusongqing 已提交
7
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

W
wusongqing 已提交
9 10

## Modules to Import
Z
zengyawen 已提交
11

W
wusongqing 已提交
12
```js
W
wusongqing 已提交
13
import vibrator from '@ohos.vibrator';
Z
zengyawen 已提交
14 15
```

G
Gloria 已提交
16
## vibrator.startVibration<sup>9+</sup>
W
wusongqing 已提交
17

G
Gloria 已提交
18
startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: AsyncCallback&lt;void&gt;): void
W
wusongqing 已提交
19

20
Starts vibration with the specified effect and attribute. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
21

G
Gloria 已提交
22
**Required permissions**: ohos.permission.VIBRATE
Z
zengyawen 已提交
23

W
wusongqing 已提交
24
**System capability**: SystemCapability.Sensors.MiscDevice
Z
zengyawen 已提交
25

W
wusongqing 已提交
26
**Parameters**
G
Gloria 已提交
27

G
Gloria 已提交
28 29 30 31
| Name   | Type                                  | Mandatory| Description                                                      |
| --------- | -------------------------------------- | ---- | :--------------------------------------------------------- |
| effect    | [VibrateEffect](#vibrateeffect9)       | Yes  | Vibration effect.                                            |
| attribute | [VibrateAttribute](#vibrateattribute9) | Yes  | Vibration attribute.                                            |
32
| callback  | AsyncCallback&lt;void&gt;              | Yes  | Callback used to return the result. If the vibration starts, **err** is **undefined**; otherwise, **err** is an error object.|
W
wusongqing 已提交
33

G
Gloria 已提交
34 35 36 37
**Error codes**

For details about the error codes, see [Vibrator Error Codes](../errorcodes/errorcode-vibrator.md).

38 39 40
| ID| Error Message                |
| -------- | ------------------------ |
| 14600101 | Device operation failed. |
G
Gloria 已提交
41

W
wusongqing 已提交
42
**Example**
G
Gloria 已提交
43

G
Gloria 已提交
44
```js
G
Gloria 已提交
45
import vibrator from '@ohos.vibrator';
G
Gloria 已提交
46 47
try {
    vibrator.startVibration({
48 49 50 51
        type: 'time',
        duration: 1000,
    }, {
        id: 0,
G
Gloria 已提交
52
        usage: 'alarm'
53 54 55 56
    }, (error) => {
        if (error) {
            console.error('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
            return;
G
Gloria 已提交
57
        }
58
        console.log('Callback returned to indicate a successful vibration.');
G
Gloria 已提交
59
    });
60 61
} catch (err) {
    console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
G
Gloria 已提交
62 63
}
```
W
wusongqing 已提交
64

G
Gloria 已提交
65
## vibrator.startVibration<sup>9+</sup>
G
Gloria 已提交
66

G
Gloria 已提交
67
startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise&lt;void&gt;
G
Gloria 已提交
68

69
Starts vibration with the specified effect and attribute. This API uses a promise to return the result.
G
Gloria 已提交
70 71 72 73 74 75 76 77

**Required permissions**: ohos.permission.VIBRATE

**System capability**: SystemCapability.Sensors.MiscDevice

**Parameters**

| Name   | Type                                  | Mandatory| Description          |
G
Gloria 已提交
78
| --------- | -------------------------------------- | ---- | -------------- |
G
Gloria 已提交
79 80 81 82 83 84 85 86 87
| effect    | [VibrateEffect](#vibrateeffect9)       | Yes  | Vibration effect.|
| attribute | [VibrateAttribute](#vibrateattribute9) | Yes  | Vibration attribute.|

**Return value**

| Type               | Description                                  |
| ------------------- | -------------------------------------- |
| Promise&lt;void&gt; | Promise that returns no value.|

G
Gloria 已提交
88 89 90 91
**Error codes**

For details about the error codes, see [Vibrator Error Codes](../errorcodes/errorcode-vibrator.md).

92 93 94
| ID| Error Message                |
| -------- | ------------------------ |
| 14600101 | Device operation failed. |
G
Gloria 已提交
95

G
Gloria 已提交
96 97
**Example**

G
Gloria 已提交
98
  ```js
G
Gloria 已提交
99
import vibrator from '@ohos.vibrator';
G
Gloria 已提交
100 101
try {
    vibrator.startVibration({
102 103
        type: 'time',
        duration: 1000
G
Gloria 已提交
104 105 106
    }, {
        id: 0,
        usage: 'alarm'
107
    }).then(() => {
G
Gloria 已提交
108
        console.log('Promise returned to indicate a successful vibration');
109 110 111 112 113
    }, (error) => {
        console.error('error.code' + error.code + 'error.message' + error.message);
    });
} catch (err) {
    console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
G
Gloria 已提交
114
}
W
wusongqing 已提交
115 116
  ```

G
Gloria 已提交
117
## vibrator.stopVibration<sup>9+</sup>
W
wusongqing 已提交
118

G
Gloria 已提交
119
stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback&lt;void&gt;): void
W
wusongqing 已提交
120

121
Stops vibration in the specified mode. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
122

G
Gloria 已提交
123
**Required permissions**: ohos.permission.VIBRATE
W
wusongqing 已提交
124 125

**System capability**: SystemCapability.Sensors.MiscDevice
Z
zengyawen 已提交
126

W
wusongqing 已提交
127
**Parameters**
W
wusongqing 已提交
128

G
Gloria 已提交
129 130 131
| Name  | Type                                 | Mandatory| Description                                                        |
| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ |
| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes  | Mode to stop the vibration.                                    |
132
| callback | AsyncCallback&lt;void&gt;             | Yes  | Callback used to return the result. If the vibration stops, **err** is **undefined**; otherwise, **err** is an error object.|
W
wusongqing 已提交
133

W
wusongqing 已提交
134
**Example**
G
Gloria 已提交
135

W
wusongqing 已提交
136
  ```js
G
Gloria 已提交
137
import vibrator from '@ohos.vibrator';
G
Gloria 已提交
138
try {
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    // Start vibration at a fixed duration.
    vibrator.startVibration({
        type: 'time',
        duration: 1000,
    }, {
        id: 0,
        usage: 'alarm'
    }, (error) => {
        if (error) {
            console.error('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
            return;
        }
        console.log('Callback returned to indicate a successful vibration.');
    });
} catch (err) {
    console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
}

try {
    // Stop vibration in VIBRATOR_STOP_MODE_TIME mode.
    vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, function (error) {
        if (error) {
G
Gloria 已提交
161
            console.log('error.code' + error.code + 'error.message' + error.message);
162
            return;
G
Gloria 已提交
163
        }
164
        console.log('Callback returned to indicate successful.');
G
Gloria 已提交
165
    })
166 167
} catch (err) {
    console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
G
Gloria 已提交
168
}
W
wusongqing 已提交
169 170
  ```

G
Gloria 已提交
171
## vibrator.stopVibration<sup>9+</sup>
G
Gloria 已提交
172

G
Gloria 已提交
173
stopVibration(stopMode: VibratorStopMode): Promise&lt;void&gt;
W
wusongqing 已提交
174

175
Stops vibration in the specified mode. This API uses a promise to return the result.
W
wusongqing 已提交
176

G
Gloria 已提交
177
**Required permissions**: ohos.permission.VIBRATE
W
wusongqing 已提交
178 179

**System capability**: SystemCapability.Sensors.MiscDevice
W
wusongqing 已提交
180

W
wusongqing 已提交
181
**Parameters**
G
Gloria 已提交
182

G
Gloria 已提交
183 184 185
| Name  | Type                                 | Mandatory| Description                    |
| -------- | ------------------------------------- | ---- | ------------------------ |
| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes  | Mode to stop the vibration.|
W
wusongqing 已提交
186

W
wusongqing 已提交
187
**Return value**
G
Gloria 已提交
188 189 190 191

| Type               | Description                                  |
| ------------------- | -------------------------------------- |
| Promise&lt;void&gt; | Promise that returns no value.|
W
wusongqing 已提交
192

W
wusongqing 已提交
193
**Example**
G
Gloria 已提交
194

W
wusongqing 已提交
195
  ```js
G
Gloria 已提交
196
import vibrator from '@ohos.vibrator';
G
Gloria 已提交
197
try {
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    // Start vibration at a fixed duration.
    vibrator.startVibration({
        type: 'time',
        duration: 1000
    }, {
        id: 0,
        usage: 'alarm'
    }).then(() => {
        console.log('Promise returned to indicate a successful vibration');
    }, (error) => {
        console.error('error.code' + error.code + 'error.message' + error.message);
    });
} catch (err) {
    console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
}

try {
    // Stop vibration in VIBRATOR_STOP_MODE_TIME mode.
    vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => {
        console.log('Promise returned to indicate a successful vibration.');
    }, (error) => {
G
Gloria 已提交
219 220
        console.log('error.code' + error.code + 'error.message' + error.message);
    });
221 222
} catch (err) {
    console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
G
Gloria 已提交
223
}
W
wusongqing 已提交
224 225
  ```

G
Gloria 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
## vibrator.stopVibration<sup>10+</sup>

stopVibration(callback: AsyncCallback&lt;void&gt;): void

Stops vibration in all modes. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.VIBRATE

**System capability**: SystemCapability.Sensors.MiscDevice

**Parameters**

| Name  | Type                     | Mandatory| Description                                                        |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result. If the vibration stops, **err** is **undefined**; otherwise, **err** is an error object.|

**Example**

  ```js
import vibrator from '@ohos.vibrator';
try {
    // Start vibration at a fixed duration.
    vibrator.startVibration({
        type: 'time',
        duration: 1000,
    }, {
        id: 0,
        usage: 'alarm'
    }, (error) => {
        if (error) {
            console.error('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
            return;
        }
        console.log('Callback returned to indicate a successful vibration.');
    });
} catch (error) {
    console.error('errCode: ' + error.code + ' ,msg: ' + error.message);
}

try {
    // Stop vibration in all modes.
    vibrator.stopVibration(function (error) {
        if (error) {
            console.log('error.code' + error.code + 'error.message' + error.message);
            return;
        }
        console.log('Callback returned to indicate successful.');
    })
} catch (error) {
    console.info('errCode: ' + error.code + ' ,msg: ' + error.message);
}
  ```

## vibrator.stopVibration<sup>10+</sup>

stopVibration(): Promise&lt;void&gt;

Stops vibration in all modes. This API uses a promise to return the result.

**Required permissions**: ohos.permission.VIBRATE

**System capability**: SystemCapability.Sensors.MiscDevice

**Return value**

| Type               | Description         |
| ------------------- | ------------- |
| Promise&lt;void&gt; | Promise that returns no value.|

**Example**

  ```js
import vibrator from '@ohos.vibrator';
try {
    // Start vibration at a fixed duration.
    vibrator.startVibration({
        type: 'time',
        duration: 1000
    }, {
        id: 0,
        usage: 'alarm'
    }).then(() => {
        console.log('Promise returned to indicate a successful vibration');
    }, (error) => {
        console.error('error.code' + error.code + 'error.message' + error.message);
    });
} catch (error) {
    console.error('errCode: ' + error.code + ' ,msg: ' + error.message);
}

try {
    // Stop vibration in all modes.
    vibrator.stopVibration().then(() => {
        console.log('Promise returned to indicate a successful vibration.');
    }, (error) => {
        console.log('error.code' + error.code + 'error.message' + error.message);
    });
} catch (error) {
    console.info('errCode: ' + error.code + ' ,msg: ' + error.message);
}
  ```

## vibrator.isSupportEffect<sup>10+</sup>

isSupportEffect(effectId: string, callback: AsyncCallback&lt;boolean&gt;): void

Checks whether the passed effect ID is supported. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Sensors.MiscDevice

**Parameters**

| Name  | Type                        | Mandatory| Description                                                  |
| -------- | ---------------------------- | ---- | ------------------------------------------------------ |
| effectId | string                       | Yes  | Vibration effect ID.                                            |
| callback | AsyncCallback&lt;boolean&gt; | Yes  | Callback used to return the result. The value **true** means that the passed effect ID is supported, and **false** means the opposite. |

**Example**

  ```js
import vibrator from '@ohos.vibrator';
try {
    // Check whether 'haptic.clock.timer' is supported.
    vibrator.isSupportEffect('haptic.clock.timer', function (err, state) {
        if (err) {
            console.error('isSupportEffect failed, error:' + JSON.stringify(err));
            return;
        }
        console.log('The effectId is ' + (state ? 'supported' : 'unsupported'));
        if (state) {
            try {
                vibrator.startVibration({ // To use startVibration, you must configure the ohos.permission.VIBRATE permission.
                    type: 'preset',
                    effectId: 'haptic.clock.timer',
                    count: 1,
                }, {
                    usage: 'unknown'
                }, (error) => {
                    if(error) {
                        console.error('haptic.clock.timer vibrator error:'  + JSON.stringify(error));
                    } else {
                        console.log('haptic.clock.timer vibrator success');
                    }
                });
            } catch (error) {
                console.error('Exception in, error:' + JSON.stringify(error));
            }
        }
    })
} catch (error) {
    console.error('Exception in, error:' + JSON.stringify(error));
}
  ```

## vibrator.isSupportEffect<sup>10+</sup>

isSupportEffect(effectId: string): Promise&lt;boolean&gt;

Checks whether the passed effect ID is supported. This API uses a promise to return the result.

**System capability**: SystemCapability.Sensors.MiscDevice

**Parameters**

| Name  | Type  | Mandatory| Description        |
| -------- | ------ | ---- | ------------ |
| effectId | string | Yes  | Vibration effect ID.|

**Return value**

| Type                  | Description                                                     |
| ---------------------- | --------------------------------------------------------- |
| Promise&lt;boolean&gt; | Promise that returns the result. The value **true** means that the passed effect ID is supported, and **false** means the opposite. |

**Example**

  ```js
import vibrator from '@ohos.vibrator';
try {
    // Check whether 'haptic.clock.timer' is supported.
    vibrator.isSupportEffect('haptic.clock.timer').then((state) => {
        console.log('The effectId is ' + (state ? 'supported' : 'unsupported'));
        if (state) {
            try {
                vibrator.startVibration({
                    type: 'preset',
                    effectId: 'haptic.clock.timer',
                    count: 1,
                }, {
                    usage: 'unknown'
                }).then(()=>{
                    console.log('Promise returned to indicate a successful vibration');
                }).catch((error)=>{
                    console.error('Promise returned to indicate a failed vibration:' + JSON.stringify(error));
                });
            } catch (error) {
                console.error('exception in, error:' + JSON.stringify(error));
            }
        }
    }, (error) => {
        console.error('isSupportEffect failed, error:' + JSON.stringify(error));
    })
} catch (error) {
    console.error('Exception in, error:' + JSON.stringify(error));
}
  ```

W
wusongqing 已提交
433
## EffectId
Z
zengyawen 已提交
434

435
Describes the preset vibration effect ID.
Z
zengyawen 已提交
436

W
wusongqing 已提交
437 438
**System capability**: SystemCapability.Sensors.MiscDevice

439 440 441
| Name              | Value                  | Description                            |
| ------------------ | -------------------- | -------------------------------- |
| EFFECT_CLOCK_TIMER | "haptic.clock.timer" | Vibration effect of the vibrator when a user adjusts the timer.|
Z
zengyawen 已提交
442 443


W
wusongqing 已提交
444 445
## VibratorStopMode

G
Gloria 已提交
446
Enumerates the modes available to stop the vibration.
Z
zengyawen 已提交
447

W
wusongqing 已提交
448 449
**System capability**: SystemCapability.Sensors.MiscDevice

450 451 452 453
| Name                     | Value      | Description                          |
| ------------------------- | -------- | ------------------------------ |
| VIBRATOR_STOP_MODE_TIME   | "time"   | The vibration to stop is in **duration** mode.|
| VIBRATOR_STOP_MODE_PRESET | "preset" | The vibration to stop is in **EffectId** mode.|
G
Gloria 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471

## VibrateEffect<sup>9+</sup>

Describes the vibration effect.

**System capability**: SystemCapability.Sensors.MiscDevice

| Type                            | Description                          |
| -------------------------------- | ------------------------------ |
| [VibrateTime](#vibratetime9)     | Triggers vibration with the specified duration. This API uses a promise to return the result.|
| [VibratePreset](#vibratepreset9) | Vibration with a preset effect.|

## VibrateTime<sup>9+</sup>

Describes the vibration with the specified duration.

**System capability**: SystemCapability.Sensors.MiscDevice

472
| Name    | Value| Description                          |
G
Gloria 已提交
473 474 475 476 477 478 479 480 481 482
| -------- | ------ | ------------------------------ |
| type     | "time" | Vibration with the specified duration.|
| duration | -      | Vibration duration, in ms.        |

## VibratePreset<sup>9+</sup>

Describes the vibration with a preset effect.

**System capability**: SystemCapability.Sensors.MiscDevice

483
| Name    | Value      | Description                          |
G
Gloria 已提交
484 485 486 487 488 489 490 491 492 493 494
| -------- | -------- | ------------------------------ |
| type     | "preset" | Vibration with the specified effect.|
| effectId | -        | Preset vibration effect ID.            |
| count    | -        | Number of vibrations to repeat.              |

## VibrateAttribute<sup>9+</sup>

Describes the vibration attribute.

**System capability**: SystemCapability.Sensors.MiscDevice

495
| Name | Value| Description          |
G
Gloria 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
| ----- | ------ | -------------- |
| id    | 0      | Vibrator ID.    |
| usage | -      | Vibration scenario.|

## Usage<sup>9+</sup>

Enumerates the vibration scenarios.

**System capability**: SystemCapability.Sensors.MiscDevice

| Name            | Type  | Description                          |
| ---------------- | ------ | ------------------------------ |
| unknown          | string | Unknown scenario, with the lowest priority.|
| alarm            | string | Vibration for alarms.          |
| ring             | string | Vibration for incoming calls.          |
| notification     | string | Vibration for notifications.          |
| communication    | string | Vibration for communication.          |
| touch            | string | Touch vibration scenario.          |
| media            | string | Multimedia vibration scenario.        |
| physicalFeedback | string | Physical feedback vibration scenario.      |
| simulateReality  | string | Simulated reality vibration scenario.      |
G
Gloria 已提交
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

## vibrator.vibrate<sup>(deprecated)</sup>

vibrate(duration: number): Promise&lt;void&gt;

Triggers vibration with the specified duration. This API uses a promise to return the result.

This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9-1) instead.

**Required permissions**: ohos.permission.VIBRATE

**System capability**: SystemCapability.Sensors.MiscDevice

**Parameters**

| Name  | Type  | Mandatory| Description                  |
| -------- | ------ | ---- | ---------------------- |
| duration | number | Yes  | Vibration duration, in ms.|

**Return value**

| Type               | Description                                  |
| ------------------- | -------------------------------------- |
| Promise&lt;void&gt; | Promise that returns no value.|

**Example**

  ```js
545
vibrator.vibrate(1000).then(() => {
G
Gloria 已提交
546
    console.log('Promise returned to indicate a successful vibration.');
547
}, (error) => {
G
Gloria 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
    console.log('error.code' + error.code + 'error.message' + error.message);
});
  ```

## vibrator.vibrate<sup>(deprecated)</sup>

vibrate(duration: number, callback?: AsyncCallback&lt;void&gt;): void

Triggers vibration with the specified duration. This API uses an asynchronous callback to return the result.

This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9) instead.

**Required permissions**: ohos.permission.VIBRATE

**System capability**: SystemCapability.Sensors.MiscDevice

**Parameters**

| Name  | Type                     | Mandatory| Description                                                      |
| -------- | ------------------------- | ---- | ---------------------------------------------------------- |
| duration | number                    | Yes  | Vibration duration, in ms.                                    |
569
| callback | AsyncCallback&lt;void&gt; | No  | Callback used to return the result. If the vibration starts, **err** is **undefined**; otherwise, **err** is an error object.|
G
Gloria 已提交
570 571 572 573

**Example**

  ```js
574 575
vibrator.vibrate(1000, function (error) {
    if (error) {
G
Gloria 已提交
576
        console.log('error.code' + error.code + 'error.message' + error.message);
577
    } else {
G
Gloria 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
        console.log('Callback returned to indicate a successful vibration.');
    }
})
  ```


## vibrator.vibrate<sup>(deprecated)</sup>

vibrate(effectId: EffectId): Promise&lt;void&gt;

Triggers vibration with the specified effect. This API uses a promise to return the result.

This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9-1) instead.

**Required permissions**: ohos.permission.VIBRATE

**System capability**: SystemCapability.Sensors.MiscDevice

**Parameters**

| Name  | Type                 | Mandatory| Description              |
| -------- | --------------------- | ---- | ------------------ |
| effectId | [EffectId](#effectid) | Yes  | Preset vibration effect ID.|

**Return value**

| Type               | Description                                  |
| ------------------- | -------------------------------------- |
| Promise&lt;void&gt; | Promise that returns no value.|

**Example**

  ```js
611
vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(() => {
G
Gloria 已提交
612
    console.log('Promise returned to indicate a successful vibration.');
613
}, (error) => {
G
Gloria 已提交
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
    console.log('error.code' + error.code + 'error.message' + error.message);
});
  ```


## vibrator.vibrate<sup>(deprecated)</sup>

vibrate(effectId: EffectId, callback?: AsyncCallback&lt;void&gt;): void

Triggers vibration with the specified effect. This API uses an asynchronous callback to return the result.

This API is deprecated since API version 9. You are advised to use [vibrator.startVibration](#vibratorstartvibration9) instead.

**Required permissions**: ohos.permission.VIBRATE

**System capability**: SystemCapability.Sensors.MiscDevice

**Parameters**

| Name  | Type                     | Mandatory| Description                                                      |
| -------- | ------------------------- | ---- | ---------------------------------------------------------- |
| effectId | [EffectId](#effectid)     | Yes  | Preset vibration effect ID.                                        |
636
| callback | AsyncCallback&lt;void&gt; | No  | Callback used to return the result. If the vibration starts, **err** is **undefined**; otherwise, **err** is an error object.|
G
Gloria 已提交
637 638 639 640

**Example**

  ```js
641 642
vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function (error) {
    if (error) {
G
Gloria 已提交
643
        console.log('error.code' + error.code + 'error.message' + error.message);
644
    } else {
G
Gloria 已提交
645 646 647 648 649 650 651 652 653
        console.log('Callback returned to indicate a successful vibration.');
    }
})
  ```

## vibrator.stop<sup>(deprecated)</sup>

stop(stopMode: VibratorStopMode): Promise&lt;void&gt;

654
Stops vibration in the specified mode. This API uses a promise to return the result.
G
Gloria 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676

This API is deprecated since API version 9. You are advised to use [vibrator.stopVibration](#vibratorstopvibration9-1) instead.

**Required permissions**: ohos.permission.VIBRATE

**System capability**: SystemCapability.Sensors.MiscDevice

**Parameters**

| Name  | Type                                 | Mandatory| Description                    |
| -------- | ------------------------------------- | ---- | ------------------------ |
| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes  | Mode to stop the vibration.|

**Return value**

| Type               | Description                                  |
| ------------------- | -------------------------------------- |
| Promise&lt;void&gt; | Promise that returns no value.|

**Example**

  ```js
677 678 679 680 681 682 683 684 685 686
// Start vibration based on the specified effect ID.
vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function (error) {
    if (error) {
        console.log('error.code' + error.code + 'error.message' + error.message);
    } else {
        console.log('Callback returned to indicate a successful vibration.');
    }
})
// Stop vibration in VIBRATOR_STOP_MODE_PRESET mode.
vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => {
G
Gloria 已提交
687
    console.log('Promise returned to indicate a successful vibration.');
688
}, (error) => {
G
Gloria 已提交
689 690 691 692 693 694 695 696 697
    console.log('error.code' + error.code + 'error.message' + error.message);
});
  ```


## vibrator.stop<sup>(deprecated)</sup>

stop(stopMode: VibratorStopMode, callback?: AsyncCallback&lt;void&gt;): void

698
Stops vibration in the specified mode. This API uses an asynchronous callback to return the result.
G
Gloria 已提交
699 700 701 702 703 704 705 706 707 708 709 710

This API is deprecated since API version 9. You are advised to use [vibrator.stopVibration](#vibratorstopvibration9) instead.

**Required permissions**: ohos.permission.VIBRATE

**System capability**: SystemCapability.Sensors.MiscDevice

**Parameters**

| Name  | Type                                 | Mandatory| Description                                                        |
| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ |
| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes  | Mode to stop the vibration.                                    |
711
| callback | AsyncCallback&lt;void&gt;             | No  | Callback used to return the result. If the vibration stops, **err** is **undefined**; otherwise, **err** is an error object.|
G
Gloria 已提交
712 713 714 715

**Example**

  ```js
716 717 718 719 720 721 722 723 724 725 726
// Start vibration based on the specified effect ID.
vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function (error) {
    if (error) {
        console.log('error.code' + error.code + 'error.message' + error.message);
    } else {
        console.log('Callback returned to indicate a successful vibration.');
    }
})
// Stop vibration in VIBRATOR_STOP_MODE_PRESET mode.
vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, function (error) {
    if (error) {
G
Gloria 已提交
727
        console.log('error.code' + error.code + 'error.message' + error.message);
728
    } else {
G
Gloria 已提交
729 730 731 732
        console.log('Callback returned to indicate successful.');
    }
})
  ```