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