美文网首页
4.Flutter混合开发-打开蓝牙(1)

4.Flutter混合开发-打开蓝牙(1)

作者: leaflying | 来源:发表于2019-10-07 17:56 被阅读0次

    因为最近上班在公司在做一些BLE方面的东西,在Futter中目前一般使用的是flutterblue,但是在使用的过程中,发现这个工具包不是特别好用,功能方面也有一些不完善,于是就想着自己通过原生去实现。我们先上源代码。

    import 'package:flutter/services.dart';
    
    class BlueToothHelper{
      static const MethodChannel methodChannel= MethodChannel('com.flutter.io/bluetooth');
      Function method;
      Future<String> openBlueTooth() async{     //打开蓝牙
        dynamic future=await methodChannel.invokeMethod('openBuleTooth');
        return future;
      }
    }
    
    package com.tdft.plugin;
    
    import android.Manifest;
    import android.app.Activity;
    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;
    import android.bluetooth.BluetoothManager;
    import android.content.Context;
    import android.content.pm.PackageManager;
    import android.os.Handler;
    import android.os.Message;
    import android.util.Log;
    import android.widget.Toast;
    
    import androidx.core.app.ActivityCompat;
    import androidx.core.content.ContextCompat;
    
    import io.flutter.plugin.common.MethodCall;
    import io.flutter.plugin.common.MethodChannel;
    import io.flutter.plugin.common.PluginRegistry;
    
    public class BluetoothPlugin implements MethodChannel.MethodCallHandler, PluginRegistry.RequestPermissionsResultListener {
        public static String CHANNEL = "com.flutter.io/bluetooth";
    
        private BluetoothAdapter mBluetoothAdapter;
        private boolean mScanning = false;
        private final int SCAN_PERIOD = 10000;
        private static final int REQUEST_COARSE_LOCATION_PERMISSIONS = 1452;
        static MethodChannel channel;
        private MethodCall pendingCall;
        private MethodChannel.Result pendingResult;
    
    
        private Handler mHandler = new Handler(){
            @Override
            public void dispatchMessage(Message msg) {
                super.dispatchMessage(msg);
                switch (msg.what){
                    case 200:
                        Log.w("leafly","onLeScan");
                        break;
                }
            }
        };
    
        private Activity activity;
        private Context context;
        private BluetoothPlugin(Activity activity) {
            this.activity = activity;
            this.context = activity.getBaseContext();
        }
    
    
        public static void registerWith(PluginRegistry.Registrar registrar) {
            channel = new MethodChannel(registrar.messenger(), CHANNEL);
            BluetoothPlugin instance = new BluetoothPlugin(registrar.activity());
            //setMethodCallHandler在此通道上接收方法调用的回调
            channel.setMethodCallHandler(instance);
    
        }
    
        @Override
        public void onMethodCall(MethodCall call, MethodChannel.Result result) {
            //通过MethodCall可以获取参数和方法名,然后再寻找对应的平台业务
            if (call.method.equals("openBuleTooth")) {
                if (!activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
                    Toast.makeText(context, "该设备不支持蓝牙", Toast.LENGTH_SHORT).show();
                    result.success("false");
                    return;
                }
    
                final BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(context.BLUETOOTH_SERVICE);
                mBluetoothAdapter = bluetoothManager.getAdapter();
                BluetoothManager mBluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
                if (mBluetoothManager != null) {
                    mBluetoothAdapter = mBluetoothManager.getAdapter();
                    if (mBluetoothAdapter != null) {
                        if (!mBluetoothAdapter.isEnabled()) {
                            mBluetoothAdapter.enable();  //打开蓝牙
                        }
                    }
                }
                result.success("success");
            }else {
                result.notImplemented();
            }
        }
    
    }
    
    private static void registerCustomPlugin(PluginRegistry registrar){
        BluetoothPlugin.registerWith(registrar.registrarFor(BluetoothPlugin.CHANNEL));
      }
    

    相关文章

      网友评论

          本文标题:4.Flutter混合开发-打开蓝牙(1)

          本文链接:https://www.haomeiwen.com/subject/btspuctx.html