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

3
> **NOTE**<br>
W
wusongqing 已提交
4
> 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 已提交
5

W
wusongqing 已提交
6 7

## Modules to Import
Z
zengyawen 已提交
8 9

```
W
wusongqing 已提交
10
import vibrator from '@ohos.vibrator';
Z
zengyawen 已提交
11 12 13
```


W
wusongqing 已提交
14 15 16 17
## vibrator.vibrate

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

W
wusongqing 已提交
18 19 20
Triggers vibration with a specific duration. This API uses a promise to return the execution result.

**Required permissions**: ohos.permission.VIBRATE (a system permission)
Z
zengyawen 已提交
21

W
wusongqing 已提交
22
**System capability**: SystemCapability.Sensors.MiscDevice
Z
zengyawen 已提交
23

W
wusongqing 已提交
24

W
wusongqing 已提交
25
**Parameters**
26 27 28
 | Name    | Type   | Mandatory | Description         |
 | -------- | ------ | ---- | ------------ |
 | duration | number | Yes  | Vibration duration. |
W
wusongqing 已提交
29

W
wusongqing 已提交
30
**Return value**
31 32 33
 | Type                | Description        |
 | ------------------- | ----------- |
 | Promise&lt;void&gt; | Promise used to indicate whether the vibration is triggered successfully. |
W
wusongqing 已提交
34 35


W
wusongqing 已提交
36
**Example**
W
wusongqing 已提交
37 38 39 40 41 42 43 44 45 46 47 48
  ```
  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

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

W
wusongqing 已提交
50 51 52 53 54
Triggers vibration with a specific duration. This API uses an asynchronous callback to return the execution result.

**Required permissions**: ohos.permission.VIBRATE (a system permission)

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

W
wusongqing 已提交
56
**Parameters**
57 58 59 60
 | Name    | Type                      | Mandatory | Description                    |
 | -------- | ------------------------- | ---- | ----------------------- |
 | duration | number                   | Yes  | Vibration duration.          |
 | callback | AsyncCallback&lt;void&gt; | No  | Callback used to indicate whether the vibration is triggered successfully. |
W
wusongqing 已提交
61

W
wusongqing 已提交
62
**Example**
W
wusongqing 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76
  ```
  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

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

W
wusongqing 已提交
78 79 80 81 82
Triggers vibration with a specific effect. This API uses a promise to return the execution result.

**Required permissions**: ohos.permission.VIBRATE (a system permission)

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

W
wusongqing 已提交
84
**Parameters**
85 86 87
 | Name    | Type                  | Mandatory | Description          |
 | -------- | --------------------- | ---- | ------------- |
 | effectId | [EffectId](#effectid) | Yes  | String that indicates the vibration effect. |
W
wusongqing 已提交
88

W
wusongqing 已提交
89
**Return value**
90 91 92
 | Type                | Description        |
 | ------------------- | ----------- |
 | Promise&lt;void&gt; | Promise used to indicate whether the vibration is triggered successfully. |
W
wusongqing 已提交
93

W
wusongqing 已提交
94
**Example**
W
wusongqing 已提交
95 96 97 98 99 100 101 102 103 104 105 106
  ```
  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

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

W
wusongqing 已提交
108 109 110 111 112
Triggers vibration with a specific effect. This API uses an asynchronous callback to return the execution result.

**Required permissions**: ohos.permission.VIBRATE (a system permission)

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

W
wusongqing 已提交
114
**Parameters**
115 116 117 118
 | Name    | Type                      | Mandatory | Description                    |
 | -------- | ------------------------- | ---- | ----------------------- |
 | effectId | [EffectId](#effectid)    | Yes  | String that indicates the vibration effect.         |
 | callback | AsyncCallback&lt;void&gt; | No  | Callback used to indicate whether the vibration is triggered successfully. |
W
wusongqing 已提交
119

W
wusongqing 已提交
120
**Example**
W
wusongqing 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  ```
  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

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

W
wusongqing 已提交
136 137 138 139 140
Stops the vibration based on the specified **stopMode**. This API uses a promise to return the execution result. If the specified **stopMode** is different from the mode used to trigger the vibration, this API fails to be called.

**Required permissions**: ohos.permission.VIBRATE (a system permission)

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

W
wusongqing 已提交
142
**Parameters**
143 144 145
 | Name     | Type                                  | Mandatory | Description |
 | -------- | ------------------------------------- | ---- | --------------- |
 | stopMode | [VibratorStopMode](#vibratorstopmode) | Yes  | Vibration mode to stop. |
W
wusongqing 已提交
146

W
wusongqing 已提交
147
**Return value**
148 149 150
 | Type                | Description |
 | ------------------- | ----------- |
 | Promise&lt;void&gt; | Promise used to indicate whether the vibration is stopped successfully. |
W
wusongqing 已提交
151

W
wusongqing 已提交
152
**Example**
W
wusongqing 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165
  ```
  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

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

W
wusongqing 已提交
166 167 168 169 170
Stops the vibration based on the specified **stopMode**. This API uses an asynchronous callback to return the execution result. If the specified **stopMode** is different from the mode used to trigger the vibration, this API fails to be called.

**Required permissions**: ohos.permission.VIBRATE (a system permission)

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

W
wusongqing 已提交
172
**Parameters**
173 174 175 176
 | Name | Type | Mandatory | Description |
 | -------- | ------------------------------------- | ---- | ----------------------- |
 | stopMode | [VibratorStopMode](#vibratorstopmode) | Yes | Vibration mode to stop. |
 | callback | AsyncCallback&lt;void&gt; | No | Callback used to indicate whether the vibration is stopped successfully. |
W
wusongqing 已提交
177

W
wusongqing 已提交
178
**Example**
W
wusongqing 已提交
179 180 181 182 183
  ```
  vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, function(error){
      if(error){
          console.log("error.code"+error.code+"error.message"+error.message);
      }else{
W
wusongqing 已提交
184
          console.log("Callback returned to indicate a successful stop.");
W
wusongqing 已提交
185 186 187 188 189 190
      }
  })
  ```


## EffectId
Z
zengyawen 已提交
191 192 193

Describes the vibration effect.

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

196 197 198
 | Name               | Default Value        | Description                                                     |
 | ------------------ | -------------------- | --------------------------------------------------------------- |
 | EFFECT_CLOCK_TIMER | "haptic.clock.timer" | Vibration effect of the vibrator when a user adjusts the timer. |
Z
zengyawen 已提交
199 200


W
wusongqing 已提交
201 202 203
## VibratorStopMode

Describes the vibration mode to stop.
Z
zengyawen 已提交
204

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

207 208 209 210
 | Name | Default Value | Description |
 | ------------------------- | -------- | ---------------------------------------- |
 | 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. |