1# 传感器开发指导(C/C++)
2
3
4## 场景介绍
5
6当设备需要获取传感器数据时,可以使用sensor模块,例如:通过订阅方向传感器数据感知用户设备当前的朝向,通过订阅计步传感器数据统计用户的步数等。
7
8详细的接口介绍请参考[Sensor接口](../../reference/apis-sensor-service-kit/_sensor.md)。
9
10## 函数说明
11
12| 名称                                                         | 描述                                                         |
13| ------------------------------------------------------------ | ------------------------------------------------------------ |
14| OH_Sensor_GetInfos(Sensor_Info **infos, uint32_t *count)     | 获取设备上所有传感器的信息。                                 |
15| OH_Sensor_Subscribe(const Sensor_SubscriptionId *id, const Sensor_SubscriptionAttribute *attribute, const Sensor_Subscriber *subscriber) | 订阅传感器数据。系统将以指定的频率向用户上报传感器数据。<br/>订阅加速度传感器,需要申请ohos.permission.ACCELEROMETER权限;<br/>订阅陀螺仪传感器,需要申请ohos.permission.GYROSCOPE权限;<br/>订阅计步器相关传感器时,需要申请ohos.permission.ACTIVITY_MOTION权限;<br/>订阅与健康相关的传感器时,比如心率传感器需要申请ohos.permission.READ_HEALTH_DATA权限,否则订阅失败;<br/>订阅其余传感器不需要申请权限。 |
16| OH_Sensor_Unsubscribe(const Sensor_SubscriptionId *id, const Sensor_Subscriber *subscriber) | 取消订阅传感器数据。<br/>取消订阅加速度计传感器,需要申请ohos.permission.ACCELEROMETER权限; <br/>取消订阅陀螺仪传感器,需要申请ohos.permission.GYROSCOPE权限; <br/>取消订阅计步器相关传感器时,需要申请ohos.permission.ACTIVITY_MOTION权限;<br/>取消订阅与健康相关的传感器时,需要申请ohos.permission.READ_HEALTH_DATA权限,否则取消订阅失败。 <br/>取消订阅其余传感器不需要申请权限。 |
17| OH_Sensor_CreateInfos(uint32_t count)                        | 用给定的数字创建一个实例数组,请参考[Sensor_Info](../../reference/apis-sensor-service-kit/_sensor.md#sensor_info)。 |
18| OH_Sensor_DestroyInfos(Sensor_Info **sensors, uint32_t count) | 销毁实例数组并回收内存,请参考[Sensor_Info](../../reference/apis-sensor-service-kit/_sensor.md#sensor_info)。 |
19| OH_SensorInfo_GetName(Sensor_Info *sensor, char *sensorName, uint32_t *length) | 获取传感器名称。                                             |
20| OH_SensorInfo_GetVendorName(Sensor_Info* sensor, char *vendorName, uint32_t *length) | 获取传感器的厂商名称。                                       |
21| OH_SensorInfo_GetType(Sensor_Info* sensor, Sensor_Type *sensorType) | 获取传感器类型。                                             |
22| OH_SensorInfo_GetResolution(Sensor_Info* sensor, float *resolution) | 获取传感器分辨率。                                           |
23| OH_SensorInfo_GetMinSamplingInterval(Sensor_Info* sensor, int64_t *minSamplingInterval) | 获取传感器的最小数据上报间隔。                               |
24| OH_SensorInfo_GetMaxSamplingInterval(Sensor_Info* sensor, int64_t *maxSamplingInterval) | 获取传感器的最大数据上报间隔时间。                           |
25| OH_SensorEvent_GetType(Sensor_Event* sensorEvent, Sensor_Type *sensorType) | 获取传感器类型。                                             |
26| OH_SensorEvent_GetTimestamp(Sensor_Event* sensorEvent, int64_t *timestamp) | 获取传感器数据的时间戳。                                     |
27| OH_SensorEvent_GetAccuracy(Sensor_Event* sensorEvent, Sensor_Accuracy *accuracy) | 获取传感器数据的精度。                                       |
28| OH_SensorEvent_GetData(Sensor_Event* sensorEvent, float **data, uint32_t *length) | 获取传感器数据。<br/>数据的长度和内容依赖于监听的传感器类型,传感器上报的数据格式如下:<br/>1.SENSOR_TYPE_ACCELEROMETER:data[0]、data[1]、data[2]分别表示设备x、y、z轴的加速度分量,单位m/s²;<br/>2.SENSOR_TYPE_GYROSCOPE:data[0]、data[1]、data[2]分别表示设备x、y、z轴的旋转角速度,单位弧度/s;<br/>3.SENSOR_TYPE_AMBIENT_LIGHT:data[0]表示环境光强度,单位lux;从API Version 12开始,将返回两个额外的数据,其中data[1]表示色温,单位kelvin;data[2]表示红外亮度,单位cd/m²;<br/> 4.SENSOR_TYPE_MAGNETIC_FIELD:data[0]、data[1]、data[2]分别表示设备x、y、z轴的地磁分量,单位微特斯拉; <br/>5.SENSOR_TYPE_BAROMETER:data[0]表示气压值,单位hPa;<br/>6.SENSOR_TYPE_HALL:data[0]表示皮套吸合状态,0表示打开,大于0表示吸附;<br/>7.SENSOR_TYPE_PROXIMITY:data[0]表示接近状态,0表示接近,大于0表示远离;<br/>8.SENSOR_TYPE_ORIENTATION:data[0]、data[1]、data[2]分别表示设备绕z、x、y轴的角度,单位度;<br/>9.SENSOR_TYPE_GRAVITY:data[0]、data[1]、data[2]分别表示设备x、y、z轴的重力加速度分量,单位m/s²;<br/>10.SENSOR_TYPE_ROTATION_VECTOR:data[0]、data[1]、data[2]分别表示设备x、y、z轴的旋转角度,单位度,data[3]表示旋转向量元素;<br/>11.SENSOR_TYPE_PEDOMETER_DETECTION:data[0]表示计步检测状态,1表示检测到了步数变化;<br/>12.SENSOR_TYPE_PEDOMETER:data[0]表示步数;<br/>13.SENSOR_TYPE_HEART_RATE:data[0]表示心率数值。 |
29| OH_Sensor_CreateSubscriptionId(void)                         | 创建一个Sensor_SubscriptionId 实例。                         |
30| OH_Sensor_DestroySubscriptionId(Sensor_SubscriptionId *id)   | 销毁Sensor_SubscriptionId 实例并回收内存。                   |
31| OH_SensorSubscriptionId_SetType(Sensor_SubscriptionId* id, const Sensor_Type sensorType) | 设置传感器类型。                                             |
32| OH_Sensor_CreateSubscriptionAttribute(void)                  | 创建Sensor_SubscriptionAttribute实例。                       |
33| OH_Sensor_DestroySubscriptionAttribute(Sensor_SubscriptionAttribute *attribute) | 销毁Sensor_SubscriptionAttribute实例并回收内存。             |
34| OH_SensorSubscriptionAttribute_SetSamplingInterval(Sensor_SubscriptionAttribute* attribute, const int64_t samplingInterval) | 设置传感器数据上报间隔。                                     |
35| OH_Sensor_CreateSubscriber(void)                             | 创建一个Sensor_Subscriber实例。                              |
36| OH_Sensor_DestroySubscriber(Sensor_Subscriber *subscriber)   | 销毁Sensor_Subscriber实例并回收内存。                        |
37| OH_SensorSubscriber_SetCallback(Sensor_Subscriber* subscriber, const Sensor_EventCallback callback) | 设置一个回调函数来上报传感器数据。                           |
38
39
40## 开发步骤
41
42开发步骤以加速度传感器为例。
43
441. 新建一个Native C++工程。
45
46   ![输入图片说明](figures/004.png)
47
482. 配置加速度传感器权限,具体配置方式请参考[声明权限](../../security/AccessToken/declare-permissions.md)。
49
50   ```json
51   "requestPermissions": [
52         {
53           "name": "ohos.permission.ACCELEROMETER",
54         },
55       ]
56   ```
57
583. CMakeLists.txt文件中引入动态依赖库。
59
60   ```c
61   target_link_libraries(entry PUBLIC libace_napi.z.so)
62   target_link_libraries(entry PUBLIC libhilog_ndk.z.so)
63   target_link_libraries(entry PUBLIC libohsensor.so)
64   ```
65
664. 在napi_init.cpp文件中编码,首先导入模块。
67
68   ```c
69   #include "sensors/oh_sensor.h"
70   #include "napi/native_api.h"
71   #include "hilog/log.h"
72   #include <thread>
73   ```
74
755. 定义常量。
76
77   ```c
78   const int GLOBAL_RESMGR = 0xFF00;
79   const char *TAG = "[Sensor]";
80   constexpr Sensor_Type SENSOR_ID { SENSOR_TYPE_ACCELEROMETER };
81   constexpr uint32_t SENSOR_NAME_LENGTH_MAX = 64;
82   constexpr int64_t SENSOR_SAMPLE_PERIOD = 200000000;
83   constexpr int32_t SLEEP_TIME_MS = 1000;
84   constexpr int64_t INVALID_VALUE = -1;
85   constexpr float INVALID_RESOLUTION = -1.0F;
86   Sensor_Subscriber *g_user = nullptr;
87   ```
88
896. 定义一个回调函数用来接收传感器数据。
90
91   ```c
92   void SensorDataCallbackImpl(Sensor_Event *event) {
93       if (event == nullptr) {
94           OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "event is null");
95           return;
96       }
97       int64_t timestamp = INVALID_VALUE;
98       int32_t ret = OH_SensorEvent_GetTimestamp(event, &timestamp); // 获取传感器数据的时间戳。
99       if (ret != SENSOR_SUCCESS) {
100           return;
101       }
102       Sensor_Type sensorType;
103       ret = OH_SensorEvent_GetType(event, &sensorType); // 获取传感器类型。
104       if (ret != SENSOR_SUCCESS) {
105           return;
106       }
107       Sensor_Accuracy accuracy = SENSOR_ACCURACY_UNRELIABLE;
108       ret = OH_SensorEvent_GetAccuracy(event, &accuracy); // 获取传感器数据的精度。
109       if (ret != SENSOR_SUCCESS) {
110           return;
111       }
112       float *data = nullptr;
113       uint32_t length = 0;
114       ret = OH_SensorEvent_GetData(event, &data, &length); // 获取传感器数据。
115       if (ret != SENSOR_SUCCESS) {
116           return;
117       }
118       OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "sensorType:%{public}d, dataLen:%{public}d, accuracy:%{public}d", sensorType, length, accuracy);
119       for (uint32_t i = 0; i < length; ++i) {
120           OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "data[%{public}d]:%{public}f", i, data[i]);
121       }
122   }
123   ```
124
1257. 获取设备上所有传感器的信息。
126
127   ```c
128   static napi_value GetSensorInfos(napi_env env, napi_callback_info info)
129   {
130       uint32_t count = 0;
131       int32_t ret = OH_Sensor_GetInfos(nullptr, &count); // 获取设备上所有传感器的个数。
132       if (ret != SENSOR_SUCCESS) {
133           return nullptr;
134       }
135       Sensor_Info **sensors = OH_Sensor_CreateInfos(count); // 用给定的数字创建一个实例数组。
136       if (sensors == nullptr) {
137           return nullptr;
138       }
139       ret = OH_Sensor_GetInfos(sensors, &count); // 获取设备上所有传感器的信息。
140       if (ret != SENSOR_SUCCESS) {
141           return nullptr;
142       }
143       for (uint32_t i = 0; i < count; ++i) {
144           char sensorName[SENSOR_NAME_LENGTH_MAX] = {};
145           uint32_t length = SENSOR_NAME_LENGTH_MAX;
146           ret = OH_SensorInfo_GetName(sensors[i], sensorName, &length); // 获取传感器名称。
147           if (ret != SENSOR_SUCCESS) {
148               return nullptr;
149           }
150           char vendorName[SENSOR_NAME_LENGTH_MAX] = {};
151           length = SENSOR_NAME_LENGTH_MAX;
152           ret = OH_SensorInfo_GetVendorName(sensors[i], vendorName, &length); // 获取传感器的厂商名称。
153           if (ret != SENSOR_SUCCESS) {
154               return nullptr;
155           }
156           Sensor_Type sensorType;
157           ret = OH_SensorInfo_GetType(sensors[i], &sensorType); // 获取传感器类型。
158           if (ret != SENSOR_SUCCESS) {
159               return nullptr;
160           }
161           float resolution = INVALID_RESOLUTION;
162           ret = OH_SensorInfo_GetResolution(sensors[i], &resolution); // 获取传感器分辨率。
163           if (ret != SENSOR_SUCCESS) {
164               return nullptr;
165           }
166           int64_t minSamplePeriod = INVALID_VALUE;
167           ret = OH_SensorInfo_GetMinSamplingInterval(sensors[i], &minSamplePeriod); // 获取传感器的最小数据上报间隔。
168           if (ret != SENSOR_SUCCESS) {
169               return nullptr;
170           }
171           int64_t maxSamplePeriod = INVALID_VALUE;
172           ret = OH_SensorInfo_GetMaxSamplingInterval(sensors[i], &maxSamplePeriod); // 获取传感器的最大数据上报间隔时间。
173           if (ret != SENSOR_SUCCESS) {
174               return nullptr;
175           }
176       }
177       OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "GetSensorInfos sucessful");
178       ret = OH_Sensor_DestroyInfos(sensors, count); // 销毁实例数组并回收内存。
179       if (ret != SENSOR_SUCCESS) {
180           return nullptr;
181       }
182   }
183   ```
184
1858. 订阅和取消订阅传感器数据。
186
187   ```c
188   static napi_value Subscriber(napi_env env, napi_callback_info info)
189   {
190       g_user = OH_Sensor_CreateSubscriber();                                         // 创建一个Sensor_Subscriber实例。
191       int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl); // 设置一个回调函数来报告传感器数据。
192       if (ret != SENSOR_SUCCESS) {
193           return nullptr;
194       }
195
196       Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId(); // 创建一个Sensor_SubscriptionId实例。
197       ret = OH_SensorSubscriptionId_SetType(id, SENSOR_ID);         // 设置传感器类型。
198       if (ret != SENSOR_SUCCESS) {
199           return nullptr;
200       }
201
202       Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();     // 创建Sensor_SubscriptionAttribute实例。
203       ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD); // 设置传感器数据报告间隔。
204       if (ret != SENSOR_SUCCESS) {
205           return nullptr;
206       }
207
208       ret = OH_Sensor_Subscribe(id, attr, g_user); // 订阅传感器数据。
209       if (ret != SENSOR_SUCCESS) {
210           return nullptr;
211       }
212       OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "Subscriber successful");
213       std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS));
214       ret = OH_Sensor_Unsubscribe(id, g_user); // 取消订阅传感器数据。
215       if (ret != SENSOR_SUCCESS) {
216           return nullptr;
217       }
218       if (id != nullptr) {
219           OH_Sensor_DestroySubscriptionId(id); // 销毁Sensor_SubscriptionId实例并回收内存。
220       }
221       if (attr != nullptr) {
222           OH_Sensor_DestroySubscriptionAttribute(attr); // 销毁Sensor_SubscriptionAttribute实例并回收内存。
223       }
224       if (g_user != nullptr) {
225           OH_Sensor_DestroySubscriber(g_user); // 销毁Sensor_Subscriber实例并回收内存。
226           g_user = nullptr;
227       }
228   }
229   ```
230
2319. 在Init函数中补充接口。
232
233   ```c
234   static napi_value Init(napi_env env, napi_value exports)
235      {
236          napi_property_descriptor desc[] = {
237              { "getSensorInfos", nullptr, GetSensorInfos, nullptr, nullptr, nullptr, napi_default, nullptr },
238              { "subscriber", nullptr, Subscriber, nullptr, nullptr, nullptr, napi_default, nullptr }
239          };
240          napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
241          return exports;
242      }
243      EXTERN_C_END
244   ```
245
24610. 在types/libentry路径下index.d.ts文件中引入Napi接口。
247
248    ```c
249     export const getSensorInfos: () => number;
250     export const subscriber: () => number;
251    ```
252
25311. 删除Index.ets中的已废弃函数。
254
255    ```js
256    .onClick(() => {
257        hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.add(2, 3));
258    })
259    ```
260