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


W
wusongqing 已提交
3
# Vibrator
Z
zengyawen 已提交
4

W
wusongqing 已提交
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 16
```


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

vibrate(duration: number): Promise<void>

W
wusongqing 已提交
21 22 23
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 已提交
24

W
wusongqing 已提交
25
**System capability**: SystemCapability.Sensors.MiscDevice
Z
zengyawen 已提交
26

W
wusongqing 已提交
27

W
wusongqing 已提交
28
**Parameters**
W
wusongqing 已提交
29 30 31
| Name     | Type    | Mandatory  | Description          |
| -------- | ------ | ---- | ------------ |
| duration | number | Yes   | Vibration duration.|
W
wusongqing 已提交
32

W
wusongqing 已提交
33
**Return value**
W
wusongqing 已提交
34 35 36
| Type                 | Description         |
| ------------------- | ----------- |
| Promise<void> | Promise used to indicate whether the vibration is triggered successfully.|
W
wusongqing 已提交
37 38


W
wusongqing 已提交
39
**Example**
W
wusongqing 已提交
40
  ```js
W
wusongqing 已提交
41 42 43 44 45 46 47 48 49 50 51
  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<void>): void
Z
zengyawen 已提交
52

W
wusongqing 已提交
53 54 55 56 57
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 已提交
58

W
wusongqing 已提交
59
**Parameters**
W
wusongqing 已提交
60 61 62 63
| Name     | Type                       | Mandatory  | Description                     |
| -------- | ------------------------- | ---- | ----------------------- |
| duration | number                    | Yes   | Vibration duration.           |
| callback | AsyncCallback<void> | No   | Callback used to indicate whether the vibration is triggered successfully.|
W
wusongqing 已提交
64

W
wusongqing 已提交
65
**Example**
W
wusongqing 已提交
66
  ```js
W
wusongqing 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79
  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<void>
Z
zengyawen 已提交
80

W
wusongqing 已提交
81 82 83 84 85
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 已提交
86

W
wusongqing 已提交
87
**Parameters**
W
wusongqing 已提交
88 89 90
| Name     | Type                   | Mandatory  | Description           |
| -------- | --------------------- | ---- | ------------- |
| effectId | [EffectId](#effectid) | Yes   | Vibration effect. |
W
wusongqing 已提交
91

W
wusongqing 已提交
92
**Return value**
W
wusongqing 已提交
93 94 95
| Type                 | Description         |
| ------------------- | ----------- |
| Promise<void> | Promise used to indicate whether the vibration is triggered successfully.|
W
wusongqing 已提交
96

W
wusongqing 已提交
97
**Example**
W
wusongqing 已提交
98
  ```js
W
wusongqing 已提交
99 100 101 102 103 104 105 106 107 108 109
  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<void>): void
Z
zengyawen 已提交
110

W
wusongqing 已提交
111 112 113 114 115
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 已提交
116

W
wusongqing 已提交
117
**Parameters**
W
wusongqing 已提交
118 119 120 121
| Name     | Type                       | Mandatory  | Description                     |
| -------- | ------------------------- | ---- | ----------------------- |
| effectId | [EffectId](#effectid)     | Yes   | Vibration effect.          |
| callback | AsyncCallback<void> | No   | Callback used to indicate whether the vibration is triggered successfully.|
W
wusongqing 已提交
122

W
wusongqing 已提交
123
**Example**
W
wusongqing 已提交
124
  ```js
W
wusongqing 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138
  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<void>

W
wusongqing 已提交
139 140 141 142 143
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 已提交
144

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

W
wusongqing 已提交
150
**Return value**
W
wusongqing 已提交
151 152 153
| Type                 | Description         |
| ------------------- | ----------- |
| Promise<void> | Promise used to indicate whether the vibration is stopped successfully.|
W
wusongqing 已提交
154

W
wusongqing 已提交
155
**Example**
W
wusongqing 已提交
156
  ```js
W
wusongqing 已提交
157 158 159 160 161 162 163 164 165 166 167 168
  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<void>): void;

W
wusongqing 已提交
169 170 171 172 173
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 已提交
174

W
wusongqing 已提交
175
**Parameters**
W
wusongqing 已提交
176 177 178 179
| Name     | Type                                   | Mandatory  | Description                     |
| -------- | ------------------------------------- | ---- | ----------------------- |
| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes   | Vibration mode to stop.        |
| callback | AsyncCallback<void>             | No   | Callback used to indicate whether the vibration is stopped successfully.|
W
wusongqing 已提交
180

W
wusongqing 已提交
181
**Example**
W
wusongqing 已提交
182
  ```js
W
wusongqing 已提交
183 184 185 186
  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 已提交
187
          console.log("Callback returned to indicate a successful stop.");
W
wusongqing 已提交
188 189 190 191 192 193
      }
  })
  ```


## EffectId
Z
zengyawen 已提交
194 195 196

Describes the vibration effect.

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

W
wusongqing 已提交
199 200 201
| Name                | Default Value                 | Description             |
| ------------------ | -------------------- | --------------- |
| EFFECT_CLOCK_TIMER | "haptic.clock.timer" | Vibration effect of the vibrator when a user adjusts the timer.|
Z
zengyawen 已提交
202 203


W
wusongqing 已提交
204 205 206
## VibratorStopMode

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

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

W
wusongqing 已提交
210 211 212 213
| 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.|