Search code examples
androidandroid-ndkandroid-sensors

Why missing sensor type 3 in Android?


I'm working with Android sensors. <sensor.h> has:

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

When I dump sensors with ASensorManager_getSensorList on an HTC Evo, I get:

SensorList: BMA150 3-axis Accelerometer (Bosh), 1
SensorList: AK8973 3-axis Magnetic field sensor (Asahi Kasei), 2
SensorList: AK8973 Orientation sensor (Asahi Kasei), 3
SensorList: CM3602 Proximity sensor (Capella Microsystems), 8
SensorList: CM3602 Light sensor (Capella Microsystems), 5

When I cross the enums to the list, it appears sensor type 3 is orientation.

Question: Will the orientation sensor always be sensor type 3? Or will it change depending on the manufacturer?


Edit: And here's the dump from an ASUS TF-101 tablet:

SensorList: MPL rotation vector (Invensense), 11
SensorList: MPL linear accel (Invensense), 10
SensorList: MPL gravity (Invensense), 9
SensorList: MPL Gyro (Invensense), 4
SensorList: MPL accel (Invensense), 1
SensorList: MPL magnetic field (Invensense), 2
SensorList: MPL Orientation (Invensense), 3
SensorList: Lite-On al3000a Ambient Light Sensor (Lite-On), 5

Orientation is 3 again, but there are a few additional sensors.


Edit: And here's the dump from an Zeki TBQG 1084B tablet:

SensorList: STK831x accelerometer (Sensortek) 1 10000 1.000000

No orientation (and only 1 sensor is kind of cheap, I guess its for games).


Solution

  • Enum is for types of sensors device can have and each type should always be of the same numerical value across platform. So no matter what device type or vendor is, if it got gyroscope then it should always be reported as ASENSOR_TYPE_GYROSCOPE which in the end equals to 4. If you see gyroscope reported differently on device then this is a bug i.e. in low level drivers on that device and most likely would be fixed with next firmware update or so.

    These values must stay the same on each device otherwise your app would not be able to really function. It would be a huge pain to develop for sensors if types would not be unified as executable binaries operate on numeric values, not ENUMs/defines and such. And the main point of having ENUMs is to use them instead of final values to make your code more readable. It's more clear what code do when is see i.e.

    if( sensor == ASENSOR_TYPE_GYROSCOPE )
    

    than

    if( sensor == 4 ) 
    

    As for Orientation sensor and its missing ENUM entry - this sensor was deprecated in API8 and you are recommended now to use Magnetic Field Sensor and Accelerometer to get equivalent functionality (see example implementation), so that's most likely the reason it disappeared from ENUMs.