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

W
wusongqing 已提交
13 14 15 16 17
## System Capabilities

SystemCapability.Sensors.MiscDevice

## Required Permissions
Z
zengyawen 已提交
18 19 20 21

ohos.permission.VIBRATE


W
wusongqing 已提交
22 23 24 25
## vibrator.vibrate

vibrate(duration: number): Promise<void>

Z
zengyawen 已提交
26 27 28

Triggers vibration with a specific duration. This method uses a promise to return the execution result.

W
wusongqing 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | duration | number | Yes| Vibration duration.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise<void> | Promise used to indicate whether the vibration is triggered successfully.|


- Example
  ```
  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 已提交
54 55 56

Triggers vibration with a specific duration. This method uses a callback to return the execution result.

W
wusongqing 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | duration | number | Yes| Vibration duration.|
  | callback | AsyncCallback<void> | No| Callback used to indicate whether the vibration is triggered successfully.|

- Example
  ```
  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 已提交
78 79 80

Triggers vibration with a specific effect. This method uses a promise to return the execution result.

W
wusongqing 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | effectId | [EffectId](#effectid) | Yes| String that indicates the vibration effect.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise<void> | Promise used to indicate whether the vibration is triggered successfully.|

- Example
  ```
  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 已提交
104 105 106

Triggers vibration with a specific effect. This method uses a callback to return the execution result.

W
wusongqing 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
- 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.|

- Example
  ```
  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>

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | stopMode | [VibratorStopMode](#vibratorstopmode) | Yes| Vibration mode to stop.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise<void> | Promise used to indicate whether the vibration is stopped successfully.|

- Example
  ```
  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 已提交
155
Stops the vibration based on the specified **stopMode**. This method uses an asynchronous callback to return the execution result. If the specified **stopMode** is different from the mode used to trigger the vibration, this method fails to be called.
W
wusongqing 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168

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

- Example
  ```
  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 已提交
169
          console.log("Callback returned to indicate a successful stop.");
W
wusongqing 已提交
170 171 172 173 174 175
      }
  })
  ```


## EffectId
Z
zengyawen 已提交
176 177 178

Describes the vibration effect.

W
wusongqing 已提交
179 180 181
| Name| Default Value| Description|
| -------- | -------- | -------- |
| EFFECT_CLOCK_TIMER | "haptic.clock.timer" | Vibration effect of the vibrator when a user adjusts the timer.|
Z
zengyawen 已提交
182 183


W
wusongqing 已提交
184 185 186
## VibratorStopMode

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

W
wusongqing 已提交
188 189 190 191
| 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.|