Pārlūkot izejas kodu

android蓝牙文件

daxu0403 7 gadi atpakaļ
vecāks
revīzija
550436189b

+ 26 - 2
android/app/src/main/java/com/example/logisticsclient/MainActivity.java

@@ -57,6 +57,7 @@ public class MainActivity extends FlutterActivity {
     private Context thisCon=null;
 
     private Thread thread;
+    private static HPRTPrinterHelper HPRTPrinter=new HPRTPrinterHelper();
 
 
     @Override
@@ -174,6 +175,9 @@ public class MainActivity extends FlutterActivity {
                         IntentFilter filter5 = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
                         IntentFilter filter6 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
                         IntentFilter filter7 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
+                        IntentFilter filter8  = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
+
+
 
 
 
@@ -186,6 +190,7 @@ public class MainActivity extends FlutterActivity {
                         registerReceiver(scanBlueReceiver, filter5);
                         registerReceiver(scanBlueReceiver, filter6);
                         registerReceiver(scanBlueReceiver, filter7);
+                        registerReceiver(scanBlueReceiver, filter8);
 
 
 
@@ -395,6 +400,10 @@ public class MainActivity extends FlutterActivity {
                 String action = intent.getAction();
                 Log.d(TAG, "action:" + action);
                 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
+
+//                if(device.getBluetoothClass().getMajorDeviceClass()==1536){
+
+
                 switch (action) {
                     case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
                         eventSink.success("开始扫描...");
@@ -405,8 +414,12 @@ public class MainActivity extends FlutterActivity {
                     case BluetoothDevice.ACTION_FOUND:
 
                         if (device.getName() != null) {
-
+                            
+                            if (1536 == device.getBluetoothClass().getMajorDeviceClass() ){
                                 eventSink.success(device.getName() + "||" + device.getAddress());
+                            }
+
+
 
                         }
 
@@ -449,7 +462,8 @@ public class MainActivity extends FlutterActivity {
                         break;
 
                 }
-            }
+                }
+//            }
         };
     }
 
@@ -510,6 +524,16 @@ public class MainActivity extends FlutterActivity {
         boolean flag = false;
 
 
+        if(HPRTPrinter!=null)
+        {
+            try {
+                HPRTPrinterHelper.PortClose();
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+
+
 
 
 

+ 32 - 9
lib/pages/my_page.dart

@@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
 import 'package:flutter_screenutil/flutter_screenutil.dart';
 import 'my_page/revise_password_page.dart';
 import 'package:flutter_picker/flutter_picker.dart';
-import 'dart:convert';
 import '../util/bluetooth_utils.dart';
 import '../showDialog.dart';
 import '../login_page.dart';
@@ -60,6 +59,8 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
 
   @override
   void initState() {
+    btList.clear();
+    btSet.clear();
     super.initState();
     getStationList().then((resp) {
       if (isNotError(context, resp)) {
@@ -128,6 +129,27 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
   onEvent(Object obj) {
     print('进入onEvent回调${obj}');
     if (obj.toString().indexOf('||') != -1) { //过滤开始搜索和结束搜索
+      if(obj.toString().indexOf('CONNECTED')!=-1){
+        setState(() {
+          for(int i=0;i<btList.length;i++){
+            if(i==publicIndex){
+               btList[i].showLoadStauts = false;
+                btList[i].connectStatus = true;
+            }else{
+               btList[i].showLoadStauts = false;
+                btList[i].connectStatus = false;
+            }
+          }
+         
+        });
+         String tempStr = '';
+        for (int i = 0; i < btList.length; i++) {
+          tempStr += '${btList[i].name}&';
+        }
+        print(tempStr);
+        setKey('btList', tempStr);
+        return;
+      }
       btSet.add(obj.toString()); //去重
       List tempBtList = btSet.toList();
        List<BtInfoObj> tempBtList2 = [];
@@ -139,12 +161,10 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
       setState(() {
         btList =tempBtList2;
       });
-      print('设置btList完成');
       String tempStr = '';
       for (int i = 0; i < btList.length; i++) {
         tempStr += '${btList[i].name}&';
       }
-      print('设置临时字符串');
       print(tempStr);
       setKey('btList', tempStr);
     }
@@ -435,11 +455,13 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
                         bluetooth.isBTDevice().then((resp) {
                           if (resp) {
                             bluetooth.isEnableBluetooth().then((bool resp) { //打开蓝牙
-                              setState(() {
-                                check = true;
+                              bluetooth.enableBluetooth().then((resp){
+                                  setState(() {
+                                    check = true;
+                                  });
+                                  setKey('check', 'true');
+                                  bluetooth.scanBT(); //
                               });
-                              setKey('check', 'true');
-                              bluetooth.scanBT(); //
                             });
 
                           }
@@ -449,6 +471,7 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
                         setState(() {
                           check = false;
                           btList.clear();
+                          btSet.clear();
                         });
                         setKey('check', 'false');
                       }
@@ -554,11 +577,11 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
         ),
       ),
       onTap: () {
-        //bluetooth.connnectBT(btAddress);
+        if(btList[index].connectStatus) return;//已连接直接返回
         bluetooth.connectBT(btAddress);
          setState(() {
           btList[index].showLoadStauts = true;
-          // publicIndex =
+          publicIndex =index;
         });
        
       },