js-apis-vibrator.md 12.8 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
```

W
wusongqing 已提交
16 17 18 19
## vibrator.vibrate

vibrate(duration: number): Promise<void>

G
Gloria 已提交
20
Triggers vibration with the specified duration. 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 28 29 30

| Name  | Type  | Mandatory| Description                  |
| -------- | ------ | ---- | ---------------------- |
| duration | number | Yes  | Vibration duration, in ms.|
W
wusongqing 已提交
31

W
wusongqing 已提交
32
**Return value**
W
wusongqing 已提交
33

G
Gloria 已提交
34 35 36
| Type               | Description                                  |
| ------------------- | -------------------------------------- |
| Promise<void> | Promise that returns no value.|
W
wusongqing 已提交
37

W
wusongqing 已提交
38
**Example**
G
Gloria 已提交
39

W
wusongqing 已提交
40
  ```js
W
wusongqing 已提交
41 42 43 44 45 46 47
  vibrator.vibrate(1000).then(()=>{
      console.log("Promise returned to indicate a successful vibration.");
  }, (error)=>{
      console.log("error.code"+error.code+"error.message"+error.message);
  });
  ```

G
Gloria 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
## vibrator.vibrate<sup>9+</sup>

vibrate(effect: VibrateEffect, attribute: VibrateAttribute): Promise&lt;void&gt;

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          |
| --------- | -------------------------------------- | ---- | :------------- |
| 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**

```js
vibrator.vibrate({
    type: 'time',
    duration: 1000
}, {
   	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);
})
```
W
wusongqing 已提交
86 87 88 89

## vibrator.vibrate

vibrate(duration: number, callback?: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
90

G
Gloria 已提交
91
Triggers vibration with the specified duration. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
92

G
Gloria 已提交
93
**Required permissions**: ohos.permission.VIBRATE
W
wusongqing 已提交
94 95

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

W
wusongqing 已提交
97
**Parameters**
G
Gloria 已提交
98 99 100 101
| 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.|
W
wusongqing 已提交
102

W
wusongqing 已提交
103
**Example**
G
Gloria 已提交
104

W
wusongqing 已提交
105
  ```js
W
wusongqing 已提交
106 107
  vibrator.vibrate(1000,function(error){
      if(error){
G
Gloria 已提交
108
          console.log("error.code" + error.code + "error.message" + error.message);
W
wusongqing 已提交
109 110 111 112 113 114 115 116 117 118
      }else{
          console.log("Callback returned to indicate a successful vibration.");
      }
  })
  ```


## vibrator.vibrate

vibrate(effectId: EffectId): Promise&lt;void&gt;
Z
zengyawen 已提交
119

G
Gloria 已提交
120
Triggers vibration with the specified effect. This API uses a promise to return the result.
W
wusongqing 已提交
121

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

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

W
wusongqing 已提交
126
**Parameters**
G
Gloria 已提交
127 128 129
| Name  | Type                 | Mandatory| Description              |
| -------- | --------------------- | ---- | ------------------ |
| effectId | [EffectId](#effectid) | Yes  | Preset vibration effect ID.|
W
wusongqing 已提交
130

W
wusongqing 已提交
131
**Return value**
G
Gloria 已提交
132 133 134
| Type               | Description                                  |
| ------------------- | -------------------------------------- |
| Promise&lt;void&gt; | Promise that returns no value.|
W
wusongqing 已提交
135

W
wusongqing 已提交
136
**Example**
W
wusongqing 已提交
137
  ```js
W
wusongqing 已提交
138 139 140
  vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(()=>{
      console.log("Promise returned to indicate a successful vibration.");
  }, (error)=>{
G
Gloria 已提交
141
      console.log("error.code" + error.code + "error.message" + error.message);
W
wusongqing 已提交
142 143 144 145 146 147 148
  });
  ```


## vibrator.vibrate

vibrate(effectId: EffectId, callback?: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
149

G
Gloria 已提交
150
Triggers vibration with the specified effect. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
151

G
Gloria 已提交
152
**Required permissions**: ohos.permission.VIBRATE
W
wusongqing 已提交
153 154

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

W
wusongqing 已提交
156
**Parameters**
G
Gloria 已提交
157 158 159 160
| 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.|
W
wusongqing 已提交
161

W
wusongqing 已提交
162
**Example**
G
Gloria 已提交
163

W
wusongqing 已提交
164
  ```js
W
wusongqing 已提交
165 166
  vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function(error){
      if(error){
G
Gloria 已提交
167
          console.log("error.code" + error.code + "error.message" + error.message);
W
wusongqing 已提交
168 169 170 171 172 173
      }else{
          console.log("Callback returned to indicate a successful vibration.");
      }
  })
  ```

G
Gloria 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
## vibrator.vibrate<sup>9+</sup>

vibrate(effect: VibrateEffect, attribute: VibrateAttribute, callback: AsyncCallback&lt;void&gt;): void

Triggers vibration with the specified effect and attribute. 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                                                      |
| --------- | -------------------------------------- | ---- | :--------------------------------------------------------- |
| 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.|

**Example**

```js
vibrator.vibrate({
    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.");
    }
});
```
W
wusongqing 已提交
209 210 211 212 213

## vibrator.stop

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

G
Gloria 已提交
214
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 已提交
215

G
Gloria 已提交
216
**Required permissions**: ohos.permission.VIBRATE
W
wusongqing 已提交
217 218

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

W
wusongqing 已提交
220
**Parameters**
G
Gloria 已提交
221 222 223
| Name  | Type                                 | Mandatory| Description                    |
| -------- | ------------------------------------- | ---- | ------------------------ |
| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes  | Mode to stop the vibration.|
W
wusongqing 已提交
224

W
wusongqing 已提交
225
**Return value**
G
Gloria 已提交
226 227 228 229

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

W
wusongqing 已提交
231
**Example**
G
Gloria 已提交
232

W
wusongqing 已提交
233
  ```js
W
wusongqing 已提交
234 235 236
  vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(()=>{
      console.log("Promise returned to indicate a successful vibration.");
  }, (error)=>{
G
Gloria 已提交
237
      console.log("error.code" + error.code + "error.message" + error.message);
W
wusongqing 已提交
238 239 240 241 242 243 244 245
  });
  ```


## vibrator.stop

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

G
Gloria 已提交
246
Stops the vibration with the specified **stopMode**. This API uses an asynchronous callback 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 已提交
247

G
Gloria 已提交
248
**Required permissions**: ohos.permission.VIBRATE
W
wusongqing 已提交
249 250

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

W
wusongqing 已提交
252
**Parameters**
G
Gloria 已提交
253 254 255 256
| 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.|
W
wusongqing 已提交
257

W
wusongqing 已提交
258
**Example**
G
Gloria 已提交
259

W
wusongqing 已提交
260
  ```js
W
wusongqing 已提交
261 262
  vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, function(error){
      if(error){
G
Gloria 已提交
263
          console.log("error.code" + error.code + "error.message" + error.message);
W
wusongqing 已提交
264
      }else{
W
wusongqing 已提交
265
          console.log("Callback returned to indicate a successful stop.");
W
wusongqing 已提交
266 267 268 269 270 271
      }
  })
  ```


## EffectId
Z
zengyawen 已提交
272

G
Gloria 已提交
273
Describes the vibration effect ID.
Z
zengyawen 已提交
274

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

G
Gloria 已提交
277 278 279
| Name              | Default Value              | Description              |
| ------------------ | -------------------- | ------------------ |
| EFFECT_CLOCK_TIMER | "haptic.clock.timer" | Preset vibration effect ID.|
Z
zengyawen 已提交
280 281


W
wusongqing 已提交
282 283
## VibratorStopMode

G
Gloria 已提交
284
Enumerates the modes available to stop the vibration.
Z
zengyawen 已提交
285

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

G
Gloria 已提交
288 289
| Name                     | Default Value  | Description                                                        |
| ------------------------- | -------- | ------------------------------------------------------------ |
W
wusongqing 已提交
290 291
| 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 已提交
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

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