回复
     详谈鸿蒙系统中的Sensor_lite组件
风在云颠
 发布于 2021-9-13 19:13
 浏览
 0收藏
简介
泛Sensor服务子系统提供了轻量级sensor服务基础框架,提供了如下功能:
- Sensor列表查询
 - Sensor启停
 - Sensor订阅/去订阅
 - 设置数据上报模式
 - 设置采样间隔等
 
泛Sensor服务框架如下图所示:
图 1 泛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) | 设置传感器的数据上报模式 | 
使用说明
本节以订阅加速度传感器数据为例进行介绍。
- 导入需要的包
 
#include "sensor_agent.h"
#include "sensor_agent_type.h"
- 创建回调函数
 
void SensorDataCallbackImpl(SensorEvent *event)
{
    if(event == NULL){
        return;
    }
    float *sensorData=(float *)event->data;
}
- 获取设备支持的Sensor列表
 
SensorInfo *sensorInfo = (SensorInfo *)NULL;
int32_t count = 0;
int32_t ret = GetAllSensors(&sensorInfo, &count);
- 创建传感器用户
 
SensorUser sensorUser;
sensorUser.callback = SensorDataCallbackImpl; //成员变量callback指向创建的回调方法
- 使能传感器
 
int32_t ret = ActivateSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser);
- 订阅传感器数据
 
int32_t ret = SubscribeSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser);
- 取消传感器数据订阅
 
int32_t ret = UnsubscribeSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser);
- 去使能一个传感器
 
int32_t ret = DeactivateSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser);
相关仓
泛Sensor子系统
sensors_sensor_lite
sensors_miscdevice_lite
sensors_sensor_lite-master.zip 67.69K 29次下载  
已于2021-9-13 19:13:20修改
 
        赞
        
 
        收藏 
      
 回复
  相关推荐
 



















