Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CompassUpdateRate parameter [Android only because android has delay] #93

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -61,6 +67,7 @@ public FlutterCompassPlugin() {
// no-op
}


private FlutterCompassPlugin(Context context) {
display = ((DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE))
.getDisplay(Display.DEFAULT_DISPLAY);
Expand All @@ -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<Integer> args = (ArrayList<Integer>) arguments;
if(!args.isEmpty()){
COMPASS_UPDATE_RATE_MS = args.get(0);
}

sensorEventListener = createSensorEventListener(events);

if (isCompassSensorAvailable()) {
Expand All @@ -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) {
Expand Down Expand Up @@ -330,4 +342,6 @@ private float[] getRotationVectorFromSensorEvent(@NonNull SensorEvent event) {
}
};
}


}
30 changes: 19 additions & 11 deletions example/.idea/libraries/Dart_SDK.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions example/.idea/libraries/Flutter_Plugins.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

120 changes: 50 additions & 70 deletions example/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -24,7 +24,7 @@ android {
defaultConfig {
applicationId "com.hemanthraj.fluttercompassexample"
minSdkVersion 23
targetSdkVersion 30
targetSdkVersion 33
versionCode 1
versionName "1.0"
}
Expand Down
1 change: 1 addition & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.android.tools.build:gradle:7.0.4'
}
}

Expand Down
2 changes: 1 addition & 1 deletion example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
android.enableJetifier=true
android.useAndroidX=true
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true

2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions example/flutter_compass_example_android.iml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
<sourceFolder url="file://$MODULE_DIR$/android/app/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/android/gen" isTestSource="false" generated="true" />
</content>
<orderEntry type="jdk" jdkName="Android API 25 Platform" jdkType="Android SDK" />
<orderEntry type="jdk" jdkName="Android Studio default JDK" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Flutter for Android" level="project" />
</component>
</module>
</module>
9 changes: 7 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ class _MyAppState extends State<MyApp> {
CompassEvent? _lastRead;
DateTime? _lastReadAt;


@override
void initState() {
super.initState();

_fetchPermissionStatus();
}



@override
Widget build(BuildContext context) {
return MaterialApp(
Expand Down Expand Up @@ -59,7 +62,7 @@ class _MyAppState extends State<MyApp> {
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();
Expand Down Expand Up @@ -89,9 +92,11 @@ class _MyAppState extends State<MyApp> {
);
}



Widget _buildCompass() {
return StreamBuilder<CompassEvent>(
stream: FlutterCompass.events,
stream: FlutterCompass.flutterCompass(compassUpdateRate: 20),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text('Error reading heading: ${snapshot.error}');
Expand Down
8 changes: 4 additions & 4 deletions lib/flutter_compass.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompassEvent>? _stream;

/// Provides a [Stream] of compass events that can be listened to.
static Stream<CompassEvent>? get events {
/// Controls the compass update rate in milliseconds
static Stream<CompassEvent>? flutterCompass ({int compassUpdateRate = 32}){
_stream ??= _compassChannel
.receiveBroadcastStream()
.receiveBroadcastStream([compassUpdateRate])
.map((dynamic data) => CompassEvent.fromList(data?.cast<double>()));
return _stream;
}
Expand Down