我试图获取x,y和z轴的magnometerData。 但是它始终返回0。精度为-1。
这意味着未校准运动。但是我该如何校准呢?
func detect() {
motionManager.startMagnetometerUpdates()
motionManager.startDeviceMotionUpdates()
var motion = CMDeviceMotion()
//var mmd = CMMagnetometerData()
motionManager.showsDeviceMovementDisplay = true
if motionManager.deviceMotion != nil && motionManager.magnetometerData != nil{
motion = motionManager.deviceMotion!
//mmd = self.motionManager.magnetometerData!
print("motion ", motion.magneticField.accuracy.rawValue)
let _magfield = CMCalibratedMagneticField(field: motion.magneticField.field, accuracy: motion.magneticField.accuracy)
print("_magfield ",_magfield.accuracy.rawValue)
let magnetometerData = _magfield.field
x.text = "X: " + String(format:"%f", magnetometerData.x)
y.text = "Y: " + String(format:"%f", magnetometerData.y)
z.text = "Z: " + String(format:"%f", magnetometerData.z)
let _strenght = sqrt(pow(magnetometerData.x, 2) + pow(magnetometerData.y, 2) + pow(magnetometerData.z, 2))
textarea.insertText("\n" + String(format:"%.2f", _strenght))
}