daxu0403 7 سال پیش
والد
کامیت
0331b731ef

+ 3 - 0
android/app/src/main/java/com/example/logisticsclient/MainActivity.java

@@ -41,6 +41,9 @@ import io.flutter.plugin.common.MethodChannel;
 import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
 import io.flutter.plugin.common.MethodChannel.Result;
 
+import android.support.v4.app.ActivityCompat;
+import android.support.v4.content.ContextCompat;
+
 
 public class MainActivity extends FlutterActivity {
   private static final String CHANNEL = "io.fivefire/method_channel";

+ 20 - 5
lib/pages/my_page.dart

@@ -54,8 +54,17 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
         setKey('stationId', stationObj.data[0].id.toString());
       } 
     });
+
+    bluetooth.getEchannel().receiveBroadcastStream().listen(onEvent,onError:onError);
+  }
+
+  onEvent(Object obj){
+    print(obj.toString());
   }
 
+  onError(Object obj){
+    print(obj.toString());
+  }
   @override
   Widget build(BuildContext context) {
     return Container(
@@ -286,12 +295,18 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
                     value: this.check,
                     activeColor: Colors.blue,
                     onChanged: (bool val) {
-                      bluetooth.isEnableBluetooth().then((bool resp){
-                         this.setState(() {
-                            this.check = resp;
-                          });
-                      });
+                      bluetooth.isBTDevice().then((resp){
+                        if(resp){
+                           bluetooth.isEnableBluetooth().then((bool resp){
+                              this.setState(() {
+                                  this.check = resp;
+                                });
+                                bluetooth.scanBT();//
+                            });
 
+                        }
+                      });
+                     
                       
                      
                       //print('check:${this.check}');

+ 2 - 2
lib/util/bluetooth_utils.dart

@@ -4,7 +4,7 @@ class BluetoothUtils {
   static const mChannel = const MethodChannel('io.fivefire/method_channel');
   static const eChannel = const EventChannel("io.fivefire/event_channel");
 
-  Future<bool> isBTDevice() async {//检查是否有蓝牙设备
+  Future<bool> isBTDevice() async {//检查本机是否有蓝牙设备
     try {
       return await mChannel.invokeMethod('isBTDevice');
     } on PlatformException catch (e) {
@@ -22,7 +22,7 @@ class BluetoothUtils {
     }
   }
 
-  Future scanBT() async {
+  Future scanBT() async {//开始搜索蓝牙设备
     try {
       return await mChannel.invokeMethod('scanBT');
     } on PlatformException catch (e) {