Android上的设备偏航

Android上的设备偏航,android,android-ndk,rotation,orientation,angle,Android,Android Ndk,Rotation,Orientation,Angle,我希望我的应用程序可以在iOS和Android设备上运行 当设备放在桌子上时,我需要它像指南针一样工作,但没有磁强计,相对偏航对我来说就足够了。我不关心设备未平放的情况 在iOS中,我首先存储参考态度: motionManager = [[CMMotionManager alloc] init]; [motionManager startDeviceMotionUpdates]; 当我需要相对偏航时,我这样认为: attitude = specific->motionManager.d

我希望我的应用程序可以在iOS和Android设备上运行

当设备放在桌子上时,我需要它像指南针一样工作,但没有磁强计,相对偏航对我来说就足够了。我不关心设备未平放的情况

在iOS中,我首先存储参考态度:

motionManager = [[CMMotionManager alloc] init];
[motionManager startDeviceMotionUpdates];

当我需要相对偏航时,我这样认为:

attitude = specific->motionManager.deviceMotion.attitude;
[attitude multiplyByInverseOfAttitude:specific->referenceAttitude];    
return attitude.yaw;
yaw = 0;
如何在Android中实现这一点?在NDK中有一个名为android/sensor.h的文件,但我只能从队列中获取一个sensorevent::vector.roll,它是旋转速度而不是角度,然后通过以下方式计算偏航:

attitude = specific->motionManager.deviceMotion.attitude;
[attitude multiplyByInverseOfAttitude:specific->referenceAttitude];    
return attitude.yaw;
yaw = 0;


这在低转速下工作正常,但非常不舒适。我如何才能在Android上获得相对的态度?

至少从标题上看,这个代码示例非常适合您的要求

在这篇文章中,我们将展示如何获取传感器的信息 设备做一个简单的指南针应用


代码似乎使用磁强计来计算姿态。