From ebc559304c9808764e202958429ced6f35b49cba Mon Sep 17 00:00:00 2001 From: Midori Date: Thu, 28 Mar 2024 14:43:16 +0900 Subject: [PATCH 1/3] Throw Exception when sensor not found in iOS --- ios/Classes/CompassxPlugin.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ios/Classes/CompassxPlugin.swift b/ios/Classes/CompassxPlugin.swift index 2c32781..aa6be04 100644 --- a/ios/Classes/CompassxPlugin.swift +++ b/ios/Classes/CompassxPlugin.swift @@ -22,6 +22,11 @@ public class CompassXPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, CLLo public func onListen(withArguments _: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? { eventSink = events + let isHeadingAvailable = CLLocationManager.headingAvailable() + if !isHeadingAvailable { + eventSink?(FlutterError(code: "SENSOR_NOT_FOUND", message: "No compass sensor found.", details: nil)) + return nil + } locationManager.startUpdatingHeading() return nil } From d7421b34bceca0e517ce0884979c7f3079807bcc Mon Sep 17 00:00:00 2001 From: Midori Date: Thu, 28 Mar 2024 14:59:49 +0900 Subject: [PATCH 2/3] Update docs --- README.md | 13 +++++++------ lib/src/compassx.dart | 6 ++++-- lib/src/compassx_event.dart | 3 +++ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ac153ff..ed690cf 100644 --- a/README.md +++ b/README.md @@ -63,9 +63,7 @@ Add code to request permissions. if (Platform.isAndroid) await Permission.location.request(); ``` -## Precautions - -**CompassX `heading` currently supports only portrait mode. It is recommended to fix the orientation of the device.** +CompassX `heading` currently supports only portrait mode. It is recommended to fix the orientation of the device. ```dart SystemChrome.setPreferredOrientations([ DeviceOrientation.portraitUp, @@ -73,11 +71,14 @@ SystemChrome.setPreferredOrientations([ ]); ``` +## Testing + +**It is recommended to use a real device.** iOS simulators cannot use the heading sensor (compass). See [Testing CompassX](https://github.com/natsuk4ze/compassx/wiki#checking-the-accuracy-of-compassx) for details. + + ## Documentation -**If you are going to use this plugin in your product apps, I strongly suggest you read [full documentation](https://github.com/natsuk4ze/compassx/wiki) carefully**. -When testing, use the actual device for testing. The emulator may not provide correct sensor data. +If you are going to use this plugin in your product apps, I strongly suggest you read [full documentation](https://github.com/natsuk4ze/compassx/wiki) carefully. -- [Testing CompassX](https://github.com/natsuk4ze/compassx/wiki#checking-the-accuracy-of-compassx) - [True Heading vs Magnetic Heading](https://github.com/natsuk4ze/compassx/wiki#true-heading) - [How to calibrate your device's compass](https://github.com/natsuk4ze/compassx/wiki#calibration) \ No newline at end of file diff --git a/lib/src/compassx.dart b/lib/src/compassx.dart index 29a11c5..9a85af0 100644 --- a/lib/src/compassx.dart +++ b/lib/src/compassx.dart @@ -11,7 +11,9 @@ final class CompassX { /// [CompassXEvent] stream for using the compass sensor. /// - /// Throws [CompassXException] for older or excessively cheap Android devices - /// that do not have a compass sensor. + /// Throw [CompassXException] when the compass sensor is not available. + /// That includes the following cases. + /// - Older or excessively cheap Android devices. + /// - iOS simulators. static Stream get events => CompassXPlatform.events; } diff --git a/lib/src/compassx_event.dart b/lib/src/compassx_event.dart index d2f5ed5..1784b1d 100644 --- a/lib/src/compassx_event.dart +++ b/lib/src/compassx_event.dart @@ -38,6 +38,9 @@ final class CompassXEvent { /// If this value is true, the sensor values are unreliable and should. /// See the [wiki](https://github.com/natsuk4ze/compassx/wiki) for /// calibration instructions. + /// Note that this is not always possible to detect. For use cases where + /// discretion is required, it is recommended to use [accuracy] to set your + /// own adjustment lines or to prompt for calibration each time. final bool shouldCalibrate; /// Factory for the map data obtained from the stream of [EventChannel]. From d2e973c674a0e27bd4e1ca1bd0a7072948cafc62 Mon Sep 17 00:00:00 2001 From: Midori Date: Thu, 28 Mar 2024 14:59:59 +0900 Subject: [PATCH 3/3] Update example --- example/lib/main.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index ef4e5fe..79e359e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -29,7 +29,8 @@ class App extends StatelessWidget { child: StreamBuilder( stream: CompassX.events, builder: (context, snapshot) { - if (!snapshot.hasData) return const Text('No data'); + if (snapshot.hasError) return Text(snapshot.error.toString()); + if (!snapshot.hasData) return const CircularProgressIndicator(); final compass = snapshot.data!; return Column( mainAxisSize: MainAxisSize.min,