# Sensor\_lite组件 - [简介](#section11660541593) - [目录](#section161941989596) - [说明](#section1312121216216) - [接口说明](#section827111510217) - [使用说明](#section129654513264) - [相关仓](#section1371113476307) ## 简介 泛Sensor服务子系统提供了轻量级sensor服务基础框架,提供了如下功能: - Sensor列表查询 - Sensor启停 - Sensor订阅/去订阅 - 设置数据上报模式 - 设置采样间隔等 泛Sensor服务框架如下图所示: **图 1** 泛Sensor服务框架图 ![](figures/泛Sensor服务框架图.png "泛Sensor服务框架图") ## 目录 ``` /base/sensors/sensor_lite ├── frameworks # 框架代码 │ ├── include # 头文件目录 │ └── src # 源代码目录 ├── interfaces # 接口目录 │ └── kits # Native接口目录 ├── services # 服务代码目录 │ ├── include # 头文件目录 │ └── src # 源代码目录 ``` ## 说明 ### 接口说明 **表 1** SensorAgent的主要接口

接口名

描述

GetAllSensors(SensorInfo **sensorInfo, int32_t *count)

获取系统中所有传感器的信息

SubscribeSensor(int32_t sensorTypeId, SensorUser *user)

订阅传感器数据,系统会将获取到的传感器数据上报给订阅者

UnsubscribeSensor(int32_t sensorTypeId, SensorUser *user)

去订阅传感器数据,系统将取消传感器数据上报给订阅者

SetBatch(int32_t sensorTypeId, SensorUser *user, int64_t samplingInterval, int64_t reportInterval)

设置传感器的数据采样间隔和数据上报间隔

ActivateSensor(int32_t sensorTypeId, SensorUser *user)

使能一个传感器订阅用户,只有在传感器使能之后,订阅该传感器的用户才能获取到数据

DeactivateSensor(int32_t sensorTypeId, SensorUser *user)

去使能一个传感器订阅用户

SetMode(int32_t sensorTypeId, SensorUser *user, int32_t mode)

设置传感器的数据上报模式

### 使用说明 本节以订阅加速度传感器数据为例进行介绍。 1. 导入需要的包 ``` #include "sensor_agent.h" #include "sensor_agent_type.h" ``` 1. 创建回调函数 ``` void SensorDataCallbackImpl(SensorEvent *event) { if(event == NULL){ return; } float *sensorData=(float *)event->data; } ``` 1. 获取设备支持的Sensor列表 ``` SensorInfo *sensorInfo = (SensorInfo *)NULL; int32_t count = 0; int32_t ret = GetAllSensors(&sensorInfo, &count); ``` 1. 创建传感器用户 ``` SensorUser sensorUser; sensorUser.callback = SensorDataCallbackImpl; //成员变量callback指向创建的回调方法 ``` 1. 使能传感器 ``` int32_t ret = ActivateSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser); ``` 1. 订阅传感器数据 ``` int32_t ret = SubscribeSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser); ``` 1. 取消传感器数据订阅 ``` int32_t ret = UnsubscribeSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser); ``` 1. 去使能一个传感器 ``` int32_t ret = DeactivateSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser); ``` ## 相关仓 [泛Sensor子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/%E6%B3%9BSensor%E5%AD%90%E7%B3%BB%E7%BB%9F.md) **sensors_sensor_lite** [sensors_miscdevice_lite](https://gitee.com/openharmony/sensors_miscdevice_lite/blob/master/README_zh.md)