我正在尝试使用 CoreMotion 从我的 iphone 6 获取磁场数据。

我使用以下代码访问原始数据没有问题:

if available {
        motionMangager.magnetometerUpdateInterval = updateInterval
        motionMangager.startMagnetometerUpdatesToQueue(queue, withHandler: {
            (data, error: NSError!) -> Void in
            println("x: \(data.magneticField.x), y: \(data.magneticField.y), z: \(data.magneticField.z)")
        })
    }

但是:我需要使用设备运动实例来获取派生数据。

所以我做了以下事情:
if motionMangager.deviceMotionAvailable {
        motionMangager.magnetometerUpdateInterval = updateInterval
        motionMangager.startDeviceMotionUpdatesUsingReferenceFrame(CMAttitudeReferenceFrameXArbitraryZVertical, toQueue: queue, withHandler: {
            (deviceMotion: CMDeviceMotion!, error: NSError!) -> Void in
            // If no device-motion data is available, the value of this property is nil.
            if let motion = deviceMotion {
                println(motion)
                var accuracy = motion.magneticField.accuracy
                var x = motion.magneticField.field.x
                var y = motion.magneticField.field.y
                var z = motion.magneticField.field.z
                println("accuracy: \(accuracy.value), x: \(x), y: \(y), z: \(z)")
            }
            else {
                println("Device motion is nil.")
            }
        })
    }

这是问题所在:

对于字段坐标 x、y 和 z,我总是得到零。准确度也是-1。根据 Apple 文档,-1 的精度意味着“CMMagneticFieldCalibrationAccuracyUncalibrated”意味着“设备没有磁力计”......但不是!这是一部iPhone 6...

那么我做错了什么?我尝试了所有四个 CMAttitudeReferenceFrame。请我需要帮助。有任何想法吗?

最佳答案

好的。。我解决了。

零点的原因是未校准的磁力计!但这并不意味着没有苹果文档中所述的磁力计。反正我没想到会这样。

我只需要添加指南针校准的功能。这很容易添加:

motionMangager.showsDeviceMovementDisplay = true

(见: https://developer.apple.com/library/ios/documentation/CoreMotion/Reference/CMMotionManager_Class/ )

所以现在就像:当我得到零和 -1 的准确性时,会弹出要求校准的警报。移动后它消失了,我得到了正确的值。

10-07 19:40
查看更多