如何访问磁力计数据

时间:2014-03-08 20:27:33

标签: ios magnetometer cmmotionmanager

关注the docs,这是我尝试访问磁力计数据。不用说,它不起作用。

我已经使用完全相同的方法使陀螺仪和加速度计数据工作,但出于某种原因,我使用这个方法得到每个轴的全零。

 motionManager = [[CMMotionManager alloc] init];


[motionManager startDeviceMotionUpdates];
[motionManager startMagnetometerUpdatesToQueue:
 [NSOperationQueue currentQueue] withHandler:^(CMMagnetometerData *magnetometerData, NSError *error) {
     double x = motionManager.deviceMotion.magneticField.field.x;
     double y = motionManager.deviceMotion.magneticField.field.y;
     double z = motionManager.deviceMotion.magneticField.field.z;

     self.magnetometerDataLabel.text = [NSString stringWithFormat:@"{%8.4f, %8.4f, %8.4f}", x, y, z];
 }];
motionManager.magnetometerUpdateInterval = 1.0 / 60.0;

我错过了什么?

1 个答案:

答案 0 :(得分:1)

这有效:

[motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryCorrectedZVertical toQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {
    double x = motionManager.deviceMotion.magneticField.field.x;
    double y = motionManager.deviceMotion.magneticField.field.y;
    double z = motionManager.deviceMotion.magneticField.field.z;

    self.magnetometerDataLabel.text = [NSString stringWithFormat:@"{%8.4f, %8.4f, %8.4f}", x, y, z];
}];