js-apis-vibrator.md 27.6 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
    }, (error) => {
        if (error) {
G
Gloria 已提交
55
            console.error('Vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
56
            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
    // Start vibration at a fixed duration.
    vibrator.startVibration({
        type: 'time',
        duration: 1000,
    }, {
        id: 0,
        usage: 'alarm'
    }, (error) => {
        if (error) {
G
Gloria 已提交
148
            console.error('Vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
149 150 151 152 153 154 155 156 157 158 159 160
            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
## 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) {
G
Gloria 已提交
256
            console.error('Vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
G
Gloria 已提交
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
            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.                                            |
G
Gloria 已提交
341
| 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.|
G
Gloria 已提交
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

**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                                                     |
| ---------------------- | --------------------------------------------------------- |
G
Gloria 已提交
398
| 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.|
G
Gloria 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421

**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) {
G
Gloria 已提交
422
                console.error('Exception in, error:' + JSON.stringify(error));
G
Gloria 已提交
423 424 425 426 427 428 429 430 431 432
            }
        }
    }, (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

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

Describes the vibration effect.

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

| Type                            | Description                          |
| -------------------------------- | ------------------------------ |
G
Gloria 已提交
463
| [VibrateTime](#vibratetime9)     | Vibration with the specified duration.|
G
Gloria 已提交
464
| [VibratePreset](#vibratepreset9) | Vibration with a preset effect.|
G
Gloria 已提交
465
| [VibrateFromFile<sup>10+</sup>](#vibratefromfile10) | Vibration according to a custom vibration configuration file.|
G
Gloria 已提交
466 467 468 469 470 471 472

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

Describes the vibration with the specified duration.

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

G
Gloria 已提交
473 474 475 476
| Name    | Type   | Mandatory| Description                          |
| -------- | ------ | ----- | ------------------------------ |
| type     | string |  Yes  | The value **time** means vibration with the specified duration.|
| duration | number |  Yes  | Vibration duration, in ms.        |
G
Gloria 已提交
477 478 479 480 481 482 483

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

Describes the vibration with a preset effect.

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

G
Gloria 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
| Name    | Type     | Mandatory| Description                          |
| -------- | -------- | ---- |------------------------------ |
| type     | string   |  Yes | The value **preset** means vibration with the specified effect.|
| effectId | string   |  Yes | Preset vibration effect ID.            |
| count    | number   |  Yes | Number of vibrations to repeat.              |

## VibrateFromFile<sup>10+</sup>

Describes the custom vibration type, which is supported only by certain devices.

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

| Name    | Type      | Mandatory| Description                          |
| -------- | --------  | ---- | ------------------------------ |
| type     | string    |  Yes | The value **file** means vibration according to a vibration configuration file.|
| hapticFd | [HapticFileDescriptor](#hapticfiledescriptor10) | Yes| File descriptor (FD) of the vibration configuration file.|

## HapticFileDescriptor<sup>10+</sup>

Describes the FD of a custom vibration configuration file. Ensure that the file is available, and the parameters in it can be obtained from the sandbox path through the [file management API](js-apis-file-fs.md#fsopen) or from the HAP resource through the [resource management API](js-apis-resource-manager.md#getrawfd9). The use case is as follows: The system triggers vibration according to the sequence set in a configuration file, based on the specified offset and length. For details about the storage format of the vibration sequence, see [Custom Vibration Format](../../device/vibrator-guidelines.md#custom-vibration-format).

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

| Name    | Type     |  Mandatory | Description                          |
| -------- | -------- |--------| ------------------------------|
| fd       | number   |  Yes   | FD of the custom vibration configuration file.               |
| offset   | number   |  No   | Offset from the start position of the file, in bytes. The default value is the start position of the file, and the value cannot exceed the valid range of the file.|
| length   | number   |  No   | Resource length, in bytes. The default value is the length from the offset position to the end of the file, and the value cannot exceed the valid range of the file.|
G
Gloria 已提交
512 513 514 515 516 517 518

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

Describes the vibration attribute.

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

G
Gloria 已提交
519 520 521 522
| Name | Type| Mandatory| Description          |
| ----- | ------ | ---- | -------------- |
| id    | number      |  No| Vibrator ID. The default value is 0.    |
| usage | [Usage](#usage9)      | Yes| Vibration scenario.|
G
Gloria 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540

## 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 已提交
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568

## 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
569
vibrator.vibrate(1000).then(() => {
G
Gloria 已提交
570
    console.log('Promise returned to indicate a successful vibration.');
571
}, (error) => {
G
Gloria 已提交
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
    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.                                    |
593
| 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 已提交
594 595 596 597

**Example**

  ```js
598 599
vibrator.vibrate(1000, function (error) {
    if (error) {
G
Gloria 已提交
600
        console.log('error.code' + error.code + 'error.message' + error.message);
601
    } else {
G
Gloria 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
        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
635
vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(() => {
G
Gloria 已提交
636
    console.log('Promise returned to indicate a successful vibration.');
637
}, (error) => {
G
Gloria 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
    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.                                        |
660
| 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 已提交
661 662 663 664

**Example**

  ```js
665 666
vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function (error) {
    if (error) {
G
Gloria 已提交
667
        console.log('error.code' + error.code + 'error.message' + error.message);
668
    } else {
G
Gloria 已提交
669 670 671 672 673 674 675 676 677
        console.log('Callback returned to indicate a successful vibration.');
    }
})
  ```

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

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

678
Stops vibration in the specified mode. This API uses a promise to return the result.
G
Gloria 已提交
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700

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
701 702 703 704 705 706 707 708 709 710
// 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 已提交
711
    console.log('Promise returned to indicate a successful vibration.');
712
}, (error) => {
G
Gloria 已提交
713 714 715 716 717 718 719 720 721
    console.log('error.code' + error.code + 'error.message' + error.message);
});
  ```


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

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

722
Stops vibration in the specified mode. This API uses an asynchronous callback to return the result.
G
Gloria 已提交
723 724 725 726 727 728 729 730 731 732 733 734

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.                                    |
735
| 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 已提交
736 737 738 739

**Example**

  ```js
740 741 742 743 744 745 746 747 748 749 750
// 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 已提交
751
        console.log('error.code' + error.code + 'error.message' + error.message);
752
    } else {
G
Gloria 已提交
753 754 755 756
        console.log('Callback returned to indicate successful.');
    }
})
  ```