Android上的设备偏航

时间:2014-10-06 12:46:19

标签: android android-ndk rotation orientation angle

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

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

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

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

...

referenceAttitude = [specific->motionManager.deviceMotion.attitude retain];

当我需要相对偏航时,我会这样做:

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

如何在Android中制作此内容?在NDK中有一个名为android / sensor.h的文件,但是我只能从队列获得ASensorEvent :: vector.roll,它的旋转速度比角度大,然后以这种方式计算yaw:

yaw = 0;

...

yaw += ASensorEvent::vector.roll * dt;

这在低转速下运行正常,但是非常不准确。我怎样才能在Android上获得相对态度?

1 个答案:

答案 0 :(得分:0)

看起来这个代码示例完全符合您的要求(至少从标题中)

  

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

http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html