Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/sensors_plus/sensors_plus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.4.0

- iOS: Corrects magnetometer implementation, returning calibrated values from
`DeviceMotion` sensor rather than raw sensor samples

## 1.3.4+1

- Add issue_tracker link.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ @implementation FLTSensorsPlusPlugin
BOOL _isCleanUp = NO;

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
// alloc channels names
_eventChannels = [NSMutableDictionary dictionary];
_streamHandlers = [NSMutableDictionary dictionary];

// Accelerometer init
//
FLTAccelerometerStreamHandlerPlus *accelerometerStreamHandler =
[[FLTAccelerometerStreamHandlerPlus alloc] init];
NSString *accelerometerStreamHandlerName =
Expand All @@ -45,8 +42,6 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
[_streamHandlers setObject:userAccelerometerStreamHandler
forKey:accelerometerStreamHandlerName];

// Gyroscopee init
//
FLTGyroscopeStreamHandlerPlus *gyroscopeStreamHandler =
[[FLTGyroscopeStreamHandlerPlus alloc] init];
NSString *gyroscopeStreamHandlerName =
Expand All @@ -59,8 +54,6 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
[_streamHandlers setObject:gyroscopeStreamHandler
forKey:accelerometerStreamHandlerName];

// Magnerometer init
//
FLTMagnetometerStreamHandlerPlus *magnetometerStreamHandler =
[[FLTMagnetometerStreamHandlerPlus alloc] init];
NSString *magnetometerStreamHandlerName =
Expand Down Expand Up @@ -99,7 +92,7 @@ static void _cleanUp() {
const double GRAVITY = 9.8;
CMMotionManager *_motionManager;

void _initMotionManager() {
void _initMotionManager(void) {
if (!_motionManager) {
_motionManager = [[CMMotionManager alloc] init];
}
Expand All @@ -110,8 +103,8 @@ static void sendTriplet(Float64 x, Float64 y, Float64 z,
if (_isCleanUp) {
return;
}
// even if we removed all with [detachFromEngineForRegistrar] we stull can
// receive and fire some events from sensors til deataching
// Even after [detachFromEngineForRegistrar] some events may still be received
// and fired until fully detached.
@try {
NSMutableData *event = [NSMutableData dataWithCapacity:3 * sizeof(Float64)];
[event appendBytes:&x length:sizeof(Float64)];
Expand Down Expand Up @@ -225,23 +218,33 @@ @implementation FLTMagnetometerStreamHandlerPlus
- (FlutterError *)onListenWithArguments:(id)arguments
eventSink:(FlutterEventSink)eventSink {
_initMotionManager();
// Allow iOS to present calibration interaction.
_motionManager.showsDeviceMovementDisplay = YES;
[_motionManager
startMagnetometerUpdatesToQueue:[[NSOperationQueue alloc] init]
withHandler:^(CMMagnetometerData *magData,
NSError *error) {
CMMagneticField magneticField =
magData.magneticField;
if (_isCleanUp) {
return;
}
sendTriplet(magneticField.x, magneticField.y,
magneticField.z, eventSink);
}];
startDeviceMotionUpdatesUsingReferenceFrame:
// https://developer.apple.com/documentation/coremotion/cmattitudereferenceframe?language=objc
// "Using this reference frame may require device movement to
// calibrate the magnetometer," which is desired to ensure the
// DeviceMotion actually has updated, calibrated geomagnetic data.
CMAttitudeReferenceFrameXMagneticNorthZVertical
toQueue:[[NSOperationQueue alloc]
init]
withHandler:^(CMDeviceMotion *motionData,
NSError *error) {
// The `magneticField` is a
// CMCalibratedMagneticField.
CMMagneticField b =
motionData.magneticField.field;
if (_isCleanUp) {
return;
}
sendTriplet(b.x, b.y, b.z, eventSink);
}];
return nil;
}

- (FlutterError *)onCancelWithArguments:(id)arguments {
[_motionManager stopMagnetometerUpdates];
[_motionManager stopDeviceMotionUpdates];
return nil;
}

Expand Down
5 changes: 3 additions & 2 deletions packages/sensors_plus/sensors_plus/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: sensors_plus
description: Flutter plugin for accessing accelerometer, gyroscope, and magnetometer sensors.
version: 1.3.4+1
description: Flutter plugin for accessing accelerometer, gyroscope, and
magnetometer sensors.
version: 1.4.0
homepage: https://plus.fluttercommunity.dev/
repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/
issue_tracker: https://github.com/fluttercommunity/plus_plugins/labels/sensors_plus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
///
/// Consider that these samples may bear effects of Earth's magnetic field as
/// well as local factors such as the metal of the device itself or nearby
/// magnets.
/// magnets, though most devices compensate for these factors.
///
/// A compass is an example of a general utility for magnetometer data.
class MagnetometerEvent {
Expand Down