Skip to content
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
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 28

namespace = "com.tablemi.flutter_bluetooth_basic"
defaultConfig {
minSdkVersion 21
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
import android.util.Log;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
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.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;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener;

import java.util.ArrayList;
Expand All @@ -34,38 +36,63 @@
import java.util.Vector;

/** FlutterBluetoothBasicPlugin */
public class FlutterBluetoothBasicPlugin implements MethodCallHandler, RequestPermissionsResultListener {
public class FlutterBluetoothBasicPlugin implements FlutterPlugin, ActivityAware, MethodCallHandler, RequestPermissionsResultListener {
private static final String TAG = "BluetoothBasicPlugin";
private int id = 0;
private ThreadPool threadPool;
private static final int REQUEST_COARSE_LOCATION_PERMISSIONS = 1451;
private static final String NAMESPACE = "flutter_bluetooth_basic";
private final Registrar registrar;
private final Activity activity;
private final MethodChannel channel;
private final EventChannel stateChannel;
private final BluetoothManager mBluetoothManager;
private Activity activity;
private MethodChannel channel;
private EventChannel stateChannel;
private BluetoothManager mBluetoothManager;
private BluetoothAdapter mBluetoothAdapter;

private MethodCall pendingCall;
private Result pendingResult;

public static void registerWith(Registrar registrar) {
final FlutterBluetoothBasicPlugin instance = new FlutterBluetoothBasicPlugin(registrar);
registrar.addRequestPermissionsResultListener(instance);
}
public FlutterBluetoothBasicPlugin() {}

FlutterBluetoothBasicPlugin(Registrar r){
this.registrar = r;
this.activity = r.activity();
this.channel = new MethodChannel(registrar.messenger(), NAMESPACE + "/methods");
this.stateChannel = new EventChannel(registrar.messenger(), NAMESPACE + "/state");
this.mBluetoothManager = (BluetoothManager) registrar.activity().getSystemService(Context.BLUETOOTH_SERVICE);
this.mBluetoothAdapter = mBluetoothManager.getAdapter();
@Override
public void onAttachedToEngine(FlutterPluginBinding binding) {
channel = new MethodChannel(binding.getBinaryMessenger(), NAMESPACE + "/methods");
stateChannel = new EventChannel(binding.getBinaryMessenger(), NAMESPACE + "/state");
channel.setMethodCallHandler(this);
stateChannel.setStreamHandler(stateStreamHandler);
}

@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
stateChannel.setStreamHandler(null);
}

@Override
public void onAttachedToActivity(ActivityPluginBinding binding) {
activity = binding.getActivity();
mBluetoothManager = (BluetoothManager) activity.getSystemService(Context.BLUETOOTH_SERVICE);
if (mBluetoothManager != null) {
mBluetoothAdapter = mBluetoothManager.getAdapter();
}
binding.addRequestPermissionsResultListener(this);
}

@Override
public void onDetachedFromActivityForConfigChanges() {
activity = null;
}

@Override
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
activity = binding.getActivity();
binding.addRequestPermissionsResultListener(this);
}

@Override
public void onDetachedFromActivity() {
activity = null;
}

@Override
public void onMethodCall(MethodCall call, Result result) {
if (mBluetoothAdapter == null && !"isAvailable".equals(call.method)) {
Expand Down