4

This question is already asked on stackoverflow here but I didn't found any answer to it, so I raised again this. Please can anyone able reply for this?

My code is as follows:

<!DOCTYPE html>
<html>
 <head>
<title>Compass Example</title>

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() 
{
    navigator.compass.getCurrentHeading(onSuccess, onError);
}

function onSuccess(heading)
 {
    alert('Heading: ' + heading.magneticHeading);
}

function onError(compassError) 
{
    alert('Compass Error: ' + compassError.code);
}

</script>
  </head>
 <body>
<h1>Example</h1>
<p>getCurrentHeading</p>
 </body>
</html>
Community
  • 1
  • 1
Laxminarayan
  • 188
  • 2
  • 16

1 Answers1

3

Either your device does not have a magnetic sensor, or the vendor has not implemented support for it in the OS.

Looking at the Android source code for the device-orientation plugin, the startup code is written like this (modified for brevity):

List<Sensor> list = this.sensorManager.getSensorList(Sensor.TYPE_ORIENTATION);

// If found, then register as listener
if (list != null)
    this.setStatus(CompassListener.STARTING);

// If error, then set status to error
else
    this.setStatus(CompassListener.ERROR_FAILED_TO_START);

Not sure why they made up their own error code there (public static int ERROR_FAILED_TO_START = 3), but really they should be reporting COMPASS_NOT_SUPPORTED as defined in the documentation.

Mike Dailor
  • 1,226
  • 8
  • 16