The Motion API tracks accelerometer and device orientation (compass heading, etc.)
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 => {});
});
const { Motion } = Capacitor.Plugins;
Motion.addListener('accel', event => {});
See the
DeviceMotionEvent API to understand the data supplied in
event
.
addListener(eventName: 'accel', listenerFunc: (event: MotionEventResult) => void) => PluginListenerHandle
Listen for accelerometer data
Param | Type |
---|---|
eventName |
"accel" |
listenerFunc |
(event: MotionEventResult) => void |
Returns:
PluginListenerHandle
addListener(eventName: 'orientation', listenerFunc: (event: MotionOrientationEventResult) => void) => PluginListenerHandle
Listen for device orientation change (compass heading, etc.)
Param | Type |
---|---|
eventName |
"orientation" |
listenerFunc |
(event: MotionOrientationEventResult) => void |
Returns:
PluginListenerHandle
removeAllListeners() => void
Remove all native listeners for this plugin
Prop | Type |
---|---|
remove |
() => void |
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 |
Prop | Type |
---|---|
alpha |
number |
beta |
number |
gamma |
number |