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