Cordova Compass API is giving error code 3

喜夏-厌秋 提交于 2021-01-27 12:22:01

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/29053012/cordova-compass-api-is-giving-error-code-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!