Opening and closing a connection to a sensor unit
Applications use a handle provided through the Sensor API to access each of the logical sensor units that are configured in the sensor configuration file.
Opening a connection
Before you can do anything with a sensor, you need to first request a handle to a logical sensor unit from the Sensor library. Your application uses this handle to make use of a given sensor through the Sensor API. To request a sensor handle, call sensor_open(). For example:
int err;
sensor_unit_t unit = SENSOR_UNIT_1;
sensor_handle_t handle = SENSOR_HANDLE_INVALID;
...
err = sensor_open(unit, SENSOR_ACCESSMODE_DATA, &handle);
...
When you call sensor_open(), you must specify the following:
- the sensor unit
-
This is the sensor unit that you are requesting the handle for. This sensor unit corresponds to one of the
sensors that are listed in your sensor configuration file. See the
Sensor configuration file
section in the System Services guide. - the mode of access
-
This mode specifies what aspects of the sensor you have access to. For example, SENSOR_ACCESSMODE_DATA
indicates that your application has access to only the sensor's data, not its configuration; the sensor's configuration
is specified in your sensor configuration file and can't be modified dynamically. See the
Sensor configuration file
section in the System Services guide.
When sensor_open() returns successfully, the library provides you with a handle, in the handle argument of the function call, that you can use throughout your application to access the specified sensor.
Closing a connection
After you are finished using a sensor data unit, you must close the connection so the Sensor library releases all the resources that it allocated for this sensor unit. To close a connection to a sensor, call sensor_close(). For example:
int err;
...
err = sensor_close(handle);
...
The handle that you pass into the sensor_close() function must have been a handle that was requested with sensor_open().
