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/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..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 @@ -1,8 +1,11 @@ package com.polidea.flutter_ble_lib; +import android.app.Activity; 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; @@ -12,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; @@ -31,15 +34,17 @@ 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; +import io.flutter.plugin.common.BinaryMessenger; import io.flutter.plugin.common.EventChannel; 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; -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,10 +94,6 @@ public static void registerWith(Registrar registrar) { characteristicMonitorChannel.setStreamHandler(plugin.characteristicsMonitorStreamHandler); } - private FlutterBleLibPlugin(Context context) { - this.context = context; - } - private void setupAdapter(Context context) { bleAdapter = BleAdapterFactory.getNewAdapter(context); delegates.add(new DeviceConnectionDelegate(bleAdapter, connectionStateStreamHandler)); @@ -192,4 +209,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 {} 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