From d03e836eb99f5730d97152b58d169503ac33b14a Mon Sep 17 00:00:00 2001 From: David Lin Date: Wed, 27 Oct 2021 14:46:09 -0500 Subject: [PATCH 1/3] (#6) Update library to support Android V2 embedding, update example app to Android V2 embedding --- .../flutter_ble_lib/FlutterBleLibPlugin.java | 77 ++++++++++++++++--- .../android/app/src/main/AndroidManifest.xml | 4 +- .../flutter_ble_lib_example/MainActivity.java | 12 +-- 3 files changed, 71 insertions(+), 22 deletions(-) diff --git a/android/src/main/java/com/polidea/flutter_ble_lib/FlutterBleLibPlugin.java b/android/src/main/java/com/polidea/flutter_ble_lib/FlutterBleLibPlugin.java index 94b5469f..6cc8b827 100644 --- a/android/src/main/java/com/polidea/flutter_ble_lib/FlutterBleLibPlugin.java +++ b/android/src/main/java/com/polidea/flutter_ble_lib/FlutterBleLibPlugin.java @@ -1,5 +1,6 @@ package com.polidea.flutter_ble_lib; +import android.app.Activity; import android.content.Context; import android.util.Log; @@ -32,6 +33,10 @@ import java.util.List; import androidx.annotation.NonNull; +import io.flutter.embedding.engine.plugins.FlutterPlugin; +import io.flutter.embedding.engine.plugins.activity.ActivityAware; +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; +import io.flutter.plugin.common.BinaryMessenger; import io.flutter.plugin.common.EventChannel; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; @@ -39,7 +44,7 @@ import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.PluginRegistry.Registrar; -public class FlutterBleLibPlugin implements MethodCallHandler { +public class FlutterBleLibPlugin implements MethodCallHandler, FlutterPlugin, ActivityAware { static final String TAG = FlutterBleLibPlugin.class.getName(); @@ -53,16 +58,32 @@ public class FlutterBleLibPlugin implements MethodCallHandler { private List delegates = new LinkedList<>(); - public static void registerWith(Registrar registrar) { - final MethodChannel channel = new MethodChannel(registrar.messenger(), ChannelName.FLUTTER_BLE_LIB); + static MethodChannel channel; + private Activity activity; + + private static FlutterBleLibPlugin factory(Context context, Activity activity) { + FlutterBleLibPlugin plugin = new FlutterBleLibPlugin(); + plugin.context = context; + plugin.activity = activity; + return plugin; + } + + /** + * Initializes the plugin. + * + * @param context registrar.context() or binding.getApplicationContext() + * @param messenger registrar.messenger() or binding.getBinaryMessenger() + */ + private static void init(Context context, BinaryMessenger messenger, Activity activity) { + channel = new MethodChannel(messenger, ChannelName.FLUTTER_BLE_LIB); - final EventChannel bluetoothStateChannel = new EventChannel(registrar.messenger(), ChannelName.ADAPTER_STATE_CHANGES); - final EventChannel restoreStateChannel = new EventChannel(registrar.messenger(), ChannelName.STATE_RESTORE_EVENTS); - final EventChannel scanningChannel = new EventChannel(registrar.messenger(), ChannelName.SCANNING_EVENTS); - final EventChannel connectionStateChannel = new EventChannel(registrar.messenger(), ChannelName.CONNECTION_STATE_CHANGE_EVENTS); - final EventChannel characteristicMonitorChannel = new EventChannel(registrar.messenger(), ChannelName.MONITOR_CHARACTERISTIC); + final EventChannel bluetoothStateChannel = new EventChannel(messenger, ChannelName.ADAPTER_STATE_CHANGES); + final EventChannel restoreStateChannel = new EventChannel(messenger, ChannelName.STATE_RESTORE_EVENTS); + final EventChannel scanningChannel = new EventChannel(messenger, ChannelName.SCANNING_EVENTS); + final EventChannel connectionStateChannel = new EventChannel(messenger, ChannelName.CONNECTION_STATE_CHANGE_EVENTS); + final EventChannel characteristicMonitorChannel = new EventChannel(messenger, ChannelName.MONITOR_CHARACTERISTIC); - final FlutterBleLibPlugin plugin = new FlutterBleLibPlugin(registrar.context()); + final FlutterBleLibPlugin plugin = factory(context, activity); channel.setMethodCallHandler(plugin); @@ -73,8 +94,8 @@ public static void registerWith(Registrar registrar) { characteristicMonitorChannel.setStreamHandler(plugin.characteristicsMonitorStreamHandler); } - private FlutterBleLibPlugin(Context context) { - this.context = context; + public static void registerWith(Registrar registrar) { + init(registrar.context(), registrar.messenger(), registrar.activity()); } private void setupAdapter(Context context) { @@ -192,4 +213,38 @@ private void cancelTransaction(MethodCall call, Result result) { } result.success(null); } + + // FlutterPlugin interface: + + @Override + public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) { + init(binding.getApplicationContext(), binding.getBinaryMessenger(), null); + } + + @Override + public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { + channel.setMethodCallHandler(null); + } + + // ActivityAware interface: + + @Override + public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) { + activity = binding.getActivity(); + } + + @Override + public void onDetachedFromActivityForConfigChanges() { + activity = null; + } + + @Override + public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) { + activity = binding.getActivity(); + } + + @Override + public void onDetachedFromActivity() { + activity = null; + } } diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 6dcf69d8..e0b4ea5f 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -8,7 +8,6 @@ additional functionality it is fine to subclass or reimplement FlutterApplication and put your custom class here. --> + diff --git a/example/android/app/src/main/java/com/polidea/flutter_ble_lib_example/MainActivity.java b/example/android/app/src/main/java/com/polidea/flutter_ble_lib_example/MainActivity.java index f0cbbdc3..6dc07cfb 100644 --- a/example/android/app/src/main/java/com/polidea/flutter_ble_lib_example/MainActivity.java +++ b/example/android/app/src/main/java/com/polidea/flutter_ble_lib_example/MainActivity.java @@ -1,13 +1,5 @@ package com.polidea.flutter_ble_lib_example; -import android.os.Bundle; -import io.flutter.app.FlutterActivity; -import io.flutter.plugins.GeneratedPluginRegistrant; +import io.flutter.embedding.android.FlutterActivity; -public class MainActivity extends FlutterActivity { - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - GeneratedPluginRegistrant.registerWith(this); - } -} +public class MainActivity extends FlutterActivity {} From af771cd7903ef88192527edaa839d05d9e16096a Mon Sep 17 00:00:00 2001 From: Leo Huang Date: Tue, 30 Apr 2024 11:01:51 +1000 Subject: [PATCH 2/3] (#6) Bump gradle wrapper to 7.6.4 and android gradle plugin to 7.3.0 --- android/build.gradle | 2 +- android/gradle/wrapper/gradle-wrapper.properties | 2 +- example/android/gradle/wrapper/gradle-wrapper.properties | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 5919a4fd..d42b55dc 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -8,7 +8,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.5.3' + classpath 'com.android.tools.build:gradle:7.3.0' } } diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties index 01a286e9..b9fa41cb 100644 --- a/android/gradle/wrapper/gradle-wrapper.properties +++ b/android/gradle/wrapper/gradle-wrapper.properties @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.4-bin.zip diff --git a/example/android/gradle/wrapper/gradle-wrapper.properties b/example/android/gradle/wrapper/gradle-wrapper.properties index 296b146b..b0276817 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-5.6.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.4-bin.zip From 867324931e22de44a26c2d520f464e0beca4fcf3 Mon Sep 17 00:00:00 2001 From: Leo Huang Date: Tue, 30 Apr 2024 11:02:36 +1000 Subject: [PATCH 3/3] (#6) Remove deprecated usage of PluginRegistry.Registrar --- .../polidea/flutter_ble_lib/FlutterBleLibPlugin.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/android/src/main/java/com/polidea/flutter_ble_lib/FlutterBleLibPlugin.java b/android/src/main/java/com/polidea/flutter_ble_lib/FlutterBleLibPlugin.java index 6cc8b827..6888b523 100644 --- a/android/src/main/java/com/polidea/flutter_ble_lib/FlutterBleLibPlugin.java +++ b/android/src/main/java/com/polidea/flutter_ble_lib/FlutterBleLibPlugin.java @@ -4,6 +4,8 @@ import android.content.Context; import android.util.Log; +import androidx.annotation.NonNull; + import com.polidea.flutter_ble_lib.constant.ArgumentKey; import com.polidea.flutter_ble_lib.constant.ChannelName; import com.polidea.flutter_ble_lib.constant.MethodName; @@ -13,8 +15,8 @@ import com.polidea.flutter_ble_lib.delegate.DescriptorsDelegate; import com.polidea.flutter_ble_lib.delegate.DeviceConnectionDelegate; import com.polidea.flutter_ble_lib.delegate.DevicesDelegate; -import com.polidea.flutter_ble_lib.delegate.LogLevelDelegate; import com.polidea.flutter_ble_lib.delegate.DiscoveryDelegate; +import com.polidea.flutter_ble_lib.delegate.LogLevelDelegate; import com.polidea.flutter_ble_lib.delegate.MtuDelegate; import com.polidea.flutter_ble_lib.delegate.RssiDelegate; import com.polidea.flutter_ble_lib.event.AdapterStateStreamHandler; @@ -32,7 +34,6 @@ import java.util.LinkedList; import java.util.List; -import androidx.annotation.NonNull; import io.flutter.embedding.engine.plugins.FlutterPlugin; import io.flutter.embedding.engine.plugins.activity.ActivityAware; import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; @@ -42,7 +43,6 @@ import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel.MethodCallHandler; import io.flutter.plugin.common.MethodChannel.Result; -import io.flutter.plugin.common.PluginRegistry.Registrar; public class FlutterBleLibPlugin implements MethodCallHandler, FlutterPlugin, ActivityAware { @@ -94,10 +94,6 @@ private static void init(Context context, BinaryMessenger messenger, Activity ac characteristicMonitorChannel.setStreamHandler(plugin.characteristicsMonitorStreamHandler); } - public static void registerWith(Registrar registrar) { - init(registrar.context(), registrar.messenger(), registrar.activity()); - } - private void setupAdapter(Context context) { bleAdapter = BleAdapterFactory.getNewAdapter(context); delegates.add(new DeviceConnectionDelegate(bleAdapter, connectionStateStreamHandler));