js-apis-vibrator.md 16.6 KB
Newer Older
W
wusongqing 已提交
1
# Vibrator
W
wusongqing 已提交
2

W
wusongqing 已提交
3
The **Vibrator** module provides APIs for triggering 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

G
Gloria 已提交
20
Triggers vibration with the specified effect and attribute. This API uses a promise 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 32
| Name   | Type                                  | Mandatory| Description                                                      |
| --------- | -------------------------------------- | ---- | :--------------------------------------------------------- |
| effect    | [VibrateEffect](#vibrateeffect9)       | Yes  | Vibration effect.                                            |
| attribute | [VibrateAttribute](#vibrateattribute9) | Yes  | Vibration attribute.                                            |
| callback  | AsyncCallback&lt;void&gt;              | Yes  | Callback used to the result. If the vibration starts, **err** is **undefined**. Otherwise, **err** is an error object.|
W
wusongqing 已提交
33

W
wusongqing 已提交
34
**Example**
G
Gloria 已提交
35

G
Gloria 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
```js
try {
    vibrator.startVibration({
    type:'time',
    duration:1000,
    },{
        id:0,
        usage: 'alarm'
    }, (error)=>{
        if(error){
            console.log('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
        }else{
            console.log('Callback returned to indicate a successful vibration.');
        }
    });
} catch(err) {
      console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
}
```
W
wusongqing 已提交
55

G
Gloria 已提交
56
## vibrator.startVibration<sup>9+</sup>
G
Gloria 已提交
57

G
Gloria 已提交
58
startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise&lt;void&gt;
G
Gloria 已提交
59 60 61 62 63 64 65 66 67 68

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

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

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

**Parameters**

| Name   | Type                                  | Mandatory| Description          |
G
Gloria 已提交
69
| --------- | -------------------------------------- | ---- | -------------- |
G
Gloria 已提交
70 71 72 73 74 75 76 77 78 79 80
| 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.|

**Example**

G
Gloria 已提交
81 82 83
  ```js
try {
    vibrator.startVibration({
G
Gloria 已提交
84 85
    type: 'time',
    duration: 1000
G
Gloria 已提交
86 87 88 89 90 91 92 93 94 95 96
    }, {
        id: 0,
        usage: 'alarm'
    }).then(()=>{
        console.log('Promise returned to indicate a successful vibration');
    }).catch((error)=>{
        console.log('error.code' + error.code + 'error.message' + error.message);
    })
} catch(err) {
      console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
}
W
wusongqing 已提交
97 98
  ```

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

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

G
Gloria 已提交
103
Stops the vibration with the specified **stopMode**. This API uses a promise to return the result. If the specified **stopMode** is different from the mode used to trigger the vibration, this API fails to be called.
W
wusongqing 已提交
104

G
Gloria 已提交
105
**Required permissions**: ohos.permission.VIBRATE
W
wusongqing 已提交
106 107

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

W
wusongqing 已提交
109
**Parameters**
W
wusongqing 已提交
110

G
Gloria 已提交
111 112 113 114
| Name  | Type                                 | Mandatory| Description                                                        |
| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ |
| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes  | Mode to stop the vibration.                                    |
| callback | AsyncCallback&lt;void&gt;             | Yes  | Callback used to the result. If the vibration stops, **err** is **undefined**. Otherwise, **err** is an error object.|
W
wusongqing 已提交
115

W
wusongqing 已提交
116
**Example**
G
Gloria 已提交
117

W
wusongqing 已提交
118
  ```js
G
Gloria 已提交
119 120 121 122 123 124 125 126 127 128 129
try {
    vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, function(error){
        if(error){
            console.log('error.code' + error.code + 'error.message' + error.message);
        }else{
            console.log('Callback returned to indicate successful.');
        }
    })
} catch(err) {
      console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
}
W
wusongqing 已提交
130 131
  ```

G
Gloria 已提交
132
## vibrator.stopVibration<sup>9+</sup>
G
Gloria 已提交
133

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

G
Gloria 已提交
136
Stops the vibration with the specified **stopMode**. This API uses a promise to return the result. If the specified **stopMode** is different from the mode used to trigger the vibration, this API fails to be called.
W
wusongqing 已提交
137

G
Gloria 已提交
138
**Required permissions**: ohos.permission.VIBRATE
W
wusongqing 已提交
139 140

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

W
wusongqing 已提交
142
**Parameters**
G
Gloria 已提交
143

G
Gloria 已提交
144 145 146
| Name  | Type                                 | Mandatory| Description                    |
| -------- | ------------------------------------- | ---- | ------------------------ |
| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes  | Mode to stop the vibration.|
W
wusongqing 已提交
147

W
wusongqing 已提交
148
**Return value**
G
Gloria 已提交
149 150 151 152

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

W
wusongqing 已提交
154
**Example**
G
Gloria 已提交
155

W
wusongqing 已提交
156
  ```js
G
Gloria 已提交
157 158 159 160 161 162 163 164 165
try {
    vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(()=>{
    console.log('Promise returned to indicate a successful vibration.');
    }, (error)=>{
        console.log('error.code' + error.code + 'error.message' + error.message);
    });
} catch(err) {
      console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
}
W
wusongqing 已提交
166 167 168
  ```

## EffectId
Z
zengyawen 已提交
169

G
Gloria 已提交
170
Describes the vibration effect ID.
Z
zengyawen 已提交
171

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

G
Gloria 已提交
174 175 176
| Name              | Default Value              | Description              |
| ------------------ | -------------------- | ------------------ |
| EFFECT_CLOCK_TIMER | "haptic.clock.timer" | Preset vibration effect ID.|
Z
zengyawen 已提交
177 178


W
wusongqing 已提交
179 180
## VibratorStopMode

G
Gloria 已提交
181
Enumerates the modes available to stop the vibration.
Z
zengyawen 已提交
182

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

G
Gloria 已提交
185 186
| Name                     | Default Value  | Description                                                        |
| ------------------------- | -------- | ------------------------------------------------------------ |
W
wusongqing 已提交
187 188
| VIBRATOR_STOP_MODE_TIME   | "time"   | The vibration to stop is in **duration** mode. This vibration is triggered with the parameter **duration** of the **number** type.|
| VIBRATOR_STOP_MODE_PRESET | "preset" | The vibration to stop is in **EffectId** mode. This vibration is triggered with the parameter **effectId** of the **EffectId** type.|
G
Gloria 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 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

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

| Name    | Default Value| Description                          |
| -------- | ------ | ------------------------------ |
| 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

| Name    | Default Value  | Description                          |
| -------- | -------- | ------------------------------ |
| 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

| Name | Default Value| Description          |
| ----- | ------ | -------------- |
| 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 已提交
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 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449

## 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
vibrator.vibrate(1000).then(()=>{
    console.log('Promise returned to indicate a successful vibration.');
}, (error)=>{
    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.                                    |
| callback | AsyncCallback&lt;void&gt; | No  | Callback used to the result. If the vibration starts, **err** is **undefined**. Otherwise, **err** is an error object.|

**Example**

  ```js
vibrator.vibrate(1000,function(error){
    if(error){
        console.log('error.code' + error.code + 'error.message' + error.message);
    }else{
        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
vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(()=>{
    console.log('Promise returned to indicate a successful vibration.');
}, (error)=>{
    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.                                        |
| callback | AsyncCallback&lt;void&gt; | No  | Callback used to the result. If the vibration starts, **err** is **undefined**. Otherwise, **err** is an error object.|

**Example**

  ```js
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.');
    }
})
  ```

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

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

Stops the vibration with the specified **stopMode**. This API uses a promise to return the result. If the specified **stopMode** is different from the mode used to trigger the vibration, this API fails to be called.

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
vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(()=>{
    console.log('Promise returned to indicate a successful vibration.');
}, (error)=>{
    console.log('error.code' + error.code + 'error.message' + error.message);
});
  ```


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

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

Stops the vibration with the specified **stopMode**. This API uses a promise to return the result. If the specified **stopMode** is different from the mode used to trigger the vibration, this API fails to be called.

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.                                    |
| callback | AsyncCallback&lt;void&gt;             | No  | Callback used to the result. If the vibration stops, **err** is **undefined**. Otherwise, **err** is an error object.|

**Example**

  ```js
vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, function(error){
    if(error){
        console.log('error.code' + error.code + 'error.message' + error.message);
    }else{
        console.log('Callback returned to indicate successful.');
    }
})
  ```