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
Param | Type |
---|---|
eventName | "accel" |
listenerFunc |
|
Returns:
PluginListenerHandle
addListener(...)
addListener(eventName: 'orientation', listenerFunc: (event: MotionOrientationEventResult) => void) => PluginListenerHandle
Listen for device orientation change (compass heading, etc.)
Param | Type |
---|---|
eventName | "orientation" |
listenerFunc |
|
Returns:
PluginListenerHandle
removeAllListeners()
removeAllListeners() => void
Remove all native listeners for this plugin
Interfaces
PluginListenerHandle
Prop | Type |
---|---|
remove | () => void |
MotionEventResult
Prop | Type |
---|---|
acceleration | { x: number; y: number; z: number; } |
accelerationIncludingGravity | { x: number; y: number; z: number; } |
rotationRate | { alpha: number; beta: number; gamma: number; } |
interval | number |
MotionOrientationEventResult
Prop | Type |
---|---|
alpha | number |
beta | number |
gamma | number |