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