0

我正在尝试使用 cordova 指南针运行示例应用程序,但每次使用错误代码 3 调用错误回调。

我使用cordova V4.0,当然我添加了插件org.apache.cordova.device-orientation。这是代码:

<!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">

    // The watch id references the current `watchHeading`
    var gWatchID = null;

    // Wait for device API libraries to load
    document.addEventListener("deviceready", onDeviceReady, false);

    // device APIs are available
    function onDeviceReady() {
        startWatch();
    }

    // Start watching the compass
    function startWatch() {
        // Update compass every 3 seconds
        var options = { frequency: 3000 };

        if (!gWatchID)
            gWatchID = navigator.compass.watchHeading(onSuccess, onError, options);
    }

    // Stop watching the compass
    function stopWatch() {
        if (gWatchID) {
            navigator.compass.clearWatch(watchID);
            gWatchID = null;
        }
    }

    // onSuccess: Get the current heading
    function onSuccess(heading) {
        var element = document.getElementById('heading');
        element.innerHTML = 'Heading: ' + heading.magneticHeading;
    }

    // onError: Failed to get the heading
    function onError(compassError) {
        alert('Compass error: ' + compassError.code);
    }

    </script>
  </head>
  <body>
    <div id="heading">Waiting for heading...</div>
    <button onclick="startWatch();">Start Watching</button>
    <button onclick="stopWatch();">Stop Watching</button>
  </body>
</html>

该应用程序已成功构建、部署和启动。但是当它启动时,只显示错误代码3。

根据文档,仅定义了两个错误代码: CompassError.COMPASS_INTERNAL_ERR = 0; CompassError.COMPASS_NOT_SUPPORTED = 20;

所以我想知道错误代码3是什么意思?我做错了什么?

谢谢你的回答,但丁

4

1 回答 1

2

您的设备没有磁传感器,或者供应商没有在操作系统中实现对它的支持。

查看面向设备的插件的Android源代码,启动代码是这样写的(为了简洁而修改):

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);

不知道他们为什么在那里编写自己的错误代码(public static int ERROR_FAILED_TO_START = 3),但实际上他们应该按照COMPASS_NOT_SUPPORTED文档中的定义进行报告。

于 2015-03-16T22:10:22.940 回答