HolyBlackCat HolyBlackCat - 3 months ago 14
C Question

Why enum values for some sensors are missing from <android/sensor.h>? Is it safe to use them?

It seems that enum values for some sensors are missing from

<android/sensor.h>
.

The file defines following enum:

/*
* Sensor types
* (keep in sync with hardware/sensor.h)
*/

enum {
ASENSOR_TYPE_ACCELEROMETER = 1,
ASENSOR_TYPE_MAGNETIC_FIELD = 2,
ASENSOR_TYPE_GYROSCOPE = 4,
ASENSOR_TYPE_LIGHT = 5,
ASENSOR_TYPE_PROXIMITY = 8
};


The comment says that this enum is synced with
<hardware/sensor.h>
(which is not exposed, it's a part of android source).

But in the
<hardware/sensor.h>
I've found values for many more useful sensors like pressure, temperature and humidity ones:

#define SENSOR_TYPE_ACCELEROMETER (1)
#define SENSOR_TYPE_GEOMAGNETIC_FIELD (2)
#define SENSOR_TYPE_ORIENTATION (3)
#define SENSOR_TYPE_GYROSCOPE (4)
#define SENSOR_TYPE_LIGHT (5)
#define SENSOR_TYPE_PRESSURE (6)
#define SENSOR_TYPE_TEMPERATURE (7)
#define SENSOR_TYPE_PROXIMITY (8)
#define SENSOR_TYPE_GRAVITY (9)
#define SENSOR_TYPE_LINEAR_ACCELERATION (10)
#define SENSOR_TYPE_ROTATION_VECTOR (11)
#define SENSOR_TYPE_RELATIVE_HUMIDITY (12)
// ...
#define SENSOR_TYPE_HEART_BEAT (31)


Why some sensors are not listed in the
<android/sensor.h>
? It it safe and portable to use them?

Answer

At the moment it is absolutely safe to use constants from <hardware/sensor.h> since SensorManager populates its internal sensor list with data from system-wide ISensorServer without any remapping of type values. And ISensorServer uses <hardware/sensor.h> constants.

I guess NDK API exposes constrained type set just because it was not updated for a while.

P.S. Of course things will be broken if some kind of mapping arise in future, but IMO AOSP will keep it unchanged.

Comments