Skip to content

Commit

Permalink
Merge pull request #7 from cbreezier/v2-embedding
Browse files Browse the repository at this point in the history
V2 embedding
  • Loading branch information
cbreezier authored Apr 30, 2024
2 parents 99849bb + 8673249 commit 254d509
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 30 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.android.tools.build:gradle:7.3.0'
}
}

Expand Down
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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();

Expand All @@ -53,16 +58,32 @@ public class FlutterBleLibPlugin implements MethodCallHandler {

private List<CallDelegate> 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);

Expand All @@ -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));
Expand Down Expand Up @@ -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;
}
}
4 changes: 3 additions & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name="io.flutter.app.FlutterApplication"
android:label="flutter_ble_lib_example"
android:icon="@mipmap/ic_launcher">
<activity
Expand All @@ -31,4 +30,7 @@
</intent-filter>
</activity>
</application>
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</manifest>
Original file line number Diff line number Diff line change
@@ -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 {}
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-5.6.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.4-bin.zip

0 comments on commit 254d509

Please sign in to comment.