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

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

W
wusongqing 已提交
5 6 7 8
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**
G
Gloria 已提交
9
> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.
W
wusongqing 已提交
10
> - 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.
11 12 13 14 15 16 17 18 19 20 21 22
> - This module requires hardware support and can only be debugged on real devices.


## Modules to Import


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

## vibrator.vibrate

G
Gloria 已提交
23
 vibrate(options?: VibrateOptions): void
24 25 26

Triggers device vibration.

G
Gloria 已提交
27
**Required permissions**: ohos.permission.VIBRATE
28

29
**System capability**: SystemCapability.Sensors.MiscDevice.Lite
30 31 32

**Parameters**

G
Gloria 已提交
33 34 35
| Name | Type                             | Mandatory| Description      |
| ------- | --------------------------------- | ---- | ---------- |
| options | [VibrateOptions](#vibrateoptions) | No  | Vibration options.|
36 37 38

**Example**

39
```ts
40 41
vibrator.vibrate({
  mode: 'short',
W
wusongqing 已提交
42
  success: function() {
43
    console.info('Succeed in vibrating');
44
  },
W
wusongqing 已提交
45
  fail: function(data, code) {
46
    console.info(`Failed to vibrate. Data: ${data}, code: ${code}`);
47
  },
W
wusongqing 已提交
48
  complete: function() {
49
    console.info('completed in vibrating');
50 51 52
  }
});
```
G
Gloria 已提交
53 54 55 56 57 58 59

## VibrateOptions

Defines the vibration options.

**Required permissions**: ohos.permission.VIBRATE

60
**System capability**: SystemCapability.Sensors.MiscDevice.Lite
G
Gloria 已提交
61 62 63 64 65 66 67

| Name    | Type    | Mandatory| Description                                                        |
| -------- | -------- | ---- | ------------------------------------------------------------ |
| mode     | string   | No  | Vibration mode. The value **long** indicates long vibration, and **short** indicates short vibration. The default value is **long**.|
| success  | Function | No  | 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.                                    |