Question

This question has been asked before CMCalibratedMagneticField doesn't update but it wasn't really answered. I am very lost on this one.

I'm using a simple formulation, but field.field.x, y, z are always zero.

if (motionManager.deviceMotionAvailable)
{
    [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]  
                                       withHandler:
       ^(CMDeviceMotion *motionData, NSError *error) {
           CMCalibratedMagneticField field = motionData.magneticField;
           NSLog(@"x: %f  y:%f  z:%f", field.field.x
                                     , field.field.y
                                     , field.field.z);

    }];       
}

Sample output is

2013-07-14 22:18:27.865 TestApp[6678:907] x: 0.000000  y:0.000000  z:0.000000

2013-07-14 22:18:27.869 TestApp[6678:907] x: 0.000000  y:0.000000  z:0.000000

2013-07-14 22:18:27.879 TestApp[6678:907] x: 0.000000  y:0.000000  z:0.000000

I am pretty certain the issue is not the phone as the preceding answer suggested: I am coding for a fairly modern 4S. I have the CoreMotion, CoreLocation, and CoreData frameworks in place -- maybe there is a header file I don't know about that I need to import in the implementation? currently I only seem to need

#import <tgmath.h>

but that is for other stuff.

Any help would be appreciated!

Tim Redfield

Was it helpful?

Solution

Try this instead:

- (void)startDeviceMotionUpdatesUsingReferenceFrame:(CMAttitudeReferenceFrame)referenceFrame 
                                            toQueue:(NSOperationQueue *)queue 
                                        withHandler:(CMDeviceMotionHandler)handler

with any of:

     CMAttitudeReferenceFrameXArbitraryCorrectedZVertical  
     CMAttitudeReferenceFrameXMagneticNorthZVertical  
     CMAttitudeReferenceFrameXTrueNorthZVertical  

but not:

     CMAttitudeReferenceFrameXArbitraryZVertical

(another SO question you missed:CoreMotion - magneticField is always 0)

if you want too much information on magnetometer data, see my answer to this question:In iOS, what is the difference between the Magnetic Field values from the Core Location and Core Motion frameworks?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top