Feihua Fang
Feihua Fang

Reputation: 1101

Why the values of magneticField.field are fixed in zero?

I need to sample the 3-axis magnetic field,3-axis acceleration and 3-axis rotation rate,so i use the DeviceMotion,and here is my code:

 if manager.deviceMotionAvailable {
        if manager.magnetometerAvailable {
        manager.magnetometerUpdateInterval=0.01
        manager.deviceMotionUpdateInterval = 0.01
             manager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: {(data: CMDeviceMotion?, error: NSError?) -> Void in
         if(standard_time) {
         let data = [
                    "ax":data!.userAcceleration.x,"ay":data!.userAcceleration.y,"az":data!.userAcceleration.z,                      
                    "gax":data!.gravity.x,"gay":data!.gravity.y,"gaz":data!.gravity.z,
                    "gx":data!.rotationRate.x,"gy":data!.rotationRate.y,"gz":data!.rotationRate.z,
                    "mmx":data!.magneticField.field.x,"mmy":data!.magneticField.field.y,"mmz":data!.magneticField.field.z,
                   "roll":data!.attitude.roll, "pitch":data!.attitude.pitch, "yaw":data!.attitude.yaw]
                 }
                )}
  }
 }

However,the values of 3-axis magnetic field I got are fixed in zero,no matter how hard I try to move my iphone. Could you please help me ???

Upvotes: 0

Views: 139

Answers (1)

CodeBender
CodeBender

Reputation: 36620

You appear to be calling the wrong method to get magnetometer data.

    if manager.deviceMotionAvailable {
        if manager.magnetometerAvailable {
            manager.magnetometerUpdateInterval = 0.01
            manager.deviceMotionUpdateInterval = 0.01
            manager.startMagnetometerUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { (data: CMMagnetometerData?, error: NSError?) in
                // Do something with data
                print(data)
            })

        }
    }

Upvotes: 1

Related Questions