1

I have an ionic capacitor android app and I would like to record orientation and acceleration of the device, but after adding event listeners, they are not triggering.

Code:

import { Motion } from '@capacitor/motion';

export default class MotionService {

async startRecording() {
    try {
        await DeviceMotionEvent.requestPermission();
    } catch (e) {
        console.error(e);
    }
    Motion.addListener('accel', event => {
        console.log('Device motion event:', event);
    });
}}

package.json:

  "dependencies": {
    "@capacitor/android": "^3.1.1",
    "@capacitor/app": "^1.0.2",
    "@capacitor/core": "3.1.1",
    "@capacitor/filesystem": "^1.0.2",
    "@capacitor/haptics": "^1.0.2",
    "@capacitor/keyboard": "^1.0.2",
    "@capacitor/motion": "^1.0.2",
    "@capacitor/status-bar": "^1.0.2",
    "@ionic/vue": "^5.4.0",
    "@ionic/vue-router": "^5.4.0",
    "chart.js": "^3.4.1",
    "core-js": "^3.6.5",
    "vue": "^3.0.0-0",
    "vue-router": "^4.0.0-0"
  },

I am trying to test it by running: "ionic capacitor run android -l --host=MY_IP", emulator device is Pixel 3 API 30, then open chrome://inspect/#devices -> inspect to see console.logs and trying to change values in the option window of emulator: enter image description here

But I don't see any logs. I would appreciate some help :)

Pat
  • 71
  • 1
  • 5

2 Answers2

0

Silly question but ... Do you call startRecording function?

I checked this feature and It works fine on the @ionic/angular project. I don't think that there is some difference for @ionic/vue.

Moreover, I checked the documentation for @capacitor/motion. There are some points which need to keep in mind:

This plugin is currently implemented using Web APIs. Most browsers require permission before using this API. To request permission, prompt the user for permission on any user-initiated action (such as a button click):

As I understand this plugin not using the native part. And seems requestPermission call is not needed (at least for Android).

Next point. DeviceMotionEvent is an experimental feature in the browser. So, It can be changing in some way in the future.

Anyway, you can also try cordova plugins if problems continue: Device Motion and Device Orientation

Danil Prokhorenko
  • 1,052
  • 1
  • 10
  • 26
0

Actually on Android you don't need to request permissions, just add listeners works

import { Motion } from '@capacitor/motion';
export default class MotionService {

async startRecording() {
    Motion.addListener('accel', event => {
        console.log('Device motion event:', event);
    });
}}