如何使用swift获取磁力计数据? [英] How to get magnetometer data using swift?

查看:214
本文介绍了如何使用swift获取磁力计数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get the magnetic field data from my iphone 6 by using CoreMotion.

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

I had no problems accessing the raw data with the following code:

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 ...

I am always getting zero for the field coordinates x, y and z. The accuracy as well is -1. According to the Apple Documentation an accuracy of -1 means "CMMagneticFieldCalibrationAccuracyUncalibrated" means "the device does not have a magnetometer"... But no! It is an iPhone 6...

那么我在做什么错了?我尝试了所有四个CMAttitudeReferenceFrame.请我帮忙.有什么想法吗?

So what am I doing wrong? I tried all four CMAttitudeReferenceFrame. Please I need help. Any ideas?

推荐答案

好的.

归零的原因是未校准的磁力计!但这并不意味着没有像苹果文档中所述的磁力计.无论如何,我从来没有想到过.

The reason for the zeros were an uncalibrated magnetometer! But that does not mean that there is no magnetometer like stated in the apple docs. Anyway I did never expected that.

我只需要添加指南针校准功能.这相当容易添加:

I just needed to add the functionality of compass calibration. And that is fairly easy to add:

motionMangager.showsDeviceMovementDisplay = true

(请参阅: https://developer.apple.com/库/ios/文档/CoreMotion/Reference/CMMotionManager_Class/)

所以现在就像:当我的精度为零和-1时,弹出警告校准的警报.移动之后,它消失了,我得到了适当的价值.

So now it is like: When I am getting zeros and -1 for accuracy than the alert asking for calibration pops up. After a move it vanishes and I am getting proper values.

这篇关于如何使用swift获取磁力计数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆