# sensor\_lite - [Introduction](#section11660541593) - [Directory Structure](#section161941989596) - [Usage](#section1312121216216) - [Available APIs](#section827111510217) - [Usage Guidelines](#section129654513264) - [Repositories Involved](#section1371113476307) ## Introduction The pan-sensor service subsystem provides a lightweight sensor service framework, through which you can perform the following operations: - Query the sensor list. - Enable or disable a sensor. - Subscribe to or unsubscribe from sensor data. - Set the data reporting mode of a sensor. - Set the data sampling interval. The following figure shows the architecture of the pan-sensor framework. **Figure 1** Pan-sensor service architecture ![](figures/pan-sensor-service-architecture.png "pan-sensor-service-architecture") ## Directory Structure ``` /base/sensors/sensor_lite ├── frameworks # Framework code │ ├── include # Header files │ └── src # Source code ├── interfaces # APIs │ └── kits # Native APIs ├── services # Code of services │ ├── include # Header files │ └── src # Source code ``` ## Usage ### Available APIs **Table 1** Major APIs in SensorAgent

API

Description

GetAllSensors(SensorInfo **sensorInfo, int32_t *count)

Obtains information about all sensors in the system.

SubscribeSensor(int32_t sensorTypeId, SensorUser *user)

Subscribes to sensor data. The system will report the obtained sensor data to the subscriber.

UnsubscribeSensor(int32_t sensorTypeId, SensorUser *user)

Unsubscribes from sensor data. The system will no longer report sensor data to the subscriber.

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

Sets the data sampling interval and data reporting interval for the specified sensor.

ActivateSensor(int32_t sensorTypeId, SensorUser *user)

Enables the sensor that has been subscribed to. The subscriber can obtain the sensor data only after the sensor is enabled.

DeactivateSensor(int32_t sensorTypeId, SensorUser *user)

Disables an enabled sensor.

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

Sets the data reporting mode for the specified sensor.

### Usage Guidelines This section uses the acceleration sensor as an example to describe how to use sensor APIs. 1. Import the required header files. ``` #include "sensor_agent.h" #include "sensor_agent_type.h" ``` 1. Create a callback. ``` void SensorDataCallbackImpl(SensorEvent *event) { if(event == NULL){ return; } float *sensorData=(float *)event->data; } ``` 1. Obtain the list of sensors supported by the device. ``` SensorInfo *sensorInfo = (SensorInfo *)NULL; int32_t count = 0; int32_t ret = GetAllSensors(&sensorInfo, &count); ``` 1. Create a sensor user. ``` SensorUser sensorUser; sensorUser.callback = SensorDataCallbackImpl; // Assign the created callback SensorDataCallbackImpl to the member variable callback. ``` 1. Enable the sensor. ``` int32_t ret = ActivateSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser); ``` 1. Subscribe to sensor data. ``` int32_t ret = SubscribeSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser); ``` 1. Unsubscribe from the sensor data. ``` int32_t ret = UnsubscribeSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser); ``` 1. Disable the sensor. ``` int32_t ret = DeactivateSensor(SENSOR_TYPE_ID_ACCELEROMETER, &sensorUser); ``` ## Repositories Involved [Pan-sensor subsystem](https://gitee.com/openharmony/docs/blob/master/en/readme/pan-sensor.md) **sensors_sensor_lite** [sensors_miscdevice_lite](https://gitee.com/openharmony/sensors_miscdevice_lite/blob/master/README.md)