diff --git a/android/src/main/java/com/hy/print/hy_printer/Bluetooth.java b/android/src/main/java/com/hy/print/hy_printer/Bluetooth.java index 82fd3c7..b04e4ae 100644 --- a/android/src/main/java/com/hy/print/hy_printer/Bluetooth.java +++ b/android/src/main/java/com/hy/print/hy_printer/Bluetooth.java @@ -164,16 +164,17 @@ public class Bluetooth { //搜索设备 public void SearchingBTDevice() { + new CheckTypesTask().execute(); System.out.println("开始搜索"); } - public int btConn(final String address, Context context) throws Exception { -// bluetoothPort.connect(address); - //final int[] portOpen = {}; - final int[] portOpen = new int[1]; + public void btConn(final String address, Context context) throws Exception { + + System.out.println("btConn"); + btThread = new Thread(new Runnable() { @Override @@ -181,9 +182,11 @@ public class Bluetooth { // TODO Auto-generated method stub try { System.out.println(address); - portOpen[0] = PrinterHelper.portOpenBT(context,address); - PrinterHelper.logcat("portOpen:"+ portOpen[0]); - System.out.println(portOpen[0]); + final int result = PrinterHelper.portOpenBT(context,address); + PrinterHelper.logcat("portOpen result:"+ result); + System.out.println(result); + + } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -193,8 +196,6 @@ public class Bluetooth { btThread.start(); //final int result = PrinterHelper.portOpenBT(context,address); //new connBT().execute(address); - - return portOpen[0]; } //断开连接 @@ -202,9 +203,15 @@ public class Bluetooth { try { // bluetoothPort.disconnect(); PrinterHelper.portClose(); - if ((btThread != null) && (btThread.isAlive())) - btThread.interrupt(); - +// if ((btThread != null) && (btThread.isAlive())) +// btThread.interrupt(); + if ((btThread != null) && (btThread.isAlive())) { + Thread dummy = btThread; + btThread = null; + dummy.interrupt(); + }else{ + return; + } disconnectflags = true; context.unregisterReceiver(connectDevice); @@ -271,53 +278,45 @@ public class Bluetooth { - class connBT extends AsyncTask { - - - @Override - protected Integer doInBackground(String... strings) { - Integer retVal = null; - - //bluetoothPort.connect(strings[0]); - - retVal = Integer.valueOf(0); - - return retVal; - } - - @Override - protected void onPreExecute() { - - super.onPreExecute(); - } - - @Override - protected void onPostExecute(Integer result) { - if (result.intValue() == 0) // Connection success. - { -// RequestHandler rh = new RequestHandler(); -// btThread = new Thread(rh); -// btThread.start(); - } else // Connection failed. - { - - } - super.onPostExecute(result); - } - } +// class connBT extends AsyncTask { +// +// +// @Override +// protected Integer doInBackground(String... strings) { +// Integer retVal = null; +// +// //bluetoothPort.connect(strings[0]); +// +// retVal = Integer.valueOf(0); +// +// return retVal; +// } +// +// @Override +// protected void onPreExecute() { +// +// super.onPreExecute(); +// } +// +// @Override +// protected void onPostExecute(Integer result) { +// if (result.intValue() == 0) // Connection success. +// { +//// RequestHandler rh = new RequestHandler(); +//// btThread = new Thread(rh); +//// btThread.start(); +// } else // Connection failed. +// { +// +// } +// super.onPostExecute(result); +// } +// } public void ExcuteDisconnect(Context context) { - new ExcuteDisconnectBT().execute(context); - - } - - private class ExcuteDisconnectBT extends AsyncTask { - - - @Override - protected Void doInBackground(Context... contexts) { +// new ExcuteDisconnectBT().execute(context); try { - DisconnectDevice(contexts[0]); + DisconnectDevice(context); while (true) { if (disconnectflags) @@ -329,24 +328,45 @@ public class Bluetooth { // TODO Auto-generated catch block e.printStackTrace(); } - return null; - } - @Override - protected void onPreExecute() { - super.onPreExecute(); - } - - ; - - - @Override - protected void onPostExecute(Void result) { - disconnectflags = false; - super.onPostExecute(result); - } - - ; } +// private class ExcuteDisconnectBT extends AsyncTask { +// +// +// @Override +// protected Void doInBackground(Context... contexts) { +// try { +// DisconnectDevice(contexts[0]); +// +// while (true) { +// if (disconnectflags) +// break; +// +// Thread.sleep(100); +// } +// } catch (InterruptedException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// return null; +// } +// +// @Override +// protected void onPreExecute() { +// super.onPreExecute(); +// } +// +// ; +// +// +// @Override +// protected void onPostExecute(Void result) { +// disconnectflags = false; +// super.onPostExecute(result); +// } +// +// ; +// } + } diff --git a/android/src/main/java/com/hy/print/hy_printer/HyPrinterPlugin.java b/android/src/main/java/com/hy/print/hy_printer/HyPrinterPlugin.java index ef5edd6..903f752 100644 --- a/android/src/main/java/com/hy/print/hy_printer/HyPrinterPlugin.java +++ b/android/src/main/java/com/hy/print/hy_printer/HyPrinterPlugin.java @@ -4,7 +4,6 @@ import android.content.Context; import androidx.annotation.NonNull; -import cpcl.PrinterHelper; import io.flutter.embedding.engine.plugins.FlutterPlugin; import io.flutter.plugin.common.EventChannel; import io.flutter.plugin.common.MethodCall; @@ -66,14 +65,12 @@ public class HyPrinterPlugin implements FlutterPlugin, MethodCallHandler { if (call.method.equals("startSearch")) { bluetooth.SearchingBTDevice(); } else if (call.method.equals("connect")) { - int code; String arg = call.argument("address"); - System.out.println(arg); + try { - code = bluetooth.btConn(arg,context); - System.out.println(code); - //PrinterHelper.portOpenBT(context,"04:7F:0E:95:FD:A1"); - result.success(code); + System.out.println("开始打印"); + bluetooth.btConn(arg,context); + result.success(true); } catch (Exception e) { e.printStackTrace(); } @@ -105,19 +102,11 @@ public class HyPrinterPlugin implements FlutterPlugin, MethodCallHandler { } result.success(sta); } else if (call.method.equals("disposeDiscovery")) { - try { - PrinterHelper.portClose(); - } catch (Exception e) { - e.printStackTrace(); - } + bluetooth.cancelDiscoveryResult(context); result.success(true); } else if (call.method.equals("disposeConnect")) { - try { - PrinterHelper.portClose(); - } catch (Exception e) { - e.printStackTrace(); - } + bluetooth.ExcuteDisconnect(context); result.success(true); } diff --git a/example/lib/main.dart b/example/lib/main.dart index 9ebb0f8..3c43ef4 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -21,33 +21,14 @@ class MyApp extends StatefulWidget { class _MyAppState extends State { List devices = []; + var cancel; @override void initState() { super.initState(); initPlatformState(); } - // Future initPlatformState() async { - // if (!mounted) return; - // setState(() {}); - // } Future initPlatformState() async { - // var per = await Permission.bluetooth.isGranted; - // if (!per) { - // Permission.bluetooth.request(); - // } - // var pers = await Permission.locationWhenInUse.isGranted; - // if (!pers) { - // Permission.locationWhenInUse.request(); - // } - // var per1 = await Permission.bluetoothScan.isGranted; - // if (!per1) { - // Permission.bluetoothScan.request(); - // } - // var per2 = await Permission.bluetoothConnect.isGranted; - // if (!per2) { - // Permission.bluetoothConnect.request(); - // } DjPrinter().init(); DjPrinter().addDiscoveryListen(onReceive: (data) { @@ -58,25 +39,23 @@ class _MyAppState extends State { address: js['address'], isPaired: js['isPaired'])); } - - - setState(() {}); }, onStart: () { print("————————————————————————"); }, onFinish: () { print('——————————————————————————————'); DjPrinter().cancelDiscovery(); + cancel(); }); DjPrinter().addConnectListen(onConnect: () { print("connected"); }, onDisconnect: () { print('disconnected'); }); + print('jieshu'); } - @override Widget build(BuildContext context) {return MaterialApp( title: '安速货运', @@ -87,10 +66,130 @@ class _MyAppState extends State { child: BotToastInit().call(context, widget)); }, navigatorObservers: [BotToastNavigatorObserver()], - home: const ScanPage() + home: Scaffold( + backgroundColor: Colors.white, + appBar: AppBar( + title: const Text('设备列表',style: TextStyle(color: Colors.white),), + ), + body: Center( + child: Column( + children: [ + TextButton( + onPressed: () { + devices.clear(); + setState(() {}); + cancel = + BotToast.showLoading(wrapToastAnimation: (controller, func, child) { + return discoveryLoadingWidget(); + }); + DjPrinter().startSearch; + }, + child: const Text('扫描设备')), + // TextButton(onPressed: () {}, child: const Text('打印')), + const SizedBox( + height: 20, + ), + ...devices + .map((e) => TextButton( + onPressed: ()async { + BotToast.showLoading(wrapToastAnimation: (controller, func, child) { + return discoveryLoadingWidget(); + }); + + + await DjPrinter().connect(e.address).then((value) { + print("main value result"); + print(value); + if(value!=null&&value){ + BotToast.closeAllLoading(); + } + }); + // Future.delayed(Duration(seconds: 3), () async { + // if(value==0){ + // BotToast.closeAllLoading(); + // } + // }); + + + }, + child: Column( + children: [ + Text(e.name), + Text(e.address), + ], + ))) + .toList(), + const SizedBox( + height: 20, + ), + TextButton( + onPressed: () { + DjPrinter().printNewAScode( + code: 'ASSZ000000002', + barCode: 'ASSZ0000000020001', + channel: '加拿大温哥华海派快线-卡派 / UPS派送', + country: '美国啊啊', + num: '2', + sum:'99', + offset: 0, + hasPlan: true, ); + }, + child: const Text('nocode打印')), + TextButton( + onPressed: () { + + DjPrinter().printNewAScode( + code: 'ASSZ000000002', + barCode: 'ASSZ0000000020001', + channel: '加拿大温哥华海派快线-卡派 / UPS派送', + country: '美国', + num: '2', + sum:'999', + offset: 0, + hasPlan: false, ); + }, + child: const Text('noplan打印')), + const SizedBox( + height: 20, + ), + TextButton( + onPressed: () { + DjPrinter().disposeConnect(); + }, + child: const Text('取消链接')), + + ], + ), + ), + ) ); } + Container discoveryLoadingWidget() { + return Container( + padding: const EdgeInsets.all(15), + decoration: const BoxDecoration( + color: Colors.black54, + borderRadius: BorderRadius.all(Radius.circular(8))), + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + CircularProgressIndicator( + backgroundColor: Colors.white, + ), + + Text( + '扫描中……请稍作等待', + style: TextStyle( + color: Colors.white, + fontSize: 18, + fontWeight: FontWeight.bold, + ), + ) + ], + ), + ); + } } diff --git a/example/lib/scan_page.dart b/example/lib/scan_page.dart index c33cb9c..4a89260 100644 --- a/example/lib/scan_page.dart +++ b/example/lib/scan_page.dart @@ -69,6 +69,7 @@ class _ScanPageState extends State { }, onDisconnect: () { print('disconnected'); }); + print('jieshu'); } @@ -77,8 +78,9 @@ class _ScanPageState extends State { Widget build(BuildContext context) { return Scaffold( + backgroundColor: Colors.white, appBar: AppBar( - title: const Text('设备列表'), + title: const Text('设备列表',style: TextStyle(color: Colors.white),), ), body: Center( child: Column( @@ -101,19 +103,8 @@ class _ScanPageState extends State { ...devices .map((e) => TextButton( onPressed: ()async { - BotToast.showLoading(wrapToastAnimation: (controller, func, child) { - return discoveryLoadingWidget(); - }); - - - int? value = await DjPrinter().connect(e.address); - Future.delayed(Duration(seconds: 3), () async { - if(value==0){ - BotToast.closeAllLoading(); - }else{ - BotToast.closeAllLoading(); - } - }); + + await DjPrinter().connect(e.address); }, diff --git a/lib/dj_printer.dart b/lib/dj_printer.dart index 728500e..99274af 100644 --- a/lib/dj_printer.dart +++ b/lib/dj_printer.dart @@ -78,14 +78,10 @@ class DjPrinter { bool get hasInit => _hasInit; - Future connect(String address) async { + Future connect(String address) async { - await Future.delayed(const Duration(milliseconds: 2000), () async { - var res = await _channel.invokeMethod('connect', {'address': address}); - print('connect'); - print(res); + final res = await _channel.invokeMethod('connect', {'address': address}); return res; - }); } void disposeDiscovery() {