isMagnetometerAvailable always return false on Apple Watch series 6

96 views Asked by At

I'm writing the code to access motion data by CMMotionManager(). I can get Acceleration, User accel, Gravity, Attitude, and Rotation rate correctly. Now I want to add the Magnetometer reading function. Follow to existing codes, the section looks like

import CoreMotion
...
let motionManager = CMMotionManager()
...
motionManager.deviceMotionUpdateInterval = 1/10 //in s
motionManager.accelerometerUpdateInterval = 1/128 //in s
motionManager.magnetometerUpdateInterval = 1/100 // in s
...
if motionManager.isMagnetometerAvailable {
    let handler:CMMagnetometerHandler = {(magnetoData:CMMagnetometerData?, error: Error?) -> Void in
    self.read1.setText(String(format: "%.2f", magnetoData!.magneticField.x))
    self.read2.setText(String(format: "%.2f", magnetoData!.magneticField.y))
    self.read3.setText(String(format: "%.2f", magnetoData!.magneticField.z))
    }
    motionManager.startMagnetometerUpdates(to: OperationQueue.main, withHandler: handler)
}

The similar codes for other sensor data (Acceleration, User accel, Gravity, Attitude, and Rotation rate) work well. However, the codes above stop at "motionManager.isMagnetometerAvailable" because it always returns false to skip the following codes.

The device I test is the iPhone 11 Pro / Apple Watch Series 6.

Thank you.

0

There are 0 answers