/cordova-plugin-magnetometer

A magnetic field detector plugin. It displays the raw x, y, and z magnetometer values and a computed magnitude of the magnetic field.

Primary LanguageJava

cordova-plugin-magnetometer

Coin Detector

A magnetic field detector plugin ideal for science toolkit or metal detector apps.

The API can be accessed via single call or by placing a listener that fires a callback function repeatedly.

It displays the raw x, y, and z magnetometer values as well as a computed magnitude of the magnetic field.

See this page for a basic sample cordova app using this plugin.

For an in-depth article try this article I wrote: How to Build a Cross-Platform Metal Detector App

Installation

cordova plugin add https://github.com/sdesalas/cordova-plugin-magnetometer

Methods

  • cordova.plugins.magnetometer.getReading
  • cordova.plugins.magnetometer.watchReadings
  • cordova.plugins.magnetometer.stop

cordova.plugins.magnetometer.getReading

Gets a single reading from the magenetometer sensor.


  cordova.plugins.magnetometer.getReading(
    function success(reading){
      console.log(JSON.stringify(reading)); 
      // Output: {x: 23.113, y:-37.245, z:6.172, magnitude: 44.266}
    }, 
    function error(message){
     console.log(message);
    }
  )

reading object properties:

  • x
  • y
  • z
  • magnitude (calculated total - always positive)

cordova.plugins.magnetometer.watchReadings

Gets regular magnetometer readings sent by the internal sensor, will fire success callback repeatedly.


  var watchID = cordova.plugins.magnetometer.watchReadings(
    function success(reading){
      console.log(JSON.stringify(reading)); 
      // Output: {x: 23.113, y:-37.245, z:6.172, magnitude: 44.266}
    }, 
    function error(message){
     console.log(message);
    }
  )

reading object properties:

  • x
  • y
  • z
  • magnitude (calculated total - always positive)

cordova.plugins.magnetometer.stop

Stops getting readings from the magnetometer sensor. Optional watchID parameter.


  cordova.plugins.magnetometer.stop([watchID])

Supported Platforms

  • iOS
  • Android