11f0016d6Sopenharmony_ci# Sensor\_lite组件<a name="ZH-CN_TOPIC_0000001078194836"></a>
21f0016d6Sopenharmony_ci
31f0016d6Sopenharmony_ci-   [简介](#section11660541593)
41f0016d6Sopenharmony_ci-   [目录](#section161941989596)
51f0016d6Sopenharmony_ci-   [说明](#section1312121216216)
61f0016d6Sopenharmony_ci    -   [接口说明](#section827111510217)
71f0016d6Sopenharmony_ci    -   [使用说明](#section129654513264)
81f0016d6Sopenharmony_ci
91f0016d6Sopenharmony_ci-   [相关仓](#section1371113476307)
101f0016d6Sopenharmony_ci
111f0016d6Sopenharmony_ci## 简介<a name="section11660541593"></a>
121f0016d6Sopenharmony_ci
131f0016d6Sopenharmony_ci泛Sensor服务子系统提供了轻量级sensor服务基础框架,提供了如下功能:
141f0016d6Sopenharmony_ci
151f0016d6Sopenharmony_ci-   Sensor列表查询
161f0016d6Sopenharmony_ci-   Sensor启停
171f0016d6Sopenharmony_ci-   Sensor订阅/去订阅
181f0016d6Sopenharmony_ci-   设置数据上报模式
191f0016d6Sopenharmony_ci-   设置采样间隔等
201f0016d6Sopenharmony_ci
211f0016d6Sopenharmony_ci泛Sensor服务框架如下图所示:
221f0016d6Sopenharmony_ci
231f0016d6Sopenharmony_ci**图 1**  泛Sensor服务框架图<a name="fig15658513184019"></a>  
241f0016d6Sopenharmony_ci![](figures/泛Sensor服务框架图.png "泛Sensor服务框架图")
251f0016d6Sopenharmony_ci
261f0016d6Sopenharmony_ci## 目录<a name="section161941989596"></a>
271f0016d6Sopenharmony_ci
281f0016d6Sopenharmony_ci```
291f0016d6Sopenharmony_ci/base/sensors/sensor_lite
301f0016d6Sopenharmony_ci├── frameworks      # 框架代码
311f0016d6Sopenharmony_ci│   ├── include     # 头文件目录
321f0016d6Sopenharmony_ci│   └── src         # 源代码目录
331f0016d6Sopenharmony_ci├── interfaces      # 接口目录
341f0016d6Sopenharmony_ci│   └── kits        # Native接口目录
351f0016d6Sopenharmony_ci├── services        # 服务代码目录
361f0016d6Sopenharmony_ci│   ├── include     # 头文件目录
371f0016d6Sopenharmony_ci│   └── src         # 源代码目录
381f0016d6Sopenharmony_ci```
391f0016d6Sopenharmony_ci
401f0016d6Sopenharmony_ci## 说明<a name="section1312121216216"></a>
411f0016d6Sopenharmony_ci
421f0016d6Sopenharmony_ci### 接口说明<a name="section827111510217"></a>
431f0016d6Sopenharmony_ci
441f0016d6Sopenharmony_ci**表 1**  SensorAgent的主要接口
451f0016d6Sopenharmony_ci
461f0016d6Sopenharmony_ci<a name="table411220157213"></a>
471f0016d6Sopenharmony_ci<table><thead align="left"><tr id="row131101415123"><th class="cellrowborder" valign="top" width="62.339999999999996%" id="mcps1.2.3.1.1"><p id="p181106151923"><a name="p181106151923"></a><a name="p181106151923"></a>接口名</p>
481f0016d6Sopenharmony_ci</th>
491f0016d6Sopenharmony_ci<th class="cellrowborder" valign="top" width="37.66%" id="mcps1.2.3.1.2"><p id="p1511021515212"><a name="p1511021515212"></a><a name="p1511021515212"></a>描述</p>
501f0016d6Sopenharmony_ci</th>
511f0016d6Sopenharmony_ci</tr>
521f0016d6Sopenharmony_ci</thead>
531f0016d6Sopenharmony_ci<tbody><tr id="row711081513219"><td class="cellrowborder" valign="top" width="62.339999999999996%" headers="mcps1.2.3.1.1 "><p id="p111020153217"><a name="p111020153217"></a><a name="p111020153217"></a><strong id="b1411081511214"><a name="b1411081511214"></a><a name="b1411081511214"></a>GetAllSensors</strong>(SensorInfo **sensorInfo, int32_t *count)</p>
541f0016d6Sopenharmony_ci</td>
551f0016d6Sopenharmony_ci<td class="cellrowborder" valign="top" width="37.66%" headers="mcps1.2.3.1.2 "><p id="p71104156214"><a name="p71104156214"></a><a name="p71104156214"></a>获取系统中所有传感器的信息</p>
561f0016d6Sopenharmony_ci</td>
571f0016d6Sopenharmony_ci</tr>
581f0016d6Sopenharmony_ci<tr id="row7111111511212"><td class="cellrowborder" valign="top" width="62.339999999999996%" headers="mcps1.2.3.1.1 "><p id="p1111091510212"><a name="p1111091510212"></a><a name="p1111091510212"></a><strong id="b1011011514211"><a name="b1011011514211"></a><a name="b1011011514211"></a>SubscribeSensor</strong>(int32_t sensorTypeId, SensorUser *user)</p>
591f0016d6Sopenharmony_ci</td>
601f0016d6Sopenharmony_ci<td class="cellrowborder" valign="top" width="37.66%" headers="mcps1.2.3.1.2 "><p id="p011014153214"><a name="p011014153214"></a><a name="p011014153214"></a>订阅传感器数据,系统会将获取到的传感器数据上报给订阅者</p>
611f0016d6Sopenharmony_ci</td>
621f0016d6Sopenharmony_ci</tr>
631f0016d6Sopenharmony_ci<tr id="row8111121512211"><td class="cellrowborder" valign="top" width="62.339999999999996%" headers="mcps1.2.3.1.1 "><p id="p1211117151227"><a name="p1211117151227"></a><a name="p1211117151227"></a><strong id="b141111715023"><a name="b141111715023"></a><a name="b141111715023"></a>UnsubscribeSensor</strong>(int32_t sensorTypeId, SensorUser *user)</p>
641f0016d6Sopenharmony_ci</td>
651f0016d6Sopenharmony_ci<td class="cellrowborder" valign="top" width="37.66%" headers="mcps1.2.3.1.2 "><p id="p311115159211"><a name="p311115159211"></a><a name="p311115159211"></a>去订阅传感器数据,系统将取消传感器数据上报给订阅者</p>
661f0016d6Sopenharmony_ci</td>
671f0016d6Sopenharmony_ci</tr>
681f0016d6Sopenharmony_ci<tr id="row21111151822"><td class="cellrowborder" valign="top" width="62.339999999999996%" headers="mcps1.2.3.1.1 "><p id="p311110151824"><a name="p311110151824"></a><a name="p311110151824"></a><strong id="b8111201519219"><a name="b8111201519219"></a><a name="b8111201519219"></a>SetBatch</strong>(int32_t sensorTypeId, SensorUser *user, int64_t samplingInterval, int64_t reportInterval)</p>
691f0016d6Sopenharmony_ci</td>
701f0016d6Sopenharmony_ci<td class="cellrowborder" valign="top" width="37.66%" headers="mcps1.2.3.1.2 "><p id="p91111151727"><a name="p91111151727"></a><a name="p91111151727"></a>设置传感器的数据采样间隔和数据上报间隔</p>
711f0016d6Sopenharmony_ci</td>
721f0016d6Sopenharmony_ci</tr>
731f0016d6Sopenharmony_ci<tr id="row8111115520"><td class="cellrowborder" valign="top" width="62.339999999999996%" headers="mcps1.2.3.1.1 "><p id="p411113155213"><a name="p411113155213"></a><a name="p411113155213"></a><strong id="b1111181512210"><a name="b1111181512210"></a><a name="b1111181512210"></a>ActivateSensor</strong>(int32_t sensorTypeId, SensorUser *user)</p>
741f0016d6Sopenharmony_ci</td>
751f0016d6Sopenharmony_ci<td class="cellrowborder" valign="top" width="37.66%" headers="mcps1.2.3.1.2 "><p id="p13111111520217"><a name="p13111111520217"></a><a name="p13111111520217"></a>使能一个传感器订阅用户,只有在传感器使能之后,订阅该传感器的用户才能获取到数据</p>
761f0016d6Sopenharmony_ci</td>
771f0016d6Sopenharmony_ci</tr>
781f0016d6Sopenharmony_ci<tr id="row15111151518213"><td class="cellrowborder" valign="top" width="62.339999999999996%" headers="mcps1.2.3.1.1 "><p id="p11112158215"><a name="p11112158215"></a><a name="p11112158215"></a><strong id="b2011118151228"><a name="b2011118151228"></a><a name="b2011118151228"></a>DeactivateSensor</strong>(int32_t sensorTypeId, SensorUser *user)</p>
791f0016d6Sopenharmony_ci</td>
801f0016d6Sopenharmony_ci<td class="cellrowborder" valign="top" width="37.66%" headers="mcps1.2.3.1.2 "><p id="p51111215729"><a name="p51111215729"></a><a name="p51111215729"></a>去使能一个传感器订阅用户</p>
811f0016d6Sopenharmony_ci</td>
821f0016d6Sopenharmony_ci</tr>
831f0016d6Sopenharmony_ci<tr id="row10111171514213"><td class="cellrowborder" valign="top" width="62.339999999999996%" headers="mcps1.2.3.1.1 "><p id="p21114151528"><a name="p21114151528"></a><a name="p21114151528"></a><strong id="b1911119151727"><a name="b1911119151727"></a><a name="b1911119151727"></a>SetMode</strong>(int32_t sensorTypeId, SensorUser *user, int32_t mode)</p>
841f0016d6Sopenharmony_ci</td>
851f0016d6Sopenharmony_ci<td class="cellrowborder" valign="top" width="37.66%" headers="mcps1.2.3.1.2 "><p id="p51110150210"><a name="p51110150210"></a><a name="p51110150210"></a>设置传感器的数据上报模式</p>
861f0016d6Sopenharmony_ci</td>
871f0016d6Sopenharmony_ci</tr>
881f0016d6Sopenharmony_ci</tbody>
891f0016d6Sopenharmony_ci</table>
901f0016d6Sopenharmony_ci
911f0016d6Sopenharmony_ci### 使用说明<a name="section129654513264"></a>
921f0016d6Sopenharmony_ci
931f0016d6Sopenharmony_ci本节以订阅加速度传感器数据为例进行介绍。
941f0016d6Sopenharmony_ci
951f0016d6Sopenharmony_ci1.  导入需要的包
961f0016d6Sopenharmony_ci
971f0016d6Sopenharmony_ci```
981f0016d6Sopenharmony_ci#include "sensor_agent.h"
991f0016d6Sopenharmony_ci#include "sensor_agent_type.h"
1001f0016d6Sopenharmony_ci```
1011f0016d6Sopenharmony_ci
1021f0016d6Sopenharmony_ci1.  创建回调函数
1031f0016d6Sopenharmony_ci
1041f0016d6Sopenharmony_ci```
1051f0016d6Sopenharmony_civoid SensorDataCallbackImpl(SensorEvent *event)
1061f0016d6Sopenharmony_ci{
1071f0016d6Sopenharmony_ci    if(event == NULL){
1081f0016d6Sopenharmony_ci        return;
1091f0016d6Sopenharmony_ci    }
1101f0016d6Sopenharmony_ci    float *sensorData=(float *)event->data;
1111f0016d6Sopenharmony_ci}
1121f0016d6Sopenharmony_ci```
1131f0016d6Sopenharmony_ci
1141f0016d6Sopenharmony_ci1.  获取设备支持的Sensor列表
1151f0016d6Sopenharmony_ci
1161f0016d6Sopenharmony_ci```
1171f0016d6Sopenharmony_ciSensorInfo *sensorInfo = (SensorInfo *)NULL;
1181f0016d6Sopenharmony_ciint32_t count = 0;
1191f0016d6Sopenharmony_ciint32_t ret = GetAllSensors(&sensorInfo, &count);
1201f0016d6Sopenharmony_ci```
1211f0016d6Sopenharmony_ci
1221f0016d6Sopenharmony_ci1.  创建传感器用户
1231f0016d6Sopenharmony_ci
1241f0016d6Sopenharmony_ci```
1251f0016d6Sopenharmony_ciSensorUser sensorUser;
1261f0016d6Sopenharmony_cisensorUser.callback = SensorDataCallbackImpl; //成员变量callback指向创建的回调方法
1271f0016d6Sopenharmony_ci```
1281f0016d6Sopenharmony_ci
1291f0016d6Sopenharmony_ci1.  使能传感器
1301f0016d6Sopenharmony_ci
1311f0016d6Sopenharmony_ci```
1321f0016d6Sopenharmony_ciint32_t ret = ActivateSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser);
1331f0016d6Sopenharmony_ci```
1341f0016d6Sopenharmony_ci
1351f0016d6Sopenharmony_ci1.  订阅传感器数据
1361f0016d6Sopenharmony_ci
1371f0016d6Sopenharmony_ci```
1381f0016d6Sopenharmony_ciint32_t ret = SubscribeSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser);
1391f0016d6Sopenharmony_ci```
1401f0016d6Sopenharmony_ci
1411f0016d6Sopenharmony_ci1.  取消传感器数据订阅
1421f0016d6Sopenharmony_ci
1431f0016d6Sopenharmony_ci```
1441f0016d6Sopenharmony_ciint32_t ret = UnsubscribeSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser);
1451f0016d6Sopenharmony_ci```
1461f0016d6Sopenharmony_ci
1471f0016d6Sopenharmony_ci1.  去使能一个传感器
1481f0016d6Sopenharmony_ci
1491f0016d6Sopenharmony_ci```
1501f0016d6Sopenharmony_ciint32_t ret = DeactivateSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser);
1511f0016d6Sopenharmony_ci```
1521f0016d6Sopenharmony_ci
1531f0016d6Sopenharmony_ci## 相关仓<a name="section1371113476307"></a>
1541f0016d6Sopenharmony_ci
1551f0016d6Sopenharmony_ci[泛Sensor子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/%E6%B3%9BSensor%E5%AD%90%E7%B3%BB%E7%BB%9F.md)
1561f0016d6Sopenharmony_ci
1571f0016d6Sopenharmony_ci**sensors_sensor_lite**
1581f0016d6Sopenharmony_ci
1591f0016d6Sopenharmony_ci[sensors_miscdevice_lite](https://gitee.com/openharmony/sensors_miscdevice_lite/blob/master/README_zh.md)
1601f0016d6Sopenharmony_ci
161