diff --git a/android/src/main/java/com/hemanthraj/fluttercompass/FlutterCompassPlugin.java b/android/src/main/java/com/hemanthraj/fluttercompass/FlutterCompassPlugin.java index 5c1bca0..e63e595 100644 --- a/android/src/main/java/com/hemanthraj/fluttercompass/FlutterCompassPlugin.java +++ b/android/src/main/java/com/hemanthraj/fluttercompass/FlutterCompassPlugin.java @@ -14,12 +14,18 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import java.util.ArrayList; + import io.flutter.embedding.engine.plugins.FlutterPlugin; +import io.flutter.plugin.common.BinaryMessenger; import io.flutter.plugin.common.EventChannel; -import io.flutter.plugin.common.EventChannel.EventSink; -import io.flutter.plugin.common.EventChannel.StreamHandler; +import io.flutter.plugin.common.MethodCall; +import io.flutter.plugin.common.MethodChannel; +import io.flutter.plugin.common.MethodChannel.MethodCallHandler; +import io.flutter.plugin.common.MethodChannel.Result; + -public final class FlutterCompassPlugin implements FlutterPlugin, StreamHandler { +public final class FlutterCompassPlugin implements FlutterPlugin, EventChannel.StreamHandler { private static final String TAG = "FlutterCompass"; // The rate sensor events will be delivered at. As the Android documentation // states, this is only a hint to the system and the events might actually be @@ -33,7 +39,7 @@ public final class FlutterCompassPlugin implements FlutterPlugin, StreamHandler private static final float ALPHA = 0.45f; // Controls the compass update rate in milliseconds - private static final int COMPASS_UPDATE_RATE_MS = 32; + private static int COMPASS_UPDATE_RATE_MS = 32; private SensorEventListener sensorEventListener; @@ -61,6 +67,7 @@ public FlutterCompassPlugin() { // no-op } + private FlutterCompassPlugin(Context context) { display = ((DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE)) .getDisplay(Display.DEFAULT_DISPLAY); @@ -87,7 +94,12 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) { public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { } - public void onListen(Object arguments, EventSink events) { + public void onListen(Object arguments, EventChannel.EventSink events) { + ArrayList args = (ArrayList) arguments; + if(!args.isEmpty()){ + COMPASS_UPDATE_RATE_MS = args.get(0); + } + sensorEventListener = createSensorEventListener(events); if (isCompassSensorAvailable()) { @@ -112,7 +124,7 @@ private boolean isCompassSensorAvailable() { return compassSensor != null; } - SensorEventListener createSensorEventListener(final EventSink events) { + SensorEventListener createSensorEventListener(final EventChannel.EventSink events) { return new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { @@ -330,4 +342,6 @@ private float[] getRotationVectorFromSensorEvent(@NonNull SensorEvent event) { } }; } + + } diff --git a/example/.idea/libraries/Dart_SDK.xml b/example/.idea/libraries/Dart_SDK.xml index 64c6d58..7f7e03f 100644 --- a/example/.idea/libraries/Dart_SDK.xml +++ b/example/.idea/libraries/Dart_SDK.xml @@ -1,17 +1,25 @@ - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + diff --git a/example/.idea/libraries/Flutter_Plugins.xml b/example/.idea/libraries/Flutter_Plugins.xml index 83a5fa6..9ac7db9 100644 --- a/example/.idea/libraries/Flutter_Plugins.xml +++ b/example/.idea/libraries/Flutter_Plugins.xml @@ -2,6 +2,7 @@ + diff --git a/example/.idea/workspace.xml b/example/.idea/workspace.xml index f208fc9..cd86e9c 100644 --- a/example/.idea/workspace.xml +++ b/example/.idea/workspace.xml @@ -1,38 +1,43 @@ + + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + @@ -55,66 +60,44 @@ - - - - - - - + + + { + "keyToString": { + "RunOnceActivity.cidr.known.project.marker": "true", + "cidr.known.project.marker": "true", + "dart.analysis.tool.window.visible": "false", + "io.flutter.reload.alreadyRun": "true", + "last_opened_file_path": "/Users/sinanhaci/Project/Mobile/Custom/flutter_compass/example", + "show.migrate.to.gradle.popup": "false" + } +} - - + + - - + @@ -168,9 +151,6 @@ - - diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index da20053..fc9fbf0 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -15,7 +15,7 @@ apply plugin: 'com.android.application' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { - compileSdkVersion 30 + compileSdkVersion 33 lintOptions { disable 'InvalidPackage' @@ -24,7 +24,7 @@ android { defaultConfig { applicationId "com.hemanthraj.fluttercompassexample" minSdkVersion 23 - targetSdkVersion 30 + targetSdkVersion 33 versionCode 1 versionName "1.0" } diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 2f91cd8..7595725 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -24,6 +24,7 @@ android:name="io.flutter.embedding.android.FlutterActivity" android:launchMode="singleTop" android:theme="@style/LaunchTheme" + android:exported="true" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize"> diff --git a/example/android/build.gradle b/example/android/build.gradle index 6e12e86..2797bde 100644 --- a/example/android/build.gradle +++ b/example/android/build.gradle @@ -5,7 +5,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.3.2' + classpath 'com.android.tools.build:gradle:7.0.4' } } diff --git a/example/android/gradle.properties b/example/android/gradle.properties index b6e61b6..bb04111 100644 --- a/example/android/gradle.properties +++ b/example/android/gradle.properties @@ -1,4 +1,4 @@ android.enableJetifier=true android.useAndroidX=true org.gradle.jvmargs=-Xmx1536M -android.enableR8=true + diff --git a/example/android/gradle/wrapper/gradle-wrapper.properties b/example/android/gradle/wrapper/gradle-wrapper.properties index 2819f02..2005b75 100644 --- a/example/android/gradle/wrapper/gradle-wrapper.properties +++ b/example/android/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip diff --git a/example/flutter_compass_example_android.iml b/example/flutter_compass_example_android.iml index b050030..af0a968 100644 --- a/example/flutter_compass_example_android.iml +++ b/example/flutter_compass_example_android.iml @@ -20,8 +20,8 @@ - + - + \ No newline at end of file diff --git a/example/lib/main.dart b/example/lib/main.dart index 06dc0ca..944af59 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -20,6 +20,7 @@ class _MyAppState extends State { CompassEvent? _lastRead; DateTime? _lastReadAt; + @override void initState() { super.initState(); @@ -27,6 +28,8 @@ class _MyAppState extends State { _fetchPermissionStatus(); } + + @override Widget build(BuildContext context) { return MaterialApp( @@ -59,7 +62,7 @@ class _MyAppState extends State { ElevatedButton( child: Text('Read Value'), onPressed: () async { - final CompassEvent tmp = await FlutterCompass.events!.first; + final CompassEvent tmp = await FlutterCompass.flutterCompass()!.first; setState(() { _lastRead = tmp; _lastReadAt = DateTime.now(); @@ -89,9 +92,11 @@ class _MyAppState extends State { ); } + + Widget _buildCompass() { return StreamBuilder( - stream: FlutterCompass.events, + stream: FlutterCompass.flutterCompass(compassUpdateRate: 20), builder: (context, snapshot) { if (snapshot.hasError) { return Text('Error reading heading: ${snapshot.error}'); diff --git a/lib/flutter_compass.dart b/lib/flutter_compass.dart index 2c2f7fa..8a2b369 100644 --- a/lib/flutter_compass.dart +++ b/lib/flutter_compass.dart @@ -39,14 +39,14 @@ class FlutterCompass { FlutterCompass._(); - static const EventChannel _compassChannel = - const EventChannel('hemanthraj/flutter_compass'); + static const EventChannel _compassChannel = const EventChannel('hemanthraj/flutter_compass'); static Stream? _stream; /// Provides a [Stream] of compass events that can be listened to. - static Stream? get events { + /// Controls the compass update rate in milliseconds + static Stream? flutterCompass ({int compassUpdateRate = 32}){ _stream ??= _compassChannel - .receiveBroadcastStream() + .receiveBroadcastStream([compassUpdateRate]) .map((dynamic data) => CompassEvent.fromList(data?.cast())); return _stream; }