1e41f4b71Sopenharmony_ci# Sensor Usage Guidelines 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe following steps use the sensor whose **sensorTypeId** is **0** as an example. The guidelines for other sensor types are similar. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci## How to Use 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci1. Import the required header files. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci ```c 11e41f4b71Sopenharmony_ci #include "sensor_agent.h" 12e41f4b71Sopenharmony_ci #include "sensor_agent_type.h" 13e41f4b71Sopenharmony_ci ``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci2. Create a sensor callback. 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci ```c 18e41f4b71Sopenharmony_ci void SensorDataCallbackImpl(SensorEvent *event) 19e41f4b71Sopenharmony_ci { 20e41f4b71Sopenharmony_ci if(event == NULL){ 21e41f4b71Sopenharmony_ci return; 22e41f4b71Sopenharmony_ci } 23e41f4b71Sopenharmony_ci float *sensorData=(float *)event->data; 24e41f4b71Sopenharmony_ci } 25e41f4b71Sopenharmony_ci ``` 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci >  **NOTE** 28e41f4b71Sopenharmony_ci > The callback must be of the RecordSensorCallback type. 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci3. Obtain the list of sensors supported by the device. 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci ```c 33e41f4b71Sopenharmony_ci SensorInfo *sensorInfo = (SensorInfo *)NULL; 34e41f4b71Sopenharmony_ci int32_t count = 0; 35e41f4b71Sopenharmony_ci int32_t ret = GetAllSensors(&sensorInfo, &count); 36e41f4b71Sopenharmony_ci ``` 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci4. Create a sensor user. 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci ```c 41e41f4b71Sopenharmony_ci SensorUser sensorUser; 42e41f4b71Sopenharmony_ci sensorUser.callback = SensorDataCallbackImpl; // Assign the created callback SensorDataCallbackImpl to the member variable callback. 43e41f4b71Sopenharmony_ci ``` 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci5. Enable the sensor. 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci ```c 48e41f4b71Sopenharmony_ci int32_t ret = ActivateSensor(0, &sensorUser); 49e41f4b71Sopenharmony_ci ``` 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci6. Subscribe to sensor data. 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci ```c 54e41f4b71Sopenharmony_ci int32_t ret = SubscribeSensor(0, &sensorUser); 55e41f4b71Sopenharmony_ci ``` 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci >  **NOTE** 58e41f4b71Sopenharmony_ci > Till now, you can obtain the sensor data via the callback. 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci7. Unsubscribe from the sensor data. 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci ```c 63e41f4b71Sopenharmony_ci int32_t ret = UnsubscribeSensor(0, &sensorUser); 64e41f4b71Sopenharmony_ci ``` 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci8. Disable the sensor. 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci ```c 69e41f4b71Sopenharmony_ci int32_t ret = DeactivateSensor(0, &sensorUser); 70e41f4b71Sopenharmony_ci ``` 71