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

W
wusongqing 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:**
> 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<void>

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 26 27 28
**Parameters**
| Name     | Type    | Mandatory  | Description          |
| -------- | ------ | ---- | ------------ |
| duration | number | Yes   | Vibration duration.|
W
wusongqing 已提交
29

W
wusongqing 已提交
30 31 32 33
**Return value**
| Type                 | Description         |
| ------------------- | ----------- |
| Promise<void> | 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<void>): 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 57 58 59 60
**Parameters**
| 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 已提交
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<void>
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 85 86 87
**Parameters**
| Name     | Type                   | Mandatory  | Description           |
| -------- | --------------------- | ---- | ------------- |
| effectId | [EffectId](#effectid) | Yes   | String that indicates the vibration effect.|
W
wusongqing 已提交
88

W
wusongqing 已提交
89 90 91 92
**Return value**
| Type                 | Description         |
| ------------------- | ----------- |
| Promise<void> | 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<void>): 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 115 116 117 118
**Parameters**
| Name     | Type                       | Mandatory  | Description                     |
| -------- | ------------------------- | ---- | ----------------------- |
| effectId | [EffectId](#effectid)     | Yes   | String that indicates the vibration effect.          |
| callback | AsyncCallback<void> | 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<void>

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

W
wusongqing 已提交
147 148 149 150
**Return value**
| Type                 | Description         |
| ------------------- | ----------- |
| Promise<void> | 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<void>): 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 173 174 175 176
**Parameters**
| 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 已提交
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

W
wusongqing 已提交
196 197
| Name                | Default Value                 | Description             |
| ------------------ | -------------------- | --------------- |
W
wusongqing 已提交
198
| 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

W
wusongqing 已提交
207 208 209
| 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.|
W
wusongqing 已提交
210
| 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.|