sensors_plus 4.0.1
sensors_plus: ^4.0.1 copied to clipboard
Flutter plugin for accessing accelerometer, gyroscope, and magnetometer sensors.
sensors_plus #
A Flutter plugin to access the accelerometer, gyroscope, and magnetometer sensors.
Platform Support #
| Android | iOS | MacOS | Web | Linux | Windows |
|---|---|---|---|---|---|
| ✅ | ✅ | ❌ | ✅* | ❌ | ❌ |
* Currently it is not possible to set sensors sampling rate on web
Usage #
Add sensors_plus as a dependency in your pubspec.yaml file.
This will expose such classes of sensor events through a set of streams:
UserAccelerometerEventdescribes the acceleration of the device, in m/s2. If the device is still, or is moving along a straight line at constant speed, the reported acceleration is zero. If the device is moving e.g. towards north and its speed is increasing, the reported acceleration is towards north; if it is slowing down, the reported acceleration is towards south; if it is turning right, the reported acceleration is towards east. The data of this stream is obtained by filtering out the effect of gravity fromAccelerometerEvent.AccelerometerEventdescribes the acceleration of the device, in m/s2, including the effects of gravity. UnlikeUserAccelerometerEvent, this stream reports raw data from the accelerometer (physical sensor embedded in the mobile device) without any post-processing. The accelerometer is unable to distinguish between the effect of an accelerated movement of the device and the effect of the surrounding gravitational field. This means that, at the surface of Earth, even if the device is completely still, the reading ofAccelerometerEventis an acceleration of intensity 9.8 directed upwards (the opposite of the graviational acceleration). This can be used to infer information about the position of the device (horizontal/vertical/tilted).AccelerometerEventreports zero acceleration if the device is free falling.GyroscopeEventdescribes the rotation of the device.MagnetometerEventdescribes the ambient magnetic field surrounding the device. A compass is an example usage of this data.
These events are exposed through a BroadcastStream: accelerometerEvents,
userAccelerometerEvents, gyroscopeEvents, and magnetometerEvents,
respectively.
Note
Some low end or old Android devices don't have all sensors available. Plugin won't crash the app, but it is highly recommended to add onError() to handle such cases gracefully.
Example #
import 'package:sensors_plus/sensors_plus.dart';
accelerometerEvents.listen(
(AccelerometerEvent event) {
print(event);
},
onError: (error) {
// Logic to handle error
// Needed for Android in case sensor is not available
},
cancelOnError: true,
);
// [AccelerometerEvent (x: 0.0, y: 9.8, z: 0.0)]
userAccelerometerEvents.listen(
(UserAccelerometerEvent event) {
print(event);
},
onError: (error) {
// Logic to handle error
// Needed for Android in case sensor is not available
},
cancelOnError: true,
);
// [UserAccelerometerEvent (x: 0.0, y: 0.0, z: 0.0)]
gyroscopeEvents.listen(
(GyroscopeEvent event) {
print(event);
},
onError: (error) {
// Logic to handle error
// Needed for Android in case sensor is not available
},
cancelOnError: true,
);
// [GyroscopeEvent (x: 0.0, y: 0.0, z: 0.0)]
magnetometerEvents.listen(
(MagnetometerEvent event) {
print(event);
},
onError: (error) {
// Logic to handle error
// Needed for Android in case sensor is not available
},
cancelOnError: true,
);
// [MagnetometerEvent (x: -23.6, y: 6.2, z: -34.9)]
Alternatively, every stream allows to specify the sampling rate for its sensor using one of predefined constants or using a custom value
magnetometerEvents(samplingPeriod: SensorInterval.normalInterval).listen(
(MagnetometerEvent event) {
print(event);
},
onError: (error) {
// Logic to handle error
// Needed for Android in case sensor is not available
},
cancelOnError: true,
);
For more detailed instruction check out the documentation linked below.
Also see the example subdirectory for an example application that uses the
sensor data.



