简体   繁体   中英

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

<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).
My onSuccess callback is never called however, any ideas why?

FYI:

cordova.js and js/index.js are both generated files unedited and where created by the cli (for the specific platform)

Also take note, my onDeviceReady is never called either, but the Start Watch button does call startWatch

I am also using sensorsimulator-2.0-rc1 to fake the values of the accelerometer. 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.

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

so i have a solution for now (code works on a real device)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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