motion_sensors_pro 1.0.8
motion_sensors_pro: ^1.0.8 copied to clipboard
Flutter plugin for accessing native shake gesture detection, accelerometer, gyroscope, magnetometer, barometer, 3D attitude, pedometer, and proximity sensors.
Motion Sensors Pro #
A premium, production-grade motion and environmental sensor engine for Flutter. By combining high-efficiency native gesture classification with real-time, low-latency streams for 8 physical hardware sensors, it provides developers with the ultimate API for immersive, device-aware mobile experiences. Engineered with sub-millisecond dynamic intervals, thread-safe memory mapping, and seamless Xcode Simulator mock-shake routing, it represents the new standard for motion tracking in Flutter.

- Shake Gesture Detection (Native classification, 100% Simulator-friendly)
- Accelerometer (Raw 3-axis acceleration including gravity)
- User Accelerometer (Gravity mathematically subtracted by OS)
- Gyroscope (Angular velocity/rotation speed)
- Magnetometer (Ambient magnetic field vector)
- Barometer (Atmospheric pressure & relative altitude changes)
- Device Attitude (Absolute 3D orientation: Roll, Pitch, Yaw via Sensor Fusion)
- Pedometer (Low-power hardware step counter updates)
- Proximity Sensor (Face detection/screen proximity monitoring)
๐ฑ Platform Support #
| Android | iOS | macOS | Web | Linux | Windows |
|---|---|---|---|---|---|
| โ | โ | โ * | โ * | โ | โ |
* macOS: Exposes safe fallback stream handlers that emit descriptive unsupported sensor exceptions instead of raising missing plugin channel registration errors. Programmatic mock triggers remain fully operational for unit/widget testing.
* Web: Supported on mobile web browsers (Safari, Chrome, etc.) using HTML5 DeviceMotion & DeviceOrientation APIs. Setting sampling rate/interval is currently unsupported on web due to browser constraints.
โจ Why motion_sensors_pro is Better than sensors_plus #
| Feature | sensors_plus |
motion_sensors_pro |
|---|---|---|
| Shake Gesture detection | โ None (must parse streams manually in Dart) | โ Native classification, debounced and ready |
| Xcode Simulator Shake Support | โ Fails (simulate shake triggers no sensors) | โ Supported (intercepts native motion window) |
| Dynamic Sampling Interval | โ ๏ธ Limited / Static configurations | โ Dynamic configuration down to microseconds |
| Bridge Overhead / CPU Load | High (floods bridge with 100+ coordinate msg/s) | Ultra Low (sleeps when static, custom intervals) |
| Barometer Fallback Handling | โ Prone to crash on missing hardware | โ Graceful fallback (emits clean Unsupported exception) |
| Desktop / macOS Support | โ ๏ธ Partial / Unhandled channels | โ Safe fallbacks (returns descriptive status exceptions) |
| 3D Attitude (Roll/Pitch/Yaw) | โ None | โ Supported (native sensor fusion) |
| Pedometer & Proximity Sensors | โ None | โ Supported (native step & face detection) |
๐ Getting Started #
Installation #
Add motion_sensors_pro to your pubspec.yaml dependencies:
dependencies:
motion_sensors_pro:
path: ../Motion_Sensor_Pro # Or pub version when published
Run flutter pub get in your project folder.
๐ Requirements #
iOS #
- iOS 12.0 or higher.
- To listen to the Barometer (
barometerEvents) or Pedometer (pedometerEvents), you must include theNSMotionUsageDescriptionkey in yourios/Runner/Info.plistfile:<key>NSMotionUsageDescription</key> <string>This app requires access to motion data to receive barometric pressure readings and count physical steps.</string>
Android #
- Android SDK 21 (Lollipop) or higher.
- Uses standard hardware sensors; no manifest permissions required.
๐ Usage #
1. Dynamic Sampling Frequency (Global Configuration) #
Unlike other plugins, you can configure the sensor polling interval dynamically from Dart. CoreMotion and Android SensorManager adapt immediately:
import 'package:motion_sensors_pro/motion_sensors_pro.dart';
// Set sampling rate to every 50 milliseconds (20Hz)
await MotionSensorsPro.setSensorInterval(const Duration(milliseconds: 50));
2. Shake Gesture Detection #
Listen for device shake events. A native-level cooldown (1.0s) and Dart-level debouncer (1.5s) ensure exactly one event fires per physical gesture:
MotionSensorsPro.onShake.listen((_) {
print("๐ฑ Device was shaken!");
});
Xcode Simulator Testing
In Xcode Simulator, trigger a shake by selecting Features -> Shake Gesture (or Hardware -> Shake Gesture). The stream will fire normally.
3. Accelerometer (Raw Acceleration including Gravity) #
Emits raw acceleration values along the X, Y, and Z axes in $m/s^2$.
MotionSensorsPro.accelerometerEvents.listen((event) {
print("Raw Accelerometer: X: ${event.x}, Y: ${event.y}, Z: ${event.z}");
});
4. User Accelerometer (Acceleration excluding Gravity) #
Emits clean acceleration values with gravity mathematically subtracted by the device's hardware abstraction layers (CoreMotion / Android Sensor Hub).
MotionSensorsPro.userAccelerometerEvents.listen((event) {
print("User Acceleration: X: ${event.x}, Y: ${event.y}, Z: ${event.z}");
});
5. Gyroscope (Angular Velocity) #
Emits spatial rotation velocity readings in radians per second ($rad/s$).
MotionSensorsPro.gyroscopeEvents.listen((event) {
print("Gyroscope Rotation: X: ${event.x}, Y: ${event.y}, Z: ${event.z}");
});
6. Magnetometer (Ambient Magnetic Field) #
Emits geomagnetic field strength readings along the three spatial axes in micro-Tesla ($\mu T$).
MotionSensorsPro.magnetometerEvents.listen((event) {
print("Magnetic Field: X: ${event.x}, Y: ${event.y}, Z: ${event.z}");
});
7. Barometer (Atmospheric Pressure & Relative Altitude) #
Emits atmospheric pressure values in hectopascals ($hPa$) / millibars and relative altitude changes in meters (supported primarily on iOS). Fails gracefully with a descriptive error if the device lacks barometer hardware:
MotionSensorsPro.barometerEvents.listen(
(event) {
print("Pressure: ${event.pressure} hPa");
print("Rel Altitude change: ${event.relativeAltitude} meters");
},
onError: (error) {
print("Barometer not supported: $error");
},
);
8. Device Attitude (3D Sensor Fusion Orientation) #
Emits absolute orientation values (roll, pitch, yaw) in radians using native device-level Kalman filtering.
MotionSensorsPro.attitudeEvents.listen((event) {
print("Device Attitude: Roll: ${event.roll}, Pitch: ${event.pitch}, Yaw: ${event.yaw}");
});
9. Pedometer (Low-Power Step Counter) #
Emits low-power hardware step counter updates.
MotionSensorsPro.pedometerEvents.listen(
(event) {
print("Steps walked: ${event.steps}");
},
onError: (error) {
print("Pedometer not supported: $error");
},
);
10. Proximity Sensor (Face Detection / Obstructed Screen) #
Emits true if an object or user's face is close to the screen, and false otherwise. Automatically enables/disables hardware monitoring dynamically based on stream subscription status to maximize battery conservation.
MotionSensorsPro.proximityEvents.listen(
(event) {
print("Is screen obstructed/face near: ${event.isNear}");
},
onError: (error) {
print("Proximity sensor not supported: $error");
},
);
๐งช Programmatic Testing (Mocking) #
You can programmatically mock a shake gesture event for automated driver/unit tests:
// Simulates a physical shake gesture
await MotionSensorsPro.mockShake();
๐ ๏ธ Implementation Details #
iOS (Swift) #
- Shake: Extended
UIWindowto intercept.motionShakeevents directly. - Motion Sensors: Binds to
CMMotionManager. Acceleration values are scaled by standard gravity ($9.80665$) to ensure cross-platform unit consistency. - Barometer: Accesses
CMAltimeterand multiplies pressure values by $10.0$ to convert kilopascals ($kPa$) to standard hectopascals ($hPa$). - Pedometer: Directly integrates with CoreMotion
CMPedometerstep counts updates. - Proximity: Dynamically monitors
UIDevice.proximityStateDidChangeNotificationon subscription.
Android (Kotlin) #
- Shake: Prioritizes Android's hardware
Sensor.TYPE_LINEAR_ACCELERATION(gravity pre-subtracted by Android OS), falling back to magnitude-delta calculations on older hardware. - Motion Sensors: Integrates
SensorManagerevent listeners forTYPE_ACCELEROMETER,TYPE_LINEAR_ACCELERATION,TYPE_GYROSCOPE,TYPE_MAGNETIC_FIELD,TYPE_PRESSURE,TYPE_ROTATION_VECTOR(Attitude),TYPE_STEP_COUNTER, andTYPE_PROXIMITY. - Thread Safety: Relies on a main-thread handler to pass events safely into Flutter's
EventSink.
โ ๏ธ Hardware Limitations & Safety Fallbacks #
- Barometer sensor availability: Atmospheric pressure barometers are missing on budget Android models and older iOS units.
motion_sensors_prochecks hardware natively and emits aSENSOR_UNSUPPORTEDstream exception rather than crashing the native process. - Desktop environment testing: macOS desktop devices do not contain raw physical accelerometers, gyroscopes, or barometers. The plugin catches this and returns a
SENSOR_UNAVAILABLEerror to protect your code layout. - Android Linear Acceleration: If
TYPE_LINEAR_ACCELERATIONis unavailable on legacy Android boards, the plugin falls back to compute high-pass delta coordinates onTYPE_ACCELEROMETERnatively.
๐ Learn More #
To explore the complete class signatures, event types, and parameter details, please check out the auto-generated API Documentation on pub.dev.
๐ License #
This project is licensed under the MIT License - see the LICENSE file for details.