3

在 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) {
        ???
    }

谢谢

4

2 回答 2

3

查看传感器的头文件:

ANDROID-NDK-ROOT-DIR/platforms/android-/arch-arm/usr/include/android/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

于 2013-10-21T09:41:58.217 回答
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
}
于 2019-05-20T16:44:13.190 回答