Skip to content

Commit 8778d6c

Browse files
committed
feat(sensors_plus): add magnetometer support
Added necessary Android, iOS, and web API for magnetometer sensor. Adds new `MagnetometerEvent` class (and fixes small typo in constructor of other `FooEvent` classes). Adds new `Stream<MagnetometerEvent>` called `magnetometerEvents`.
1 parent 43d9077 commit 8778d6c

29 files changed

+284
-47
lines changed

packages/sensors_plus/sensors_plus/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.1.0
2+
3+
- Adds magnetometer support
4+
15
## 1.0.2
26

37
- Android: migrate to mavenCentral

packages/sensors_plus/sensors_plus/README.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@
77
<p class="center">
88
<center><a href="https://flutter.dev/docs/development/packages-and-plugins/favorites" target="_blank" rel="noreferrer noopener"><img src="../../website/static/img/flutter-favorite-badge.png" width="100" alt="build"></a></center>
99
</p>
10-
A Flutter plugin to access the accelerometer and gyroscope sensors.
10+
11+
A Flutter plugin to access the accelerometer, gyroscope, and magnetometer
12+
sensors.
1113

1214
## Platform Support
1315

14-
| Android | iOS | MacOS | Web | Linux | Windows |
15-
| :-----: | :-: | :---: | :-: | :---: | :----: |
16-
| ✔️ | ✔️ | | ✔️ | | |
16+
| Android | iOS | MacOS | Web | Linux | Windows |
17+
| :-----: | :---: | :---: | :---: | :---: | :-----: |
18+
| ✔️ | ✔️ | | ✔️ | | |
1719

1820
## Usage
1921

2022
To use this plugin, add `sensors_plus` as a [dependency in your pubspec.yaml
2123
file](https://plus.fluttercommunity.dev/docs/overview).
2224

23-
This will expose three classes of sensor events, through three different
25+
This will expose four classes of sensor events through four different
2426
streams.
2527

2628
- `AccelerometerEvent`s describe the velocity of the device, including the
@@ -30,9 +32,12 @@ streams.
3032
include gravity. They can also be thought of as just the user's affect on the
3133
device.
3234
- `GyroscopeEvent`s describe the rotation of the device.
35+
- `MagnetometerEvent`s describe the ambient magnetic field surrounding the
36+
device. A compass is an example usage of this data.
3337

3438
Each of these is exposed through a `BroadcastStream`: `accelerometerEvents`,
35-
`userAccelerometerEvents`, and `gyroscopeEvents`, respectively.
39+
`userAccelerometerEvents`, `gyroscopeEvents`, and `magnetometerEvents`,
40+
respectively.
3641

3742
### Example
3843

@@ -54,11 +59,20 @@ gyroscopeEvents.listen((GyroscopeEvent event) {
5459
});
5560
// [GyroscopeEvent (x: 0.0, y: 0.0, z: 0.0)]
5661
62+
magnetometerEvents.listen((MagnetometerEvent event) {
63+
print(event);
64+
});
65+
// [MagnetometerEvent (x: -23.6, y: 6.2, z: -34.9)]
66+
5767
```
5868

5969
Also see the `example` subdirectory for an example application that uses the
6070
sensor data.
6171

62-
Check out our documentation website to learn more. [Plus plugins documentation](https://plus.fluttercommunity.dev/docs/overview)
72+
Check out our website to learn more: [Plus Plugins documentation](https://plus.fluttercommunity.dev/docs/overview)
6373

64-
**Important:** As of January 2021, the Flutter team is no longer accepting non-critical PRs for the original set of plugins in `flutter/plugins`, and instead they should be submitted in this project. [You can read more about this announcement here.](https://github.com/flutter/plugins/blob/master/CONTRIBUTING.md#important-note) as well as [in the Flutter 2 announcement blog post.](https://medium.com/flutter/whats-new-in-flutter-2-0-fe8e95ecc65)
74+
**Important:** As of January 2021, the Flutter team is no longer accepting
75+
non-critical PRs for the original set of plugins in `flutter/plugins`, and
76+
instead they should be submitted in this project.
77+
[You can read more about this announcement here](https://github.com/flutter/plugins/blob/master/CONTRIBUTING.md#important-note)
78+
as well as [in the Flutter 2 announcement blog post](https://medium.com/flutter/whats-new-in-flutter-2-0-fe8e95ecc65).

packages/sensors_plus/sensors_plus/android/src/main/java/dev/fluttercommunity/plus/sensors/SensorsPlugin.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ public class SensorsPlugin implements FlutterPlugin {
2020
"dev.fluttercommunity.plus/sensors/gyroscope";
2121
private static final String USER_ACCELEROMETER_CHANNEL_NAME =
2222
"dev.fluttercommunity.plus/sensors/user_accel";
23+
private static final String MAGNETOMETER_CHANNEL_NAME =
24+
"dev.fluttercommunity.plus/sensors/magnetometer";
2325

2426
private EventChannel accelerometerChannel;
2527
private EventChannel userAccelChannel;
2628
private EventChannel gyroscopeChannel;
29+
private EventChannel magnetometerChannel;
2730

2831
/** Plugin registration. */
2932
public static void registerWith(Registrar registrar) {
@@ -63,11 +66,19 @@ private void setupEventChannels(Context context, BinaryMessenger messenger) {
6366
(SensorManager) context.getSystemService(context.SENSOR_SERVICE),
6467
Sensor.TYPE_GYROSCOPE);
6568
gyroscopeChannel.setStreamHandler(gyroScopeStreamHandler);
69+
70+
magnetometerChannel = new EventChannel(messenger, MAGNETOMETER_CHANNEL_NAME);
71+
final StreamHandlerImpl magnetometerStreamHandler =
72+
new StreamHandlerImpl(
73+
(SensorManager) context.getSystemService(context.SENSOR_SERVICE),
74+
Sensor.TYPE_MAGNETIC_FIELD);
75+
magnetometerChannel.setStreamHandler(magnetometerStreamHandler);
6676
}
6777

6878
private void teardownEventChannels() {
6979
accelerometerChannel.setStreamHandler(null);
7080
userAccelChannel.setStreamHandler(null);
7181
gyroscopeChannel.setStreamHandler(null);
82+
magnetometerChannel.setStreamHandler(null);
7283
}
7384
}

packages/sensors_plus/sensors_plus/android/src/main/java/dev/fluttercommunity/plus/sensors/StreamHandlerImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ class StreamHandlerImpl implements EventChannel.StreamHandler {
2424
@Override
2525
public void onListen(Object arguments, EventChannel.EventSink events) {
2626
sensorEventListener = createSensorEventListener(events);
27-
sensorManager.registerListener(sensorEventListener, sensor, sensorManager.SENSOR_DELAY_NORMAL);
27+
// TODO: Could allow developer to alter rate?
28+
// sensorManager.registerListener(sensorEventListener, sensor, sensorManager.SENSOR_DELAY_FASTEST); // 0
29+
// sensorManager.registerListener(sensorEventListener, sensor, sensorManager.SENSOR_DELAY_GAME); // 1
30+
// sensorManager.registerListener(sensorEventListener, sensor, sensorManager.SENSOR_DELAY_UI); // 2
31+
sensorManager.registerListener(sensorEventListener, sensor, sensorManager.SENSOR_DELAY_NORMAL); // 3
2832
}
2933

3034
@Override

packages/sensors_plus/sensors_plus/example/android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
5+
# distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip

packages/sensors_plus/sensors_plus/example/lib/main.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class _MyHomePageState extends State<MyHomePage> {
4444
List<double>? _accelerometerValues;
4545
List<double>? _userAccelerometerValues;
4646
List<double>? _gyroscopeValues;
47+
List<double>? _magnetometerValues;
4748
final _streamSubscriptions = <StreamSubscription<dynamic>>[];
4849

4950
@override
@@ -55,6 +56,8 @@ class _MyHomePageState extends State<MyHomePage> {
5556
final userAccelerometer = _userAccelerometerValues
5657
?.map((double v) => v.toStringAsFixed(1))
5758
.toList();
59+
final magnetometer =
60+
_magnetometerValues?.map((double v) => v.toStringAsFixed(1)).toList();
5861

5962
return Scaffold(
6063
appBar: AppBar(
@@ -106,6 +109,15 @@ class _MyHomePageState extends State<MyHomePage> {
106109
],
107110
),
108111
),
112+
Padding(
113+
padding: const EdgeInsets.all(16.0),
114+
child: Row(
115+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
116+
children: <Widget>[
117+
Text('Magnetometer: $magnetometer'),
118+
],
119+
),
120+
),
109121
],
110122
),
111123
);
@@ -149,5 +161,14 @@ class _MyHomePageState extends State<MyHomePage> {
149161
},
150162
),
151163
);
164+
_streamSubscriptions.add(
165+
magnetometerEvents.listen(
166+
(MagnetometerEvent event) {
167+
setState(() {
168+
_magnetometerValues = <double>[event.x, event.y, event.z];
169+
});
170+
},
171+
),
172+
);
152173
}
153174
}

packages/sensors_plus/sensors_plus/ios/Classes/FLTSensorsPlusPlugin.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@
1515

1616
@interface FLTGyroscopeStreamHandlerPlus : NSObject <FlutterStreamHandler>
1717
@end
18+
19+
@interface FLTMagnetometerStreamHandlerPlus : NSObject <FlutterStreamHandler>
20+
@end

packages/sensors_plus/sensors_plus/ios/Classes/FLTSensorsPlusPlugin.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
2828
[FlutterEventChannel eventChannelWithName:@"dev.fluttercommunity.plus/sensors/gyroscope"
2929
binaryMessenger:[registrar messenger]];
3030
[gyroscopeChannel setStreamHandler:gyroscopeStreamHandler];
31+
32+
FLTMagnetometerStreamHandlerPlus* magnetometerStreamHandler =
33+
[[FLTMagnetometerStreamHandlerPlus alloc] init];
34+
FlutterEventChannel* magnetometerChannel =
35+
[FlutterEventChannel eventChannelWithName:@"dev.fluttercommunity.plus/sensors/magnetometer"
36+
binaryMessenger:[registrar messenger]];
37+
[magnetometerChannel setStreamHandler:magnetometerStreamHandler];
3138
}
3239

3340
@end
@@ -113,3 +120,23 @@ - (FlutterError*)onCancelWithArguments:(id)arguments {
113120
}
114121

115122
@end
123+
124+
@implementation FLTMagnetometerStreamHandlerPlus
125+
126+
- (FlutterError*)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)eventSink {
127+
_initMotionManager();
128+
[_motionManager
129+
startMagnetometerUpdatesToQueue:[[NSOperationQueue alloc] init]
130+
withHandler:^(CMMagnetometerData* magData, NSError* error) {
131+
CMMagneticField magneticField = magData.magneticField;
132+
sendTriplet(magneticField.x, magneticField.y, magneticField.z, eventSink);
133+
}];
134+
return nil;
135+
}
136+
137+
- (FlutterError*)onCancelWithArguments:(id)arguments {
138+
[_motionManager stopMagnetometerUpdates];
139+
return nil;
140+
}
141+
142+
@end

packages/sensors_plus/sensors_plus/ios/sensors_plus.podspec

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
#
44
Pod::Spec.new do |s|
55
s.name = 'sensors_plus'
6-
s.version = '0.0.1'
7-
s.summary = 'Flutter Sensors'
6+
s.version = '0.1.0'
7+
s.summary = 'Flutter Community: Sensors Plus'
88
s.description = <<-DESC
9-
A Flutter plugin to access the accelerometer and gyroscope sensors.
9+
Flutter plugin to access the accelerometer, gyroscope, and magnetometer sensors.
1010
DESC
11-
s.homepage = 'https://github.com/flutter/plugins'
11+
# Should update?
12+
s.homepage = 'https://github.com/fluttercommunity/plus_plugins'
1213
s.license = { :type => 'BSD', :file => '../LICENSE' }
1314
s.author = { 'Flutter Dev Team' => '[email protected]' }
14-
s.source = { :http => 'https://github.com/flutter/plugins/tree/main/packages/sensors' }
15-
s.documentation_url = 'https://pub.dev/packages/sensors'
15+
s.source = { :http => 'https://github.com/fluttercommunity/plus_plugins/tree/main/packages/sensors_plus' }
16+
s.documentation_url = 'https://pub.dev/documentation/sensors_plus'
1617
s.source_files = 'Classes/**/*'
1718
s.public_header_files = 'Classes/**/*.h'
1819
s.dependency 'Flutter'
19-
20+
2021
s.platform = :ios, '8.0'
2122
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'VALID_ARCHS[sdk=iphonesimulator*]' => 'x86_64' }
2223
end
23-

packages/sensors_plus/sensors_plus/lib/sensors_plus.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ Stream<GyroscopeEvent> get gyroscopeEvents {
2121
Stream<UserAccelerometerEvent> get userAccelerometerEvents {
2222
return _sensors.userAccelerometerEvents;
2323
}
24+
25+
/// A broadcast stream of events from the device magnetometer.
26+
Stream<MagnetometerEvent> get magnetometerEvents {
27+
return _sensors.magnetometerEvents;
28+
}

0 commit comments

Comments
 (0)