Android NDK中的陀螺仪和磁场传感器事件

时间:2013-09-30 08:15:57

标签: android android-ndk android-sensors

在Android上本地访问传感器时,如何访问磁场和陀螺仪传感器事件的值:

if(event.type == ASENSOR_TYPE_ACCELEROMETER) {
        float x = event.acceleration.x;
            ...
    }
else if(event.type == ASENSOR_TYPE_GYROSCOPE) {
        ???
    }
else if(event.type == ASENSOR_TYPE_MAGNETIC_FIELD) {
        ???
    }

由于

2 个答案:

答案 0 :(得分:3)

查看传感器的头文件:

  

Android的NDK-ROOT-DIR /平台/ android- /弓形臂的/ usr /包括/机器人/ sensor.h

typedef struct ASensorVector {
    union {
        float v[3];
        struct {
            float x;
            float y;
            float z;
        };
        struct {
            float azimuth;
            float pitch;
            float roll;
        };
    };
    int8_t status;
    uint8_t reserved[3];
} ASensorVector;

typedef struct ASensorEvent {
    int32_t version; /* sizeof(struct ASensorEvent) */
    int32_t sensor;
    int32_t type;
    int32_t reserved0;
    int64_t timestamp;
    union {
        float           data[16];
        ASensorVector   vector;
        ASensorVector   acceleration;
        ASensorVector   magnetic;
        float           temperature;
        float           distance;
        float           light;
        float           pressure;
    };
    int32_t reserved1[4];
} ASensorEvent;

顺便说一下,我找到了这个例子:

https://github.com/Uroc327Mirrors/pixellight/blob/43a661e762034054b47766d7e38d94baf22d2038/Base/PLInput/src/Backend/Android/AndroidSensorManagerDevice.cpp

答案 1 :(得分:0)

根据此https://developer.android.com/guide/topics/sensors/sensors_motion,您可以通过以下字段数据获取任何事件的数据:

if(event.type == ASENSOR_TYPE_GYROSCOPE) 
{
    float x=event.data[0];//Rate of rotation around the x axis
    float y=event.data[1];//Rate of rotation around the y axis
    float z=event.data[2];//Rate of rotation around the z axis
}