Skip to main content
Version: v2

Motion

The Motion API tracks accelerometer and device orientation (compass heading, etc.)

Permissions​

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):

myButton.addEventListener('click', async () => {
try {
await DeviceMotionEvent.requestPermission();
} catch (e) {
// Handle error
return;
}

// Once the user approves, can start listening:
const { Motion } = Capacitor.Plugins;
Capacitor.Plugins.Motion.addListener('accel', (event) => {});
});

Example​

const { Motion } = Capacitor.Plugins;
Motion.addListener('accel', (event) => {});

See the DeviceMotionEvent API to understand the data supplied in event.

API​

addListener(...)​

addListener(eventName: 'accel', listenerFunc: (event: MotionEventResult) => void) => PluginListenerHandle

Listen for accelerometer data

ParamType
eventName"accel"
listenerFunc
(event: MotionEventResult) => void

Returns:

PluginListenerHandle


addListener(...)​

addListener(eventName: 'orientation', listenerFunc: (event: MotionOrientationEventResult) => void) => PluginListenerHandle

Listen for device orientation change (compass heading, etc.)

ParamType
eventName"orientation"
listenerFunc
(event: MotionOrientationEventResult) => void

Returns:

PluginListenerHandle


removeAllListeners()​

removeAllListeners() => void

Remove all native listeners for this plugin


Interfaces​

PluginListenerHandle​

PropType
remove() => void

MotionEventResult​

PropType
acceleration{ x: number; y: number; z: number; }
accelerationIncludingGravity{ x: number; y: number; z: number; }
rotationRate{ alpha: number; beta: number; gamma: number; }
intervalnumber

MotionOrientationEventResult​

PropType
alphanumber
betanumber
gammanumber