7 Replies Latest reply on Apr 22, 2016 8:42 AM by kerrishotts

    Getting-less-of-a-Newbie

    ianhowse Level 1

      Hi,

       

      I thought I'd put some 'shake' functionality in my under-development app, so started by installing the plugin thru the CLI -

      cordova plugin add cordova-plugin-device-motion)

       

      checked out the config.xml and yep, found

      <gap:plugin name="org.apache.cordova.device-motion"/>

       

      But as soon as I add the following line into the onDeviceReady() function, the app (on iOS) hangs -

       

      var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);

       

      remove that line, and all good.

       

      What am I doing wrong, or what else do I need to install / configure?

       

      Cheers.

       

      PS. <script src="cordova.js"></script> is in the code as the first load

        • 1. Re: Getting-less-of-a-Newbie
          kerrishotts Adobe Community Professional

          If you're using the CLI, then <gap:plugin....> does nothing. Better to use the following when installing plugins:

           

          cordova plugin add cordova-plugin-device-motion --save

           

          This will take care of modifying config.xml for you (and will use the correct <plugin...> tag). Bonus: should you make a build and the plugin isn't installed, it will be installed automatically.

           

          I'm guessing that by "hang" you mean that your app never starts. This would probably be because the plugin hasn't been loaded (in your case), and so this line generates an error. Since JS likes to quit on the first error, nothing else ever occurs.

          • 2. Re: Getting-less-of-a-Newbie
            ianhowse Level 1

            Thanks for that

             

            I re-installed the plugin using the command line you mentioned, but nothing happens - below is the relevant line of code ,,,

             

            alert('in'); WatchID = navigator.accelerometer.watchAcceleration(onShake, onError, 300); alert('back')

             

            function onShake()

            { alert('I got shook')}

             

            function onError()

            { alert('AccError')}

             

            ......the 'in' happens, but the 'back' doesn't and no other alerts on shaking.

             

            What am I doing wrong, or what else do I need to install / configure?

             

            Cheers, Ian.

            • 3. Re: Getting-less-of-a-Newbie
              Chris W. Griffith Adobe Community Professional

              There is another option to consider, does your device have an accelerometer. You should check that it does. I have seen some low end Chinese phones without one.

               

              Also are you certain that the  device ready event fires? That will confirm the webview to the device layer is active.

               

              Chris

              • 4. Re: Getting-less-of-a-Newbie
                ianhowse Level 1

                Hi again Chris,

                 

                Yep - it's a `proper` iPhone5

                 

                Positive onDeviceReady fires - the App does a setTimeout loop waiting for a flag I set to be true, then continues - and this it does.

                 

                Cheers, Ian.

                • 5. Re: Getting-less-of-a-Newbie
                  Chris W. Griffith Adobe Community Professional

                  Ok, that device is ok

                   

                  Wonder if the fact the success callback doesn't have an input parameter and that is causing it to fail

                   

                  function onSuccess(acceleration) {

                      alert('Acceleration X: ' + acceleration.x + '\n' +

                            'Acceleration Y: ' + acceleration.y + '\n' +

                            'Acceleration Z: ' + acceleration.z + '\n' +

                            'Timestamp: '      + acceleration.timestamp + '\n');

                  };

                  • 6. Re: Getting-less-of-a-Newbie
                    ianhowse Level 1

                    Thanks.

                    Doh!!!!!

                     

                    I changed to -

                     

                    function onShake(acceleration) {

                        alert('Acceleration X: ' + acceleration.x + '\n' +

                              'Acceleration Y: ' + acceleration.y + '\n' +

                              'Acceleration Z: ' + acceleration.z + '\n' +

                              'Timestamp: '      + acceleration.timestamp + '\n');}

                     

                    left -

                    alert('in'); AccID = navigator.accelerometer.watchAcceleration(onShake, onError, 300); alert('out')

                     

                    still got the 'in' but no 'out' :[

                     

                    UPDATE: In working an another problem I noticed the Debugger Console in Firefox is saying -

                     

                    TypeError: navigator.accelerometer is undefined

                     

                    Does this help anybody tell me where I've gone wrong?

                     

                    SOLVED!

                    Problem was the line -

                    AccID = navigator.accelerometer.watchAcceleration(onShake, onError, 300)

                    the 300 should have been an Object not just a number! The following works

                    AccID = navigator.accelerometer.watchAcceleration(onShake, onError, {frequency:300})

                     

                    Thanks all for your interest/help

                    1 person found this helpful
                    • 7. Re: Getting-less-of-a-Newbie
                      kerrishotts Adobe Community Professional

                      Glad you got it working! I totally missed that you weren't passing an object there, and I've used that API before.