Cordova compass API (navigator.compass.watchHeading) not working (error code 3)

匿名 (未验证) 提交于 2019-12-03 00:44:02

问题:

I'm trying to get a sample app running using the cordova compass, but each time the error callback is called with error code 3.

I use cordova V4.0 and of course I added the plugin org.apache.cordova.device-orientation. Here's the code:

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

The app is built, deployed and started successfully. But when it is started, only error code 3 is displayed.

According to the documentation only two error code are definded: CompassError.COMPASS_INTERNAL_ERR = 0; CompassError.COMPASS_NOT_SUPPORTED = 20;

So I wonder what's the meaning error code 3? And what am I doing wrong?

Thanks for your answers, Dante

回答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.



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