iOS Question Calibrated Magnetometer data

Erel

Administrator
Staff member
Licensed User
Add this OBJC code:
B4X:
#if OBJC

- (void)startMotion:(CMMotionManager*)manager {
   [manager startDeviceMotionUpdatesUsingReferenceFrame: CMAttitudeReferenceFrameXArbitraryCorrectedZVertical];
}

- (NSArray*) GetCalibratedMagnetometer:(CMMotionManager*)manager {
   CMCalibratedMagneticField cfield = manager.deviceMotion.magneticField;
   CMMagneticField mf = cfield.field;
   return @[@(mf.x), @(mf.y), @(mf.z)];
}
#End If

2. Start Motion with this code instead of Motion.Start:
B4X:
Dim no As NativeObject = Me
no.RunMethod("startMotion:", Array(mot)) 'mot is the global motion object

3. Get the values with this code:
B4X:
Dim CalibratedValues As List = no.RunMethod("GetCalibratedMagnetometer:", Array(mot))
Log($"x: ${CalibratedValues.Get(0)}
y: ${CalibratedValues.Get(1)}
z: ${CalibratedValues.Get(2)}
"$)

Note that there are 3 possible references: https://developer.apple.com/reference/coremotion/cmattitudereferenceframe?language=objc
(it will not work with CMAttitudeReferenceFrameXArbitraryZVertical).
 
Last edited:
Upvote 0

JackKirk

Well-Known Member
Licensed User
Erel,

Thanks for this.

I'm using this in my B4A.Phonesensors to B4i.Motion porting class:

https://www.b4x.com/android/forum/threads/class-xphonesensor-class-motion-object-as-per-b4a.79810/

I have done some preliminary testing of all 3 reference frames:

CMAttitudeReferenceFrameXArbitraryCorrectedZVertical
CMAttitudeReferenceFrameXMagneticNorthZVertical
CMAttitudeReferenceFrameXTrueNorthZVertical

as per your pointer to the Apple documentation in the previous post.

It seems that CMAttitudeReferenceFrameXMagneticNorthZVertical is giving better correlation to my Android stuff but I'm not convinced.

If I google these all I get is the reference you gave (which is not very useful IMO) and a whole lot of others that seem to be just parroting it.

I also tried googling to see what they do in Android - without success.

Q1. Do you have any insights beyond the Apple documentation?

Q2. Do you have any insights into what Android does?

Thanks...
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Erel, could you tell me what the Objective C is for the StartMagnetometer method.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Erel,

Could you confirm this objective C will generate uncalibrated magnetometer readings as per your Motion objects StartMagnetometer/StopMagnetometer/GetMagnetometer:
B4X:
- (void) startMagnetometer: (CMMotionManager*) manager {
   [manager startMagnetometerUpdates];
}

- (NSArray*) getUncalMagField: (CMMotionManager*) manager {
    CMMagnetometerData *magdata = manager.magnetometerData;
    CMMagneticField uncalmagfld = magdata.magneticField;
    return @[@(uncalmagfld.x), @(uncalmagfld.y), @(uncalmagfld.z)];
}
It seems to be working...
 
Upvote 0
Top