@napacitor/motion
Unified sensor plugin wrapping the five sensor APIs described in the .NET MAUI Accelerometer documentation:
| Sensor | MAUI native class | Event name |
|---|---|---|
| Accelerometer | Accelerometer | accelerometerReading |
| Gyroscope | Gyroscope | gyroscopeReading |
| Magnetometer | Magnetometer | magnetometerReading |
| Compass | Compass | compassReading |
| Barometer | Barometer | barometerReading |
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).
| Param | Type |
|---|---|
options | StartSensorOptions |
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).
| Param | Type |
|---|---|
eventName | 'accelerometerReading' / 'gyroscopeReading' / 'magnetometerReading' |
callback | AccelerometerCallback / GyroscopeCallback / MagnetometerCallback |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
addListener('compassReading', callback)
Listens for compass readings.
| Param | Type |
|---|---|
eventName | 'compassReading' |
callback | CompassCallback |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
addListener('barometerReading', callback)
Listens for barometer readings.
| Param | Type |
|---|---|
eventName | 'barometerReading' |
callback | BarometerCallback |
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
| Prop | Type | Description |
|---|---|---|
supported | boolean | Whether the sensor is available. |
StartSensorOptions
| Prop | Type | Description |
|---|---|---|
speed | SensorSpeed | Sample 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).
| Prop | Type | Description |
|---|---|---|
x | number | X-axis component (unit depends on the sensor: accelerometer G / gyroscope rad/s / magnetometer µT). |
y | number | Y-axis component (same as above). |
z | number | Z-axis component (same as above). |
CompassReading
| Prop | Type | Description |
|---|---|---|
headingMagnetic | number | Magnetic north heading (0-360°, 0 = north, increasing clockwise). |
headingTrue | number | True north heading. MAUI 10's CompassData only provides the magnetic heading, so the native side always returns -1 (unsupported). |
calibration | CompassCalibration | Calibration accuracy level. |
BarometerReading
| Prop | Type | Description |
|---|---|---|
pressure | number | Atmospheric pressure (hPa). |