MetaWear Magnetometer Code (Swift)

Hi,

I'm unable to get the code for Magnetometer. Here's my code.


        device.gyro?.dataReadyEvent.startNotificationsWithHandlerAsync { (obj, error) in

            

            //print(obj)

            }.success { result in

                print("Successfully subscribed")

            }.failure { error in

                print("Error on subscribe: \(error)")

        }

        

        device.magnetometer?.dataReadyEvent.startNotificationsWithHandlerAsync { (obj, error) in

            

            //print(obj)

            }.success { result in

                print("Successfully subscribed")

            }.failure { error in

                print("Error on subscribe: \(error)")

        }


Using this code, I'm able to get the data for Gyro, but not for Magnetometer. I'm getting the following error "value of type MBLMagnetometer has no member 'dataReadyEvent'".


How to get the data for magnetometer?

Comments

  • edited June 2016
    What MetaWear board are you using and what is the firmware version it is running?
  • I'm using MetaWear C CPRO board (wireless) and firmware version is 1.1.3
  • Check out the MagnetometerBMM150 section in the Objective C documentation.  As I said in a previous pose, you can refer to the Objective C documentation for API details.  Most simple usage questions will already be answered in the documentation.
  • edited October 2016
    I have tried reading the Objective C documentation and tried to convert Objective C code into Swift. But still, I am not getting the magnetometer data. Here is my code in Swift 2.2

    device.magnetometer?.magneticField!.readAsync().success({result in

                print(result)

            })


  • Hi...I am also trying to get data for magnetometer by using Swift language for my project but not able to figure out the problem. I have tried the code which you have posted above and tried to find the problem with that also but still it does not work. 
  • Can first you check with the metawear app that the magnetometer is functioning correctly just to ensure that the board is working?
  • Magnetometer is working fine in metawear app. 

    I also tried this code, but still no success

    device.magnetometer?.magneticField?.periodicReadWithPeriod(1).startNotificationsWithHandlerAsync { obj in

                print("magnetometer data")

                print(obj)

                }.success{result in

            print("Magno successfully subscribed")

        }

  • gulshan,

    The magneticField property is not yet implemented, the documentation was just updated to reflect this.  You will need to use the periodicMagneticField property:

    Here is the swfit 3.0 version:

    if let magnetometer = self.device.magnetometer as? MBLMagnetometerBMM150 {
        magnetometer.periodicMagneticField.startNotifications(handlerAsync: { (obj, error) in
            print(obj)
        })
    }

This discussion has been closed.