Sensor

The Android sensor framework
supports a wide variety of sensor types to measure the conditions of the
physical environment and read the raw data from apps. Using sensor drivers, your
apps can extend this framework and add new sensor devices connected over
Peripheral I/O.

The data from these sensors is delivered through the same
SensorManager APIs as the
built-in Android sensors. Your app can implement a driver to connect a new
sensor of a known type, such as an accelerometer, or a sensor type that Android
doesn't currently define, such as a blood glucose sensor.

Adding the required permission

Add the required permission for the user driver to your app's manifest file:

Implementing the driver

The framework polls your driver periodically when listeners are registered for
sensor updates. To respond to poll requests for new data, extend the
UserSensorDriver class and override the read() method. Each call to read
should return a new UserSensorReading containing the current sensor data.

Note: For more information on the data format expected for known sensor types,
see sensor event values.

The framework may call read() at a time when the driver is not able to produce
a sensor reading. When this occurs, your driver should throw an IOException.

If your sensor supports low power or sleep modes, override the setEnabled()
method of your driver implementation to activate them. The framework calls this
method to indicate that sensors should be ramped up to deliver readings or put
to sleep to save power:

Note: Sensors without low power modes can still use this callback to increase or
reduce the reporting frequency of the data in order to manage power consumption.

Describing the sensor

To add a new sensor driver to the Android framework:

Use the UserSensor.Builder to declare the sensor's type.

For most apps, the sensor type should match one of Android's existing known
Sensor types.

Provide the sensor name and vendor name of your driver.

Apply the range, resolution, update frequency (delay), and power
requirements (if available) of your sensor to the builder. These values
assist the framework in selecting the best sensor based on the requests
received by SensorManager.

With the driver properly registered, apps can receive updates from the
associated device using the existing Android sensor framework
services.

The registration process can take some time before the sensor is available to
clients. Apps interested in a user sensor should register a
DynamicSensorCallback
to be notified when it is available before registering a listener for sensor
readings: