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


## 场景介绍

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

8
详细的接口介绍请参考[Vibrator接口](../reference/apis/js-apis-vibrator.md)
H
update  
HelloCrease 已提交
9 10 11 12


## 接口说明

C
cff-gite 已提交
13 14 15 16 17 18
| 模块          | 接口名                                                       | 描述                                                         |
| ------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| ohos.vibrator | startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise<void> | 根据指定振动效果和振动属性触发马达振动,使用Promise异步回调。 |
| ohos.vibrator | startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: AsyncCallback<void>): void | 根据指定振动效果和振动属性触发马达振动,使用Callback异步回调。 |
| ohos.vibrator | stopVibration(stopMode: VibratorStopMode): Promise<void> | 按照指定模式停止马达的振动。                                 |
| ohos.vibrator | stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback<void>): void | 按照指定模式停止马达的振动。                                 |
19 20 21 22
| ohos.vibrator | stopVibration(callback: AsyncCallback<void>): void     | 停止所有模式的马达振动。                                     |
| ohos.vibrator | stopVibration(): Promise<void>                         | 停止所有模式的马达振动。                                     |
| ohos.vibrator | isSupportEffect(effectId: string, callback: AsyncCallback<boolean>): void | 查询是否支持传入的参数effectId。                             |
| ohos.vibrator | isSupportEffect(effectId: string): Promise<void>       | 查询是否支持传入的参数effectId。                             |
H
update  
HelloCrease 已提交
23 24 25 26


## 开发步骤

H
h00514358 已提交
27
1. 控制设备上的振动器,需要申请权限ohos.permission.VIBRATE。具体配置方式请参考[权限申请声明](../security/accesstoken-guidelines.md)
H
update  
HelloCrease 已提交
28

L
li-yaoyao777 已提交
29
2. 根据指定振动效果和振动属性触发马达振动。
C
cff-gite 已提交
30 31

   ```js
C
cff-gite 已提交
32
   import vibrator from '@ohos.vibrator';
C
cff-gite 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
   try {
       vibrator.startVibration({
           type: 'time',
           duration: 1000,
       }, {
           id: 0,
           usage: 'alarm'
       }, (error) => {
           if (error) {
               console.error('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
               return;
           }
           console.log('Callback returned to indicate a successful vibration.');
       });
   } catch (err) {
       console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
   }
   ```

L
li-yaoyao777 已提交
52
3. 按照指定模式停止马达的振动。 
C
cff-gite 已提交
53 54

   ```js
C
cff-gite 已提交
55
   import vibrator from '@ohos.vibrator';
C
cff-gite 已提交
56 57 58 59 60 61 62 63 64 65 66 67
   try {
     // 按照VIBRATOR_STOP_MODE_TIME模式停止振动
     vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, function (error) {
         if (error) {
             console.log('error.code' + error.code + 'error.message' + error.message);
             return;
         }
         console.log('Callback returned to indicate successful.');
     })
   } catch (err) {
     console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
   }
L
li-yaoyao777 已提交
68
   ```
C
cff-gite 已提交
69
   
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
4. 停止所有模式的马达振动。

   ```js
   import vibrator from '@ohos.vibrator';
   try {
       // 停止所有模式的马达振动
       vibrator.stopVibration(function (error) {
           if (error) {
               console.log('error.code' + error.code + 'error.message' + error.message);
               return;
           }
           console.log('Callback returned to indicate successful.');
       })
   } catch (error) {
       console.info('errCode: ' + error.code + ' ,msg: ' + error.message);
   }
   ```

5. 查询是否支持传入的参数effectId。

   ```js
   import vibrator from '@ohos.vibrator';
   try {
       // 查询是否支持'haptic.clock.timer'
       vibrator.isSupportEffect('haptic.clock.timer', function (state) {
           console.log('The effectId is ' + (state ? 'supported' : 'unsupported'));
           if (state) {
               try {
       		    vibrator.startVibration({
                   type: 'preset',
                   effectId: 'haptic.clock.timer',
                   count: 1,
                   }, {
                       usage: 'unknown'
                   }, (error) => {
                       if(error) {
                           console.log('haptic.clock.timer vibrator error');
                       } else {
                           console.log('haptic.clock.timer vibrator success');
                       }
                   });
               } catch (error) {
                   console.error('errCode: ' + error.code + ' ,msg: ' + error.message);
               }
           }
       })
   } catch (error) {
       console.error('exception in, error:' + JSON.stringify(error));
   }
   ```

C
cff-gite 已提交
121 122
   

Z
zengyawen 已提交
123 124 125 126
## 相关实例

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

Z
zwx1094577 已提交
127
- [`Vibrator`:振动(ArkTS)(API9)](https://gitee.com/openharmony/applications_app_samples/tree/master/code/BasicFeature/DeviceManagement/Vibrator)