Browse Source

1.蓝牙扫描,连接,保存状态2.app图标更改,名称更改,启动页图片更改

daxu0403 7 years ago
parent
commit
7840e4c2b7

+ 1 - 1
android/app/src/main/AndroidManifest.xml

@@ -24,7 +24,7 @@
     å
     <application
         android:name="io.flutter.app.FlutterApplication"
-        android:label="logistics_client"
+        android:label="物流助手"
         android:icon="@mipmap/ic_launcher">
         <activity
             android:name=".MainActivity"

+ 2 - 2
android/app/src/main/res/drawable/launch_background.xml

@@ -4,9 +4,9 @@
     <item android:drawable="@android:color/white" />
 
     <!-- You can insert your own image assets here -->
-    <!-- <item>
+    <item>
         <bitmap
             android:gravity="center"
             android:src="@mipmap/launch_image" />
-    </item> -->
+    </item>
 </layer-list>

BIN
android/app/src/main/res/mipmap-hdpi/ic_launcher.png


BIN
android/app/src/main/res/mipmap-mdpi/ic_launcher.png


BIN
android/app/src/main/res/mipmap-xhdpi/ic_launcher.png


BIN
android/app/src/main/res/mipmap-xhdpi/launch_image.png


BIN
android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png


BIN
android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png


+ 1 - 0
lib/main.dart

@@ -9,6 +9,7 @@ class MyApp extends StatelessWidget {
   @override
   Widget build(BuildContext context) {
     return MaterialApp(
+      title: '物流助手',
       debugShowCheckedModeBanner: false,
       //debugShowMaterialGrid:true,
       home: StartPage(),

+ 82 - 44
lib/pages/my_page.dart

@@ -31,7 +31,7 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
   BluetoothUtils bluetooth = BluetoothUtils();
   List < BtInfoObj > btList = [];
   Set btSet = Set();
-  int publicIndex;
+  bool btLinkStatus = false;
   List printTypeList = ["全部", "配件商", "修理厂", "司机", "货物", "货物外", "不打印"];
   static
   var printTypeMap = {
@@ -115,12 +115,20 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
             break;
           }
         }
-        for (int i = 0; i < tempList.length; i++) {
-          BtInfoObj tempObj = BtInfoObj(name: tempList[i], showLoadStauts: false, connectStatus: false);
-          setState(() {
-            btList.add(tempObj);
-          });
-        }
+        getKey('checkedBt').then((value){
+          print(value);
+          for (int i = 0; i < tempList.length; i++) {
+             BtInfoObj tempObj;
+            if(value!=null && value==i.toString()){
+              tempObj = BtInfoObj(name: tempList[i], showLoadStauts: false, connectStatus: true);
+            }else{
+              tempObj = BtInfoObj(name: tempList[i], showLoadStauts: false, connectStatus: false);
+            }
+            setState(() {
+              btList.add(tempObj);
+            });
+          }
+        });
       }
     });
   }
@@ -129,44 +137,69 @@ 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(() {
+      if(obj.toString().indexOf('DISCAONNECTED')!=-1){//当上一个连接断开后,将连接的蓝牙动画状态关闭
+      print('**************DISCAONNECTED***************');
           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;
+            print(btList[i].connectStatus);
+            if(btList[i].connectStatus==true){
+              setState(() {
+                 btList[i].showLoadStauts = false;
+                  btLinkStatus = true;
+              });
+              setKey('checkedBt', i);
             }
-          }
-         
+           }
+            String tempStr = '';
+            for (int i = 0; i < btList.length; i++) {
+              tempStr += '${btList[i].name}&';
+            }
+            setKey('btList', tempStr);
+            return;
+      }else
+      if(obj.toString().indexOf('CONNECTED')!=-1){//连接回调成功,将连接状态改为蓝色,单扔显示连接动画
+        setState(() {
+            for(int i=0;i<btList.length;i++){
+              if(obj.toString().indexOf(btList[i].name)!=-1){
+                  if(btLinkStatus){
+                     btList[i].connectStatus = true;
+                  }else{
+                    btList[i].showLoadStauts = false;
+                    btList[i].connectStatus = true;
+                    btLinkStatus = true;
+                  }
+                   setKey('checkedBt', i.toString());
+                }else{
+                   btList[i].showLoadStauts = false;
+                  btList[i].connectStatus = false;
+                }
+              }
+              String tempStr = '';
+              for (int i = 0; i < btList.length; i++) {
+                tempStr += '${btList[i].name}&';
+              }
+              setKey('btList', tempStr);
+              return;
         });
-         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 = [];
-      for (int i = 0; i < tempBtList.length; i++) {
-          tempBtList2.add(
-            BtInfoObj(name: tempBtList[i], showLoadStauts: false, connectStatus: false)
-          );
-      }
-      setState(() {
-        btList =tempBtList2;
-      });
-      String tempStr = '';
-      for (int i = 0; i < btList.length; i++) {
-        tempStr += '${btList[i].name}&';
+       
+      }else{
+         btSet.add(obj.toString()); //去重
+          List tempBtList = btSet.toList();
+          List<BtInfoObj> tempBtList2 = [];
+          for (int i = 0; i < tempBtList.length; i++) {
+              tempBtList2.add(
+                BtInfoObj(name: tempBtList[i], showLoadStauts: false, connectStatus: false)
+              );
+          }
+          setState(() {
+            btList =tempBtList2;
+          });
+          String tempStr = '';
+          for (int i = 0; i < btList.length; i++) {
+            tempStr += '${btList[i].name}&';
+          }
+          print(tempStr);
+          setKey('btList', tempStr);
       }
-      print(tempStr);
-      setKey('btList', tempStr);
     }
   }
 
@@ -458,6 +491,7 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
                               bluetooth.enableBluetooth().then((resp){
                                   setState(() {
                                     check = true;
+                                    btLinkStatus = false;
                                   });
                                   setKey('check', 'true');
                                   bluetooth.scanBT(); //
@@ -564,8 +598,8 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
             children: < Widget > [
               Text(name),
               obj.showLoadStauts ? SizedBox(
-                width: ScreenUtil.getInstance().setWidth(35),
-                height: ScreenUtil.getInstance().setHeight(35),
+                width: ScreenUtil.getInstance().setWidth(30),
+                height: ScreenUtil.getInstance().setHeight(30),
                 child: Image.asset('lib/images/loading.gif'),
               ) :
               CircleAvatar(
@@ -578,10 +612,14 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
       ),
       onTap: () {
         if(btList[index].connectStatus) return;//已连接直接返回
+        for(int i=0;i<btList.length;i++){
+          if(btList[i].showLoadStauts==true){//任何一个蓝牙连接在加载中就直接返回
+            return;
+          }
+        }
         bluetooth.connectBT(btAddress);
          setState(() {
           btList[index].showLoadStauts = true;
-          publicIndex =index;
         });
        
       },

+ 1 - 0
lib/util/session.dart

@@ -3,6 +3,7 @@ import 'package:shared_preferences/shared_preferences.dart';
 
 void setKey(key,value) async{
   SharedPreferences prefs = await SharedPreferences.getInstance();
+  print(value);
   prefs.setString(key, value);
 }