Ionic device motion error

Well today I was trying to add shake event but I found that it is available for ios only… so start working on device motion however I am not able to integrate that as well :(. Anybody who has integrated that please help me out… Already wasted a day :frowning:

I just followed the Ionic Native instructions for installation and I’m using it on android and ios and it works fine. Installing the shake plugin also installs the device motion android plugin:

$ ionic cordova plugin add cordova-plugin-shake 
> cordova plugin add cordova-plugin-shake --save
✔ Running command - done!
Installing "cordova-plugin-shake" for android
Plugin dependency "cordova-plugin-device-motion@1.2.5" already fetched, using that version.
Installing "cordova-plugin-device-motion" for android

...

:clean
:CordovaLib:clean

BUILD SUCCESSFUL

Total time: 1.383 secs
Installing "cordova-plugin-shake" for ios
Adding cordova-plugin-shake to package.json
Saved plugin info for "cordova-plugin-shake" to config.xml

The only change I had to make for my devices was to set the sensitivity down to 10 for my needs.

Thanks saschwarz,
I have installed that as well but when i add the code from ionic native forum to my app…
I start giving me errors…
Can you share your controller code in which you have integrated device motion please…
It will be a great help

You don’t need to integrate the device motion plugin at all. The shake plugin uses it behind the scenes.

  ionViewWillEnter() {
    this.course = new DoubleBoxCourse();  // the instance I want to change when a shake happens
    this.platform.ready().then(() => {
      this._watch = this.shake.startWatch(5).subscribe((event) => {
        this.zone.run(() => this.course.scramble()); // changing it and updating the view
      });
    });
  }

  ionViewDidExit() {
    this._watch.unsubscribe();  // don't know if this is required but might as well.
  }

If you have to directly interact with zones, I would consider that an ionic-native bug. Are you absolutely certain this is necessary?

You might also try removing and adding back your platforms. I’ve had to do that with some plugins.

According to ionic platform, their native shake module is for ios only?

Have you tried this on android device as well?

It was definitely necessary. I never add ngZone unless I need it.

Yes I am using it on Android. The cordova-plugin-shake doesn’t say it doesn’t work on Android it says:

For non-iOS platforms, there is no native component to this plugin but it depends on the device motion plugin (added when this plugin is added).

So cordova-plugin-shake adds device motion for you, then it works on Android.

At least on Android, I don’t think this is true. I just tested a sandbox app with no explicit zone interaction and it worked as expected.

Ok I am goint to try this now…
Thanks Saschwarz :slight_smile:

I just re-tested it on Android and changes don’t always appear unless I use zone.run() explicitly. So it may depend on what changes are made to the tracked object tree w/in the callback.

I can’t see how that is possible. What I did:

export class HomePage {
  shaken = 'nothing jim';
  constructor(public navCtrl: NavController, shake: Shake) {
    shake.startWatch(10).subscribe(() => {
      this.shaken = 'shook me up';
    });
  }
}

<div>{{shaken}}</div>

I just hopped on the forum and thought I could help this person out. I didn’t expect all of this…

You might change your model to:

export class HomePage {
  shaken = 0;
  constructor(public navCtrl: NavController, shake: Shake) {
    shake.startWatch(10).subscribe(() => {
      this.shaken += 1;
    });
  }
}

<div>{{shaken}}</div>

and see if it updates sequentially. In my case, I’m moving nodes around in a tree and the display wouldn’t always update after shaking.

OK, I did and it does. Number goes up on each shake. I think something else is going on in your app, because the fundamental Observable returned by ionic-native seems to be operating in the correct zone.

I made the same change in my app and you are correct - my app’s UI (doesn’t yet) always make it obvious what changed on each shake. Thanks for the clarification.

Rediscovered why I needed the NgZone: retesting on iOS devices and the iOS emulator uncovered they need it.

If you get the time, please check out wkwebview-engine. It solved some problems I had with iOS.

Unfortunately, WKWebView has issues with PKCE style authentication: https://github.com/auth0/auth0-cordova/issues/42

1 Like