Skip to main content

@napacitor/motion

Unified sensor plugin wrapping the five sensor APIs described in the .NET MAUI Accelerometer documentation:

SensorMAUI native classEvent name
AccelerometerAccelerometeraccelerometerReading
GyroscopeGyroscopegyroscopeReading
MagnetometerMagnetometermagnetometerReading
CompassCompasscompassReading
BarometerBarometerbarometerReading

Installation​

To use npm:

npm install @napacitor/motion

To use yarn:

yarn add @napacitor/motion

Sync native files:

npx nap sync

Usage​

import { Motion, SensorSpeed } from '@napacitor/motion';

// ── Support detection (five sensors) ──────────────────────────
const accel = (await Motion.isAccelerometerSupported()).supported;
const gyro = (await Motion.isGyroscopeSupported()).supported;
const magneto = (await Motion.isMagnetometerSupported()).supported;
const compass = (await Motion.isCompassSupported()).supported;
const baro = (await Motion.isBarometerSupported()).supported;

// ── Accelerometer ─────────────────────────────────────────────
if (accel) {
await Motion.startAccelerometer({ speed: SensorSpeed.Game });
const handle = await Motion.addListener('accelerometerReading', ({ x, y, z }) => {
console.log(`Acceleration: x=${x} y=${y} z=${z} (G)`);
});
await Motion.stopAccelerometer();
await handle.remove();
}

// ── Gyroscope ─────────────────────────────────────────────────
if (gyro) {
await Motion.startGyroscope({ speed: SensorSpeed.Ui });
await Motion.addListener('gyroscopeReading', ({ x, y, z }) => {
console.log(`Angular velocity: x=${x} y=${y} z=${z} (rad/s)`);
});
await Motion.stopGyroscope();
}

// ── Magnetometer ──────────────────────────────────────────────
if (magneto) {
await Motion.startMagnetometer();
await Motion.addListener('magnetometerReading', ({ x, y, z }) => {
console.log(`Magnetic field: x=${x} y=${y} z=${z} (µT)`);
});
await Motion.stopMagnetometer();
}

// ── Compass ───────────────────────────────────────────────────
if (compass) {
await Motion.startCompass();
await Motion.addListener('compassReading', ({ headingMagnetic, headingTrue, calibration }) => {
console.log(`Magnetic north: ${headingMagnetic}° True north: ${headingTrue}° Calibration: ${calibration}`);
});
await Motion.stopCompass();
}

// ── Barometer ─────────────────────────────────────────────────
if (baro) {
await Motion.startBarometer();
await Motion.addListener('barometerReading', ({ pressure }) => {
console.log(`Pressure: ${pressure} hPa`);
});
await Motion.stopBarometer();
}

// Remove all listeners
await Motion.removeAllListeners();

How It Works​

Wraps the APIs described in the .NET MAUI Accelerometer / Gyroscope / Magnetometer / Compass / Barometer documentation: each sensor manages its subscription independently — the native ReadingChanged event is subscribed when the first JS listener registers and unsubscribed when the last one is removed; start*() calls Default.Start(speed) and stop*() calls Default.Stop(). Sample rates map to the MAUI SensorSpeed enum (DEFAULT ~200ms / UI ~60ms / GAME ~40ms / FASTEST ~20ms). On web: accelerometer/gyroscope use DeviceMotionEvent (requires HTTPS or localhost), compass uses DeviceOrientationEvent (requires user permission on iOS), and magnetometer/barometer have no standard browser API.

Permissions​

Android​

No additional configuration required (sensor access needs no permission declaration).

iOS​

No additional configuration required.

Windows​

No additional configuration required.

API​

isAccelerometerSupported() / isGyroscopeSupported() / isMagnetometerSupported() / isCompassSupported() / isBarometerSupported()​

Checks whether the corresponding sensor is available.

Returns: Promise<SensorSupportedResult>

startAccelerometer(options?) / startGyroscope(options?) / startMagnetometer(options?) / startCompass(options?) / startBarometer(options?)​

Starts listening to the corresponding sensor (throws UNSUPPORTED when the device does not support it).

ParamType
optionsStartSensorOptions

Returns: Promise<void>

stopAccelerometer() / stopGyroscope() / stopMagnetometer() / stopCompass() / stopBarometer()​

Stops the corresponding sensor.

Returns: Promise<void>

addListener('accelerometerReading', callback) / addListener('gyroscopeReading', callback) / addListener('magnetometerReading', callback)​

Listens for three-axis sensor readings (see the table below for event names and callback types).

ParamType
eventName'accelerometerReading' / 'gyroscopeReading' / 'magnetometerReading'
callbackAccelerometerCallback / GyroscopeCallback / MagnetometerCallback

Returns: Promise<PluginListenerHandle> & PluginListenerHandle

addListener('compassReading', callback)​

Listens for compass readings.

ParamType
eventName'compassReading'
callbackCompassCallback

Returns: Promise<PluginListenerHandle> & PluginListenerHandle

addListener('barometerReading', callback)​

Listens for barometer readings.

ParamType
eventName'barometerReading'
callbackBarometerCallback

Returns: Promise<PluginListenerHandle> & PluginListenerHandle

removeAllListeners()​

Removes all listeners of this plugin (unsubscribes native events for every sensor; call stop*() separately to stop sensors that are still running).

Returns: Promise<void>

Interfaces​

SensorSupportedResult​

PropTypeDescription
supportedbooleanWhether the sensor is available.

StartSensorOptions​

PropTypeDescription
speedSensorSpeedSample rate (default DEFAULT): DEFAULT (~200ms / 5Hz) / UI (~60ms / ~17Hz) / GAME (~40ms / 25Hz) / FASTEST (~20ms / 50Hz).

AxisReading​

Three-axis reading (shared structure for accelerometer / gyroscope / magnetometer).

PropTypeDescription
xnumberX-axis component (unit depends on the sensor: accelerometer G / gyroscope rad/s / magnetometer µT).
ynumberY-axis component (same as above).
znumberZ-axis component (same as above).

CompassReading​

PropTypeDescription
headingMagneticnumberMagnetic north heading (0-360°, 0 = north, increasing clockwise).
headingTruenumberTrue north heading. MAUI 10's CompassData only provides the magnetic heading, so the native side always returns -1 (unsupported).
calibrationCompassCalibrationCalibration accuracy level.

BarometerReading​

PropTypeDescription
pressurenumberAtmospheric pressure (hPa).