简体   繁体   English

Cordova cordova-plugin-device-motion插件不调用回调

[英]Cordova cordova-plugin-device-motion plugin not calling callback

I used the Cordova CLI to create my project, added the plugin cordova-plugin-device-motion Then followed the simple tutorial to try and get values 我使用Cordova CLI来创建我的项目,添加了插件cordova-plugin-device-motion,然后按照简单的教程尝试获取值

<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <!--<link rel="stylesheet" type="text/css" href="css/index.css">-->
        <title>Rotations</title>
    </head>
    <body>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <!--<script type="text/javascript" src="js/rotationHandler.js"></script>-->
        <script type="text/javascript" charset="utf-8">
            // The watch id references the current `watchAcceleration`
            var watchID = null;

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

            // device APIs are available
            //
            function onDeviceReady() {
                console.log('OGDEBUG onDeviceReady');
                startWatch();
            }

            // Start watching the acceleration
            //
            function startWatch() {
                // Update acceleration every 1 seconds
                var options = { frequency: 1000 };
                try{
                    watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
                }catch(ex){
                    console.log('OGDEBUG startWatch error '+ex);
                }
                console.log('OGDEBUG startWatch');
            }

            // Stop watching the acceleration
            function stopWatch() {
                try{
                    if (watchID) {
                        navigator.accelerometer.clearWatch(watchID);
                        watchID = null;
                    }
                }catch(ex){
                    console.log('OGDEBUG stopWatch error '+ex);
                }
                console.log('OGDEBUG stopWatch');
            }

            // onSuccess: Get a snapshot of the current acceleration
            //
            function onSuccess(acceleration) {    
                var accelerationString = 
                        'Acceleration X: ' + acceleration.x + '\n' +
                              'Acceleration Y: ' + acceleration.y + '\n' +
                              'Acceleration Z: ' + acceleration.z + '\n' +
                              'Timestamp: '      + acceleration.timestamp + '\n';

                console.log('OGDEBUG onSuccess '+accelerationString);
            }

            // onError: Failed to get the acceleration
            //
            function onError() {
                alert('onError!');
                console.log('OGDEBUG onError' );
            }

            function buttonTapStart() {
                startWatch();
                console.log('OGDEBUG buttonTap (index)');
            }

            function buttonTapStop() {
                stopWatch();
                console.log('OGDEBUG buttonTap (index)');
            }
         </script>

        <div>
            <button onclick="buttonTapStart()">Start Watch</button>
            <button onclick="buttonTapStop()">Stop Watch</button>
        </div>
    </body>
</html>

None of my catch/error log statements get called so it seems to be working (prior I have received navigator is undefined but moved stuff around and it seems to be good now). 我的catch / error日志语句均未调用,因此似乎正常(在我收到navigator器之前未定义,但移动了东西,现在看起来不错)。
My onSuccess callback is never called however, any ideas why? 我的onSuccess回调从未调用过,为什么有什么主意?

FYI: 仅供参考:

cordova.js and js/index.js are both generated files unedited and where created by the cli (for the specific platform) cordova.js和js / index.js都是未经cli生成的文件,并且都是由cli创建的(针对特定平台)

Also take note, my onDeviceReady is never called either, but the Start Watch button does call startWatch 还要注意,我的onDeviceReady也从未调用过,但是“开始监视”按钮确实会调用startWatch

I am also using sensorsimulator-2.0-rc1 to fake the values of the accelerometer. 我还使用sensorsimulator-2.0-rc1伪造了加速度计的值。 Regardless though (even if thats not working, though I have no reason to doubt that it does, and its own test work) I would think my onSuccess would be called with 0 values. 尽管不管(即使那是行不通的,尽管我没有理由怀疑它是否有效以及它自己的测试工作),但我还是认为我的onSuccess会被调用为0值。

after alot of troubleshooting and help from other forums I have found no way to successfully get this working. 经过大量的故障排除和其他论坛的帮助后,我发现没有办法成功进行此工作。

However I did get my droid x to 4.4.4 and was able to test on it and it works 但是我确实将droid x设置为4.4.4,并且能够对其进行测试,并且可以正常工作

so i have a solution for now (code works on a real device) 所以我现在有一个解决方案(代码可以在真实设备上运行)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM