js-apis-vibrator.md 18.4 KB
Newer Older
W
wusongqing 已提交
1
# 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 45 46
```js
try {
    vibrator.startVibration({
47 48 49 50
        type: 'time',
        duration: 1000,
    }, {
        id: 0,
G
Gloria 已提交
51
        usage: 'alarm'
52 53 54 55
    }, (error) => {
        if (error) {
            console.error('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
            return;
G
Gloria 已提交
56
        }
57
        console.log('Callback returned to indicate a successful vibration.');
G
Gloria 已提交
58
    });
59 60
} catch (err) {
    console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
G
Gloria 已提交
61 62
}
```
W
wusongqing 已提交
63

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

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

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

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

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

**Parameters**

| Name   | Type                                  | Mandatory| Description          |
G
Gloria 已提交
77
| --------- | -------------------------------------- | ---- | -------------- |
G
Gloria 已提交
78 79 80 81 82 83 84 85 86
| 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 已提交
87 88 89 90
**Error codes**

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

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

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

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

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

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

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

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

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

W
wusongqing 已提交
125
**Parameters**
W
wusongqing 已提交
126

G
Gloria 已提交
127 128 129
| Name  | Type                                 | Mandatory| Description                                                        |
| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ |
| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes  | Mode to stop the vibration.                                    |
130
| 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 已提交
131

W
wusongqing 已提交
132
**Example**
G
Gloria 已提交
133

W
wusongqing 已提交
134
  ```js
G
Gloria 已提交
135
try {
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    // 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 已提交
158
            console.log('error.code' + error.code + 'error.message' + error.message);
159
            return;
G
Gloria 已提交
160
        }
161
        console.log('Callback returned to indicate successful.');
G
Gloria 已提交
162
    })
163 164
} catch (err) {
    console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
G
Gloria 已提交
165
}
W
wusongqing 已提交
166 167
  ```

G
Gloria 已提交
168
## vibrator.stopVibration<sup>9+</sup>
G
Gloria 已提交
169

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

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

G
Gloria 已提交
174
**Required permissions**: ohos.permission.VIBRATE
W
wusongqing 已提交
175 176

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

W
wusongqing 已提交
178
**Parameters**
G
Gloria 已提交
179

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

W
wusongqing 已提交
184
**Return value**
G
Gloria 已提交
185 186 187 188

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

W
wusongqing 已提交
190
**Example**
G
Gloria 已提交
191

W
wusongqing 已提交
192
  ```js
G
Gloria 已提交
193
try {
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    // 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 已提交
215 216
        console.log('error.code' + error.code + 'error.message' + error.message);
    });
217 218
} catch (err) {
    console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
G
Gloria 已提交
219
}
W
wusongqing 已提交
220 221 222
  ```

## EffectId
Z
zengyawen 已提交
223

224
Describes the preset vibration effect ID.
Z
zengyawen 已提交
225

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

228 229 230
| Name              | Value                  | Description                            |
| ------------------ | -------------------- | -------------------------------- |
| EFFECT_CLOCK_TIMER | "haptic.clock.timer" | Vibration effect of the vibrator when a user adjusts the timer.|
Z
zengyawen 已提交
231 232


W
wusongqing 已提交
233 234
## VibratorStopMode

G
Gloria 已提交
235
Enumerates the modes available to stop the vibration.
Z
zengyawen 已提交
236

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

239 240 241 242
| 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 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

## 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

261
| Name    | Value| Description                          |
G
Gloria 已提交
262 263 264 265 266 267 268 269 270 271
| -------- | ------ | ------------------------------ |
| 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

272
| Name    | Value      | Description                          |
G
Gloria 已提交
273 274 275 276 277 278 279 280 281 282 283
| -------- | -------- | ------------------------------ |
| 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

284
| Name | Value| Description          |
G
Gloria 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
| ----- | ------ | -------------- |
| 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 已提交
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

## 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
334
vibrator.vibrate(1000).then(() => {
G
Gloria 已提交
335
    console.log('Promise returned to indicate a successful vibration.');
336
}, (error) => {
G
Gloria 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    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.                                    |
358
| 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 已提交
359 360 361 362

**Example**

  ```js
363 364
vibrator.vibrate(1000, function (error) {
    if (error) {
G
Gloria 已提交
365
        console.log('error.code' + error.code + 'error.message' + error.message);
366
    } else {
G
Gloria 已提交
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
        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
400
vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(() => {
G
Gloria 已提交
401
    console.log('Promise returned to indicate a successful vibration.');
402
}, (error) => {
G
Gloria 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
    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.                                        |
425
| 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 已提交
426 427 428 429

**Example**

  ```js
430 431
vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function (error) {
    if (error) {
G
Gloria 已提交
432
        console.log('error.code' + error.code + 'error.message' + error.message);
433
    } else {
G
Gloria 已提交
434 435 436 437 438 439 440 441 442
        console.log('Callback returned to indicate a successful vibration.');
    }
})
  ```

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

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

443
Stops vibration in the specified mode. This API uses a promise to return the result.
G
Gloria 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465

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
466 467 468 469 470 471 472 473 474 475
// 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 已提交
476
    console.log('Promise returned to indicate a successful vibration.');
477
}, (error) => {
G
Gloria 已提交
478 479 480 481 482 483 484 485 486
    console.log('error.code' + error.code + 'error.message' + error.message);
});
  ```


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

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

487
Stops vibration in the specified mode. This API uses an asynchronous callback to return the result.
G
Gloria 已提交
488 489 490 491 492 493 494 495 496 497 498 499

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.                                    |
500
| 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 已提交
501 502 503 504

**Example**

  ```js
505 506 507 508 509 510 511 512 513 514 515
// 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 已提交
516
        console.log('error.code' + error.code + 'error.message' + error.message);
517
    } else {
G
Gloria 已提交
518 519 520 521
        console.log('Callback returned to indicate successful.');
    }
})
  ```