• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 振动开发指导(C/C++)
2<!--Kit: Sensor Service Kit-->
3<!--Subsystem: Sensors-->
4<!--Owner: @dilligencer-->
5<!--Designer: @butterls-->
6<!--Tester: @murphy84-->
7<!--Adviser: @hu-zhiqiong-->
8
9## 场景介绍
10
11当设备需要设置不同的振动效果时,可以调用Vibrator模块,例如:设备的按键可以设置不同强度和不同时长的振动,闹钟和来电可以设置不同强度和时长的单次或周期振动。
12
13详细的接口介绍请参考[Vibrator接口](../../reference/apis-sensor-service-kit/vibrator_8h.md)。
14
15
16## 函数说明
17
18| 名称                                                         | 描述                           |
19| ------------------------------------------------------------ | ------------------------------ |
20| OHOS::Sensors::OH_Vibrator_PlayVibration(int32_t duration, Vibrator_Attribute attribute) | 控制马达在指定时间内持续振动。 |
21| OHOS::Sensors::OH_Vibrator_PlayVibrationCustom(Vibrator_FileDescription fileDescription, Vibrator_Attribute vibrateAttribute) | 播放自定义振动序列。           |
22| OHOS::Sensors::OH_Vibrator_Cancel()                          | 停止马达振动。                 |
23
24## 振动效果说明
25
26目前支持两类振动效果,如下所示。
27
28### 固定时长振动
29
30传入一个固定时长,马达按照默认强度和频率触发振动。
31
32### 自定义振动
33
34自定义振动提供给用户设计自己所需振动效果的能力,用户可通过自定义振动配置文件,并遵循相应规则编排所需振动形式,使能更加开放的振感交互体验。
35
36
37## 开发步骤
38
391. 新建一个Native C++工程。
40
41   ![输入图片说明](figures/004.png)
42
432. 控制设备上的振动器,需要申请权限ohos.permission.VIBRATE。具体配置方式请参考[声明权限](../../security/AccessToken/declare-permissions.md)。
44
45   ```json
46   "requestPermissions": [
47         {
48           "name": "ohos.permission.VIBRATE",
49         },
50       ]
51   ```
52
533. CMakeLists.txt文件中引入动态依赖库。
54
55   ```c
56   target_link_libraries(entry PUBLIC libace_napi.z.so)
57   target_link_libraries(entry PUBLIC libhilog_ndk.z.so)
58   target_link_libraries(entry PUBLIC libohvibrator.z.so)
59   ```
60
614. 导入模块。
62
63   ```c
64   #include <sensors/vibrator.h>
65   #include "napi/native_api.h"
66   #include "hilog/log.h"
67   #include <thread>
68   #include <fcntl.h>
69   #include <unistd.h>
70   #include <sys/stat.h>
71   ```
72
735. 定义常量。
74
75   ```c
76   const int GLOBAL_RESMGR = 0xFF00;
77   const char *TAG = "[NativeVibratorTest]";
78   constexpr int32_t TIME_WAIT_FOR_OP = 2;
79   ```
80
816. 控制马达在指定时间内持续振动和停止马达振动。
82
83   ```c
84   static napi_value Vibration_Test(napi_env env, napi_callback_info info)
85   {
86       Vibrator_Attribute vibrateAttribute;
87       vibrateAttribute.usage = VIBRATOR_USAGE_ALARM;
88
89       int32_t ret = OH_Vibrator_PlayVibration(0, vibrateAttribute); // 控制马达在指定时间内持续振动。
90       OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "Vibration successful");
91       if (ret != 0) {
92           return nullptr;
93       }
94       std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
95       ret = OH_Vibrator_Cancel(); // 停止马达振动。
96       if (ret != 0) {
97           return nullptr;
98       }
99       napi_value result = nullptr;
100       napi_create_int32(env, ret, &result);
101       return result;
102   }
103   ```
104
1058. 播放自定义振动序列。
106
107   ```c
108   static napi_value VibrationCustom_Test(napi_env env, napi_callback_info info)
109   {
110       int32_t ret = 0;
111       int32_t fd = open("/data/test/vibrator/coin_drop.json", O_RDONLY);
112       OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "Test fd:%{public}d", fd);
113       struct stat64 statbuf = { 0 };
114       if (fd < 0) {
115           OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "File open failed");
116           return nullptr;
117       }
118       if (fstat64(fd, &statbuf) == 0) {
119           Vibrator_FileDescription fileDescription = {
120               .fd = fd,
121               .offset = 0,
122               .length = statbuf.st_size
123           };
124           Vibrator_Attribute vibrateAttribute = {
125               .usage = VIBRATOR_USAGE_RING
126           };
127           ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute); // 播放自定义振动序列。
128           OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "Vibratecustom successful");
129           bool isSuccess = ((ret == 0) || (ret == UNSUPPORTED));
130           if (isSuccess == true) {
131               close(fd);
132               return nullptr;
133           }
134       }
135       std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
136       close(fd);
137       OH_Vibrator_Cancel(); // 停止马达振动。
138       napi_value result = nullptr;
139       napi_create_int32(env, ret, &result);
140       return result;
141   }
142   ```
143
1448. 在types/libentry路径下index.d.ts文件中引入Napi接口。
145
146   ```c
147   export const vibration_Test: () => number;
148   export const vibrationCustom_Test: () => number;
149   ```
150
1519. 编写Js用例调用接口。
152