1

I am writing code to detect magnet. I am doing this through this code:

let manager = CMMotionManager()

if manager.isMagnetometerAvailable {
manager.startMagnetometerUpdates(to: OperationQueue.main) { (data, error) in    
        let bField = sqrt((pow(data?.magneticField.x ?? 0, 2) + pow(data?.magneticField.y ?? 0, 2) + pow(data?.magneticField.z ?? 0, 2)))
        print(bField)    
    }
}

problem is that it is printing value above 250 when there is no magnet. This value should be around 50uT if there is no magnet around. Is it raw data? If it is raw data how can I get original data?

staticVoidMan
  • 19,275
  • 6
  • 69
  • 98
Salman Khalid
  • 543
  • 5
  • 23
  • are you trying to say that `data?.magneticField` is not accurate? – staticVoidMan Mar 19 '19 at 12:57
  • yes, its values are not accurate – Salman Khalid Mar 19 '19 at 13:01
  • What are the `x`, `y`, `z` values that you're getting? Also did you check it's [calibration](http://w3cgeek.com/is-apples-iphone-magnetometer-calibration-working-properly.html) – staticVoidMan Mar 19 '19 at 13:16
  • x = 37.316314697265625 :: y = 198.1747283935547 :: z = 186.2473602294922 and I am not check calibration. How can I check for calibration? – Salman Khalid Mar 19 '19 at 13:20
  • Hm... atleast you're getting values :P Anyways, check: [Is Apple’s iPhone magnetometer calibration working properly?](http://w3cgeek.com/is-apples-iphone-magnetometer-calibration-working-properly.html) – staticVoidMan Mar 19 '19 at 13:22

0 Answers0