vibrator-guidelines.md 3.3 KB
Newer Older
H
update  
HelloCrease 已提交
1 2 3 4 5 6 7 8 9 10
# Vibrator开发指导


## 场景介绍

当设备需要设置不同的振动效果时,可以调用Vibrator模块,例如:设备的按键可以设置不同强度和不同时长的振动,闹钟和来电可以设置不同强度和时长的单次或周期振动。


## 接口说明

11 12 13 14 15 16 17 18
| 模块            | 接口名                                      | 描述                              |
| ------------- | ---------------------------------------- | ------------------------------- |
| ohos.vibrator | vibrate(duration: number): Promise<void> | 触发马达按照时长振动,使用Promise异步回调。       |
| ohos.vibrator | vibrate(duration: number, callback?: AsyncCallback<void>): void | 触发马达按照时长振动,使用Callback异步回调。      |
| ohos.vibrator | vibrate(effectId: EffectId): Promise<void> | 触发马达按照开发者传递效果振动,使用Promise异步回调。  |
| ohos.vibrator | vibrate(effectId: EffectId, callback?: AsyncCallback<void>): void | 触发马达按照开发者传递效果振动,使用Callback异步回调。 |
| ohos.vibrator | stop(stopMode: VibratorStopMode): Promise<void> | 停止振动。                           |
| ohos.vibrator | stop(stopMode: VibratorStopMode, callback?: AsyncCallback<void>): void | 停止振动。                           |
H
update  
HelloCrease 已提交
19 20 21 22


## 开发步骤

23
1. 控制设备上的振动器,需要在`config.json`里面进行配置请求权限。具体如下:
H
HelloCrease 已提交
24

H
update  
HelloCrease 已提交
25
   ```
26
   "reqPermissions": [
H
update  
HelloCrease 已提交
27
     {
28 29 30 31 32
         "name": "ohos.permission.ACCELEROMETER",
         "reason": "", 
         "usedScene": {
         "ability": [
             ".MainAbility"
H
update  
HelloCrease 已提交
33
         ],
34
         "when": "inuse"
H
update  
HelloCrease 已提交
35 36 37
       }
     },
     {
38 39 40 41 42
         "name": "ohos.permission.VIBRATE",
         "reason": "", 
         "usedScene": {
         "ability": [
             ".MainAbility"
H
update  
HelloCrease 已提交
43
         ],
44
         "when": "inuse"
H
update  
HelloCrease 已提交
45 46 47
       }
     },
     {
48 49 50 51 52
         "name": "ohos.permission.ACTIVITY_MOTION",
         "reason": "", 
         "usedScene": {
         "ability": [
             ".MainAbility"
H
update  
HelloCrease 已提交
53
         ],
54
         "when": "inuse"
H
update  
HelloCrease 已提交
55 56 57 58 59 60
       }
     },
   ]
   ```

2. 触发设备振动。
H
HelloCrease 已提交
61

H
update  
HelloCrease 已提交
62 63
   ```
   import vibrator from "@ohos.vibrator"
C
cff-gite 已提交
64
   vibrator.vibrate(1000).then((error)=>{
H
update  
HelloCrease 已提交
65
       if(error){//调用失败,打印error.code和error.message
C
cff-gite 已提交
66
          Console.log("Promise return failed.error.code"+error.code+"error.message"+error.message);  
H
update  
HelloCrease 已提交
67
       }else{//调用成功,设备开始振动
C
cff-gite 已提交
68
          Console.log("Promise returned to indicate a successful vibration.")  
H
update  
HelloCrease 已提交
69 70 71 72 73
       };
   })
   ```

3. 停止设备振动。
H
HelloCrease 已提交
74

H
update  
HelloCrease 已提交
75 76
   ```
   import vibrator from "@ohos.vibrator"
C
cff-gite 已提交
77
   vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then((error)=>{
H
update  
HelloCrease 已提交
78
      if(error){//调用失败,打印error.code和error.message
C
cff-gite 已提交
79
          Console.log("Promise return failed.error.code"+error.code+"error.message"+error.message);
H
update  
HelloCrease 已提交
80
      }else{//调用成功,设备停止振动
C
cff-gite 已提交
81
          Console.log("Promise returned to indicate successful.");
H
update  
HelloCrease 已提交
82 83 84
      };
   })
   ```
Z
zengyawen 已提交
85 86 87 88 89 90

## 相关实例

针对振动开发,有以下相关实例可供参考:

- [`Vibrator`:振动(eTS)(API8)](https://gitee.com/openharmony/app_samples/tree/master/device/Vibrator)