• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Sensors Usage Guidelines<a name="EN-US_TOPIC_0000001077367158"></a>
2
3-   [How to Use](#section18816105182315)
4
5The following steps use the sensor whose  **sensorTypeId**  is  **0**  as an example. The guidelines for other sensor types are similar.
6
7## How to Use<a name="section18816105182315"></a>
8
91.  Import the required header files.
10
11```
12#include "sensor_agent.h"
13#include "sensor_agent_type.h"
14```
15
161.  Create a sensor callback.
17
18```
19void SensorDataCallbackImpl(SensorEvent *event)
20{
21    if(event == NULL){
22        return;
23    }
24    float *sensorData=(float *)event->data;
25}
26```
27
28>![](../public_sys-resources/icon-note.gif) **NOTE:**
29>The callback must be of the RecordSensorCallback type.
30
311.  Obtain the list of sensors supported by the device.
32
33```
34SensorInfo *sensorInfo = (SensorInfo *)NULL;
35int32_t count = 0;
36int32_t ret = GetAllSensors(&sensorInfo, &count);
37```
38
391.  Create a sensor user.
40
41```
42SensorUser sensorUser;
43sensorUser.callback = SensorDataCallbackImpl; // Assign the created callback SensorDataCallbackImpl to the member variable callback.
44```
45
461.  Enable the sensor.
47
48```
49int32_t ret = ActivateSensor(0, &sensorUser);
50```
51
521.  Subscribe to sensor data.
53
54```
55int32_t ret = SubscribeSensor(0, &sensorUser);
56```
57
58>![](../public_sys-resources/icon-note.gif) **NOTE:**
59>Till now, you can obtain the sensor data via the callback.
60
611.  Unsubscribe from the sensor data.
62
63```
64int32_t ret = UnsubscribeSensor(0, &sensorUser);
65```
66
671.  Disable the sensor.
68
69```
70int32_t ret = DeactivateSensor(0, &sensorUser);
71```
72
73