js-apis-system-vibrate.md 2.0 KB
Newer Older
1
# @system.vibrator (Vibrator)
2

3 4

The **Vibrator** module provides APIs for controlling LED lights and vibrators. You can use the APIs to query the LED light list, turn on and off the LED light, query the vibrator list, query the vibrator effect, and trigger and turn off the vibrator.
5

W
wusongqing 已提交
6 7 8 9
Misc devices refer to LED lights and vibrators on devices. LED lights are mainly used for indication (for example, indicating the charging state) and blinking (such as tri-colored lights). Vibrators are mainly used in scenarios such as the alarm clock, power-on/off, and incoming call vibration.


> **NOTE**
10
> - The initial APIs of this module are supported since API version 4. Newly added APIs will be marked with a superscript to indicate their earliest API version.
W
wusongqing 已提交
11
> - The APIs of this module are no longer maintained since API version 8. You are advised to use [`@ohos.vibrator`](js-apis-vibrator.md) instead.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
> - This module requires hardware support and can only be debugged on real devices.


## Modules to Import


```
import vibrator from '@system.vibrator';
```

## vibrator.vibrate

vibrate(Object): void

Triggers device vibration.

**System capability**: SystemCapability.Sensors.MiscDevice

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

**Parameters**

W
wusongqing 已提交
34
| Name| Type| Mandatory| Description|
35 36
| -------- | -------- | -------- | -------- |
| mode | string | No| Vibration mode. The value **long** indicates long vibration, and **short** indicates short vibration. The default value is **long**.|
W
wusongqing 已提交
37 38 39
| success | Function | Yes| Called when the vibrator data changes.|
| fail | Function | No| Called when the API call fails.|
| complete | Function | No| Called when the API call is complete.|
40 41 42 43 44 45

**Example**

```
vibrator.vibrate({
  mode: 'short',
W
wusongqing 已提交
46
  success: function() {
47 48
    console.log('vibrate is successful');
  },
W
wusongqing 已提交
49 50
  fail: function(data, code) {
    console.log("vibrate is failed, data: " + data + ", code: " + code);
51
  },
W
wusongqing 已提交
52
  complete: function() {
53 54 55 56
    console.log('vibrate is completed');
  }
});
```