• Home
Name Date Size #Lines LOC

..--

figures/12-May-2024-

frameworks/12-May-2024-1,175842

interfaces/kits/native/12-May-2024-1,132624

services/12-May-2024-735606

LICENSED12-May-202410.1 KiB177150

OAT.xmlD12-May-20244 KiB6815

README.mdD12-May-20247.6 KiB161124

README_zh.mdD12-May-20247.3 KiB161124

bundle.jsonD12-May-2024820 3636

README.md

1# sensor\_lite<a name="EN-US_TOPIC_0000001078194836"></a>
2
3-   [Introduction](#section11660541593)
4-   [Directory Structure](#section161941989596)
5-   [Usage](#section1312121216216)
6    -   [Available APIs](#section827111510217)
7    -   [Usage Guidelines](#section129654513264)
8
9-   [Repositories Involved](#section1371113476307)
10
11## Introduction<a name="section11660541593"></a>
12
13The pan-sensor service subsystem provides a lightweight sensor service framework, through which you can perform the following operations:
14
15-   Query the sensor list.
16-   Enable or disable a sensor.
17-   Subscribe to or unsubscribe from sensor data.
18-   Set the data reporting mode of a sensor.
19-   Set the data sampling interval.
20
21The following figure shows the architecture of the pan-sensor framework.
22
23**Figure  1**  Pan-sensor service architecture<a name="fig15658513184019"></a>
24![](figures/pan-sensor-service-architecture.png "pan-sensor-service-architecture")
25
26## Directory Structure<a name="section161941989596"></a>
27
28```
29/base/sensors/sensor_lite
30├── frameworks      # Framework code
31│   ├── include     # Header files
32│   └── src         # Source code
33├── interfaces      # APIs
34│   └── kits        # Native APIs
35├── services        # Code of services
36│   ├── include     # Header files
37│   └── src         # Source code
38```
39
40## Usage<a name="section1312121216216"></a>
41
42### Available APIs<a name="section827111510217"></a>
43
44**Table  1**  Major APIs in SensorAgent
45
46<a name="table411220157213"></a>
47<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>API</p>
48</th>
49<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>Description</p>
50</th>
51</tr>
52</thead>
53<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>
54</td>
55<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>Obtains information about all sensors in the system.</p>
56</td>
57</tr>
58<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>
59</td>
60<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>Subscribes to sensor data. The system will report the obtained sensor data to the subscriber.</p>
61</td>
62</tr>
63<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>
64</td>
65<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>Unsubscribes from sensor data. The system will no longer report sensor data to the subscriber.</p>
66</td>
67</tr>
68<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>
69</td>
70<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>Sets the data sampling interval and data reporting interval for the specified sensor.</p>
71</td>
72</tr>
73<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>
74</td>
75<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>Enables the sensor that has been subscribed to. The subscriber can obtain the sensor data only after the sensor is enabled.</p>
76</td>
77</tr>
78<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>
79</td>
80<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>Disables an enabled sensor.</p>
81</td>
82</tr>
83<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>
84</td>
85<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>Sets the data reporting mode for the specified sensor.</p>
86</td>
87</tr>
88</tbody>
89</table>
90
91### Usage Guidelines<a name="section129654513264"></a>
92
93This section uses the acceleration sensor as an example to describe how to use sensor APIs.
94
951.  Import the required header files.
96
97```
98#include "sensor_agent.h"
99#include "sensor_agent_type.h"
100```
101
1021.  Create a callback.
103
104```
105void SensorDataCallbackImpl(SensorEvent *event)
106{
107    if(event == NULL){
108        return;
109    }
110    float *sensorData=(float *)event->data;
111}
112```
113
1141.  Obtain the list of sensors supported by the device.
115
116```
117SensorInfo *sensorInfo = (SensorInfo *)NULL;
118int32_t count = 0;
119int32_t ret = GetAllSensors(&sensorInfo, &count);
120```
121
1221.  Create a sensor user.
123
124```
125SensorUser sensorUser;
126sensorUser.callback = SensorDataCallbackImpl; // Assign the created callback SensorDataCallbackImpl to the member variable callback.
127```
128
1291.  Enable the sensor.
130
131```
132int32_t ret = ActivateSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser);
133```
134
1351.  Subscribe to sensor data.
136
137```
138int32_t ret = SubscribeSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser);
139```
140
1411.  Unsubscribe from the sensor data.
142
143```
144int32_t ret = UnsubscribeSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser);
145```
146
1471.  Disable the sensor.
148
149```
150int32_t ret = DeactivateSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser);
151```
152
153## Repositories Involved<a name="section1371113476307"></a>
154
155[Pan-sensor subsystem](https://gitee.com/openharmony/docs/blob/master/en/readme/pan-sensor.md)
156
157**sensors_sensor_lite**
158
159[sensors_miscdevice_lite](https://gitee.com/openharmony/sensors_miscdevice_lite/blob/master/README.md)
160
161

README_zh.md

1# Sensor\_lite组件<a name="ZH-CN_TOPIC_0000001078194836"></a>
2
3-   [简介](#section11660541593)
4-   [目录](#section161941989596)
5-   [说明](#section1312121216216)
6    -   [接口说明](#section827111510217)
7    -   [使用说明](#section129654513264)
8
9-   [相关仓](#section1371113476307)
10
11## 简介<a name="section11660541593"></a>
12
13泛Sensor服务子系统提供了轻量级sensor服务基础框架,提供了如下功能:
14
15-   Sensor列表查询
16-   Sensor启停
17-   Sensor订阅/去订阅
18-   设置数据上报模式
19-   设置采样间隔等
20
21泛Sensor服务框架如下图所示:
22
23**图 1**  泛Sensor服务框架图<a name="fig15658513184019"></a>
24![](figures/泛Sensor服务框架图.png "泛Sensor服务框架图")
25
26## 目录<a name="section161941989596"></a>
27
28```
29/base/sensors/sensor_lite
30├── frameworks      # 框架代码
31│   ├── include     # 头文件目录
32│   └── src         # 源代码目录
33├── interfaces      # 接口目录
34│   └── kits        # Native接口目录
35├── services        # 服务代码目录
36│   ├── include     # 头文件目录
37│   └── src         # 源代码目录
38```
39
40## 说明<a name="section1312121216216"></a>
41
42### 接口说明<a name="section827111510217"></a>
43
44**表 1**  SensorAgent的主要接口
45
46<a name="table411220157213"></a>
47<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>
48</th>
49<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>
50</th>
51</tr>
52</thead>
53<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>
54</td>
55<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>
56</td>
57</tr>
58<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>
59</td>
60<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>
61</td>
62</tr>
63<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>
64</td>
65<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>
66</td>
67</tr>
68<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>
69</td>
70<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>
71</td>
72</tr>
73<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>
74</td>
75<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>
76</td>
77</tr>
78<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>
79</td>
80<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>
81</td>
82</tr>
83<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>
84</td>
85<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>
86</td>
87</tr>
88</tbody>
89</table>
90
91### 使用说明<a name="section129654513264"></a>
92
93本节以订阅加速度传感器数据为例进行介绍。
94
951.  导入需要的包
96
97```
98#include "sensor_agent.h"
99#include "sensor_agent_type.h"
100```
101
1021.  创建回调函数
103
104```
105void SensorDataCallbackImpl(SensorEvent *event)
106{
107    if(event == NULL){
108        return;
109    }
110    float *sensorData=(float *)event->data;
111}
112```
113
1141.  获取设备支持的Sensor列表
115
116```
117SensorInfo *sensorInfo = (SensorInfo *)NULL;
118int32_t count = 0;
119int32_t ret = GetAllSensors(&sensorInfo, &count);
120```
121
1221.  创建传感器用户
123
124```
125SensorUser sensorUser;
126sensorUser.callback = SensorDataCallbackImpl; //成员变量callback指向创建的回调方法
127```
128
1291.  使能传感器
130
131```
132int32_t ret = ActivateSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser);
133```
134
1351.  订阅传感器数据
136
137```
138int32_t ret = SubscribeSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser);
139```
140
1411.  取消传感器数据订阅
142
143```
144int32_t ret = UnsubscribeSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser);
145```
146
1471.  去使能一个传感器
148
149```
150int32_t ret = DeactivateSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser);
151```
152
153## 相关仓<a name="section1371113476307"></a>
154
155[泛Sensor子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/%E6%B3%9BSensor%E5%AD%90%E7%B3%BB%E7%BB%9F.md)
156
157**sensors_sensor_lite**
158
159[sensors_miscdevice_lite](https://gitee.com/openharmony/sensors_miscdevice_lite/blob/master/README_zh.md)
160
161