diff --git a/.idea/libraries/Dart_SDK.xml b/.idea/libraries/Dart_SDK.xml index 82934cc..bf2e4a6 100644 --- a/.idea/libraries/Dart_SDK.xml +++ b/.idea/libraries/Dart_SDK.xml @@ -1,17 +1,25 @@ - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + diff --git a/.idea/libraries/Flutter_Plugins.xml b/.idea/libraries/Flutter_Plugins.xml index d281870..c3a4d75 100644 --- a/.idea/libraries/Flutter_Plugins.xml +++ b/.idea/libraries/Flutter_Plugins.xml @@ -1,8 +1,9 @@ + + - diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/android/.idea/.gitignore b/android/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/android/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/android/.idea/.name b/android/.idea/.name new file mode 100644 index 0000000..d382d5f --- /dev/null +++ b/android/.idea/.name @@ -0,0 +1 @@ +hy_printer \ No newline at end of file diff --git a/android/.idea/compiler.xml b/android/.idea/compiler.xml new file mode 100644 index 0000000..fb7f4a8 --- /dev/null +++ b/android/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/android/.idea/gradle.xml b/android/.idea/gradle.xml new file mode 100644 index 0000000..15345bf --- /dev/null +++ b/android/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/android/.idea/jarRepositories.xml b/android/.idea/jarRepositories.xml new file mode 100644 index 0000000..d2ce72d --- /dev/null +++ b/android/.idea/jarRepositories.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/android/.idea/misc.xml b/android/.idea/misc.xml new file mode 100644 index 0000000..6199cc2 --- /dev/null +++ b/android/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/android/.idea/modules.xml b/android/.idea/modules.xml new file mode 100644 index 0000000..0305d62 --- /dev/null +++ b/android/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/android/.idea/runConfigurations.xml b/android/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/android/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/android/.idea/vcs.xml b/android/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/android/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/android/build.gradle b/android/build.gradle index c0e3ead..8f09bef 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -31,7 +31,7 @@ android { } defaultConfig { - minSdkVersion 19 + minSdkVersion 16 } sourceSets { main { @@ -42,8 +42,8 @@ android { dependencies{ implementation fileTree(dir: "libs", include: ["*.jar"]) - implementation files('libs/CPCL_SDK_V1.11.jar') - + implementation files('libs/CPCL_SDK_V1.12.jar') + implementation 'androidx.annotation:annotation:1.3.0' } diff --git a/android/libs/CPCL_SDK_V1.11.jar b/android/libs/CPCL_SDK_V1.12.jar similarity index 72% rename from android/libs/CPCL_SDK_V1.11.jar rename to android/libs/CPCL_SDK_V1.12.jar index 52365e6..83c8e5b 100644 Binary files a/android/libs/CPCL_SDK_V1.11.jar and b/android/libs/CPCL_SDK_V1.12.jar differ diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index 79cbb3a..eb20f34 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -1,7 +1,15 @@ - + - + + + - + + + + + + + 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 new file mode 100644 index 0000000..4d0f498 --- /dev/null +++ b/android/src/main/java/com/hy/print/hy_printer/Bluetooth.java @@ -0,0 +1,349 @@ +package com.hy.print.hy_printer; + +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.AsyncTask; + +import java.io.IOException; + +import cpcl.PrinterHelper; +import io.flutter.plugin.common.EventChannel; + +//import com.sewoo.port.android.BluetoothPort; +//import com.sewoo.request.android.RequestHandler; + + +public class Bluetooth { + private BluetoothAdapter mBluetoothAdapter; + //private BluetoothPort bluetoothPort; + + private BroadcastReceiver connectDevice; + private BroadcastReceiver discoveryResult; + private BroadcastReceiver searchFinish; + private BroadcastReceiver searchStart; + + private Thread btThread; + + private boolean searchflags; + private boolean disconnectflags; + + public void init() { + +// bluetoothPort = BluetoothPort.getInstance(); +// bluetoothPort.SetMacFilter(false); + bluetoothSetup(); + searchflags = false; + disconnectflags = false; + System.out.println("打印机蓝牙初始化完成"); + } + + //初始化连接监听 + public void createConnectBroadcast(Context context, EventChannel.EventSink eventSink) { + + connectDevice = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { + eventSink.success("connected"); + } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) { + try { + PrinterHelper.portClose(); +// if (bluetoothPort.isConnected()) { +// System.out.println("1111s"); +// bluetoothPort.disconnect(); +// } + if ((btThread != null) && (btThread.isAlive())) { + cancelThread(); + } + System.out.println("disconnected"); + eventSink.success("disconnected"); + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + }; + + context.registerReceiver(connectDevice, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED)); + context.registerReceiver(connectDevice, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED)); + System.out.println("注册连接广播"); + } + + public boolean isValidAddress(String address) { + String[] btDevAddr = new String[]{"00:12:6f", "00:13:7b", "00:07:80", "88:6b:0f", "40:19:20", "00:01:90", "ec:1b:bd", "58:8e:81", "14:b4:57", "90:fd:9f", "00:0b:57", "68:0a:e2", "84:2e:14", "08:6b:d7", "60:a4:23", "cc:cc:cc", "bc:33:ac", "74:f0:7d", "84:fd:27", "84:71:27", "80:4b:50", "5c:02:72", "cc:86:ec", "2c:11:65", "b4:e3:f9", "0c:43:14", "50:32:5f", "04:cd:15", "8c:f6:81", "90:35:ea"}; + if (!BluetoothAdapter.checkBluetoothAddress(address)) { + return false; + } else { + for(int i = 0; i < btDevAddr.length; ++i) { + if (btDevAddr[i].equalsIgnoreCase(address.substring(0, 8))) { + return true; + } + } + + return false; + } + } + + //初始化搜索设备监听 + public void createDiscoveryBroadcast(Context context, EventChannel.EventSink eventSink) { + discoveryResult = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String key = ""; + BluetoothDevice remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); + String name = remoteDevice.getName(); + System.out.println(name); + System.out.println(remoteDevice.getAddress()); + if (remoteDevice != null && name != null) { + if (remoteDevice.getBondState() != BluetoothDevice.BOND_BONDED) { + key = "{" + "\"name\":" + "\"" + name + "\"" + "," + + "\"address\":" + "\"" + remoteDevice.getAddress() + "\"" + "," + + "\"isPaired\":" + false + + "}"; + + } else { + + key = "{" + "\"name\":" + "\"" + name + "\"" + "," + + "\"address\":" + "\"" + remoteDevice.getAddress() + "\"" + "," + + "\"isPaired\":" + true + + "}"; + } + System.out.println(key); + eventSink.success(key); + System.out.println(key); +// if (isValidAddress(remoteDevice.getAddress())) { +// eventSink.success(key); +// System.out.println(key); +// } + } + + } + }; + context.registerReceiver(discoveryResult, new IntentFilter(BluetoothDevice.ACTION_FOUND)); + searchStart = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + eventSink.success("start"); + System.out.println("start"); + } + }; + context.registerReceiver(searchStart, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED)); + + searchFinish = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + eventSink.success("finish"); + searchflags = true; + System.out.println("finish"); + } + }; + context.registerReceiver(searchFinish, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)); + System.out.println("注册搜索广播"); + } + + //蓝牙设置 + private void bluetoothSetup() { + + mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + if (mBluetoothAdapter == null) { + System.out.println("不支持蓝牙"); + } + } + + + //搜索设备 + 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 = {-4}; + btThread = new Thread(new Runnable() { + + @Override + public void run() { + // 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]); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + }); + btThread.start(); + //final int result = PrinterHelper.portOpenBT(context,address); + //new connBT().execute(address); + + return portOpen[0]; + } + + //断开连接 + public void DisconnectDevice(Context context) { + try { + // bluetoothPort.disconnect(); + PrinterHelper.portClose(); + if ((btThread != null) && (btThread.isAlive())) + btThread.interrupt(); + + disconnectflags = true; + context.unregisterReceiver(connectDevice); + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + //取消搜索 + public void cancelDiscoveryResult(Context context) { + System.out.println("取消搜索"); + mBluetoothAdapter.cancelDiscovery(); + context.unregisterReceiver(discoveryResult); + context.unregisterReceiver(searchStart); + context.unregisterReceiver(searchFinish); + } + + + public void cancelThread() { + btThread.interrupt(); + btThread = null; + } + + private class CheckTypesTask extends AsyncTask { + + @Override + protected void onPreExecute() { + mBluetoothAdapter.startDiscovery(); + super.onPreExecute(); + } + + ; + + @Override + protected Void doInBackground(Void... params) { + // TODO Auto-generated method stub + try { + while (true) { + if (searchflags) + break; + + Thread.sleep(100); + } + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + + @Override + protected void onPostExecute(Void result) { + + searchflags = false; + 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) { + 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 bb9a330..b8a72f1 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,8 +4,8 @@ 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; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel.MethodCallHandler; @@ -15,62 +15,100 @@ import io.flutter.plugin.common.MethodChannel.Result; * HyPrinterPlugin */ public class HyPrinterPlugin implements FlutterPlugin, MethodCallHandler { - /// The MethodChannel that will the communication between Flutter and native Android - /// - /// This local reference serves to register the plugin with the Flutter Engine and unregister it - /// when the Flutter Engine is detached from the Activity + +// private MethodChannel channel; +// private Context context; + private MethodChannel channel; + private EventChannel discoveryChannel; + private EventChannel connectChannel; private Context context; + private static final String DISCOVERYDEVICE = "com.discovery.devices"; + private static final String CONNECT = "com.connect"; + PrintAsOrder printAsOrder; + Bluetooth bluetooth = new Bluetooth(); @Override public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) { channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "hy_printer"); channel.setMethodCallHandler(this); context = flutterPluginBinding.getApplicationContext(); + discoveryChannel = new EventChannel(flutterPluginBinding.getBinaryMessenger(), DISCOVERYDEVICE); + + connectChannel = new EventChannel(flutterPluginBinding.getBinaryMessenger(), CONNECT); + discoveryChannel.setStreamHandler(new EventChannel.StreamHandler() { + @Override + public void onListen(Object arguments, EventChannel.EventSink events) { + bluetooth.createDiscoveryBroadcast(context, events); + } + + @Override + public void onCancel(Object arguments) { + bluetooth.cancelDiscoveryResult(context); + } + }); + connectChannel.setStreamHandler(new EventChannel.StreamHandler() { + @Override + public void onListen(Object arguments, EventChannel.EventSink events) { + bluetooth.createConnectBroadcast(context, events); + } + + @Override + public void onCancel(Object arguments) { + bluetooth.ExcuteDisconnect(context); + } + }); } @Override - public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { - if (call.method.equals("connect")) { + public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { + if (call.method.equals("startSearch")) { + bluetooth.SearchingBTDevice(); + } else if (call.method.equals("connect")) { int code = -4; + String arg = call.argument("address"); + System.out.println(arg); try { - code = PrinterHelper.portOpenBT(context, (String) call.argument("address")); + code = bluetooth.btConn(arg,context); + System.out.println(code); + //PrinterHelper.portOpenBT(context,"04:7F:0E:95:FD:A1"); + result.success(code); } catch (Exception e) { e.printStackTrace(); } - result.success(code); - } else if (call.method.equals("disConnect")) { - boolean status = false; - try { - status = PrinterHelper.portClose(); - } catch (Exception e) { - e.printStackTrace(); - } - result.success(status); + } else if (call.method.equals("init")) { + System.out.println("打印机初始化"); + bluetooth.init(); + printAsOrder = new PrintAsOrder(); + + result.success(true); + } else if (call.method.equals("newPrint")) { + System.out.println("newPrint"); + String code = call.argument("code"); + String barCode = call.argument("barCode"); + String channel = call.argument("channel"); + String country = call.argument("country"); + String num = call.argument("num"); + String sum = call.argument("sum"); + int offset = call.argument("offset"); + boolean hasPlan = call.argument("hasPlan"); + + printAsOrder.printAsCodeNew(code, barCode, channel, country, num, sum, offset, hasPlan); + result.success(true); } else if (call.method.equals("getStatus")) { - int status = -1; - try { - status = PrinterHelper.getstatus(); - } catch (Exception e) { - e.printStackTrace(); - } - result.success(status); - } else if (call.method.equals("printAsOrder")) { - int re = PrintAsOrder.print(call.argument("code"), call.argument("fbaCode"), call.argument("country"), call.argument("channel"), call.argument("count"), call.argument("hasPlan")); - result.success(re); - } else if (call.method.equals("printBarCode")) { - int re = -1; + int sta = 0; try { - PrinterHelper.printAreaSize("0", "200", "200", "500", "1"); - re = PrinterHelper.Barcode(PrinterHelper.BARCODE, PrinterHelper.code128, call.argument("width"), call.argument("ratio"), call.argument("height"), call.argument("x"), call.argument("y"), call.argument("undertext"), "7", call.argument("size"), call.argument("offset"), call.argument("data")); - PrinterHelper.Form(); - PrinterHelper.Print(); + sta = printAsOrder.getStatus(); } catch (Exception e) { e.printStackTrace(); } - result.success(re); - } else { - result.notImplemented(); + result.success(sta); + } else if (call.method.equals("disposeDiscovery")) { + bluetooth.cancelDiscoveryResult(context); + result.success(true); + } else if (call.method.equals("disposeConnect")) { + bluetooth.ExcuteDisconnect(context); + result.success(true); } } diff --git a/android/src/main/java/com/hy/print/hy_printer/PrintAsOrder.java b/android/src/main/java/com/hy/print/hy_printer/PrintAsOrder.java index 67afe1b..a34f401 100644 --- a/android/src/main/java/com/hy/print/hy_printer/PrintAsOrder.java +++ b/android/src/main/java/com/hy/print/hy_printer/PrintAsOrder.java @@ -3,6 +3,7 @@ package com.hy.print.hy_printer; import cpcl.PrinterHelper; public class PrintAsOrder { + private static void setBold() { try { PrinterHelper.SetBold("2"); @@ -19,18 +20,7 @@ public class PrintAsOrder { } } - private static void printBarCode(String code) { - String width = "2"; - if (code.length() > 18) { - width = "1"; - } - try { - PrinterHelper.Barcode(PrinterHelper.BARCODE, PrinterHelper.code128, width, "1", "130", "10", "26", false, "", "", "", code); - } catch (Exception e) { - e.printStackTrace(); - } - } public static int print(String code, String fbaCode, String country, String channel, String count, Boolean hasPlan) { int re = 0; @@ -49,7 +39,7 @@ public class PrintAsOrder { try { PrinterHelper.printAreaSize("0", "200", "200", "400", "1"); PrinterHelper.Align(PrinterHelper.CENTER); - printBarCode(code); +// printBarCode(code); PrinterHelper.Align(PrinterHelper.LEFT); PrinterHelper.SetBold("3"); PrinterHelper.AutCenter(PrinterHelper.TEXT, "45", "170", 500, 4, code); @@ -83,7 +73,7 @@ public class PrintAsOrder { try { PrinterHelper.printAreaSize("0", "200", "200", "400", "1"); PrinterHelper.Align(PrinterHelper.CENTER); - printBarCode(code); +// printBarCode(code); PrinterHelper.Align(PrinterHelper.LEFT); PrinterHelper.SetBold("3"); PrinterHelper.AutCenter(PrinterHelper.TEXT, "45", "170", 500, 4, code); @@ -116,4 +106,215 @@ public class PrintAsOrder { System.out.println("print finish"); return reusult; } + + + public void printAsCodeNew(String code, String barCode, String channel, String country, String num, String sum, + int offset, boolean hasPlan) { + + if (hasPlan) { + PrintPlanNew(code, barCode, channel, country, num, sum, offset); + } else { + PrintNoPlanNew(code, barCode, num, sum, offset); + } + } + + private void PrintPlanNew(String code, String barCode, String channel, String country, String num, String sum, + int offset) { + System.out.println("print start"); + int cx = 0,cy = 0; + int cx1 = 0,cy1 = 0; + String country1 = ""; + String cMultiple = "3"; + String cSize = "55"; + String channel1 = ""; + if(country.length()>5){ + cMultiple = "3"; + cx = 65;cy = 220; + country1 = country.substring(4); + cx1 = (165-country1.length()*25); + cy1 = 280; + + country = country.substring(0,4); + + }else if(country.length()==5){ + cMultiple = "3";cx = 45;cy = 250; + cSize = "55"; + }else if(country.length()==4){ + cMultiple = "4";cx = 40;cy = 245; + cSize = "55"; + } + else { + cMultiple = "3";cy = 245;cx = 175-country.length()*40; + cSize = "60"; + } + int channelY = 460; + int channel1Y = 460; + if(channel.length()>11){ + channelY = 440; + channel1Y = 490; + channel1 = channel.substring(11); + channel = channel.substring(0,11); + }else{ + channelY = 460; + } + + System.out.println("cx1"); + System.out.println(cx1); + + System.out.println("country1"); + System.out.println(country1); + + try { + PrinterHelper.printAreaSize("0", "200", "200", "800", "1"); + PrinterHelper.Box("0","10","575","790","3"); + + + PrinterHelper.Line("0", "160", "575", "160", "3"); + PrinterHelper.Line("0", "400", "575", "400", "3"); + PrinterHelper.Line("0", "560", "575", "560", "3"); + PrinterHelper.Line("380", "360", "540", "200", "3"); + PrinterHelper.Line("333", "160", "333", "400", "3"); + + PrinterHelper.SetBold("3"); + PrinterHelper.SetMag("5","5"); + PrinterHelper.Align(PrinterHelper.CENTER); + PrinterHelper.Text(PrinterHelper.TEXT, "1", "0", "0", "55", code); + + +// PrinterHelper.Text(PrinterHelper.TEXT, "50", "0", "100", "0", code); +// PrinterHelper.Text(PrinterHelper.TEXT, "24", "0", "200", "0", code); + + + + closeBold(); + PrinterHelper.SetMag("1","1"); + + + + PrinterHelper.Align(PrinterHelper.LEFT); + PrinterHelper.SetBold("3"); + PrinterHelper.SetMag(cMultiple,cMultiple); + PrinterHelper.Text(PrinterHelper.TEXT,cSize , "0",String.valueOf(cx), String.valueOf(cy), country); + PrinterHelper.Align(PrinterHelper.LEFT); + if(!country1.isEmpty()){ + PrinterHelper.Text(PrinterHelper.TEXT,cSize , "0",String.valueOf(cx1), String.valueOf(cy1), country1); + } + + closeBold(); + PrinterHelper.SetMag("1","1"); + + PrinterHelper.SetBold("2"); + PrinterHelper.SetMag("2.5","2.5"); + PrinterHelper.Text(PrinterHelper.TEXT, "50", "0", "390", "210", num); + + PrinterHelper.Text(PrinterHelper.TEXT, "50", "0", String.valueOf(500-sum.length()*10), String.valueOf(315), sum); + closeBold(); + PrinterHelper.SetMag("1","1"); + + PrinterHelper.Align(PrinterHelper.CENTER); + printBarCode(barCode,"0","590"); + + + +// PrinterHelper.SetBold("2"); + PrinterHelper.SetMag("2","2"); + PrinterHelper.Text(PrinterHelper.TEXT,"3" , "0","0", String.valueOf(channelY), channel); + + if(!channel1.isEmpty()){ + PrinterHelper.Text(PrinterHelper.TEXT,"3" , "0","0", String.valueOf(channel1Y), channel1); + } + closeBold(); + PrinterHelper.SetMag("1","1"); + + PrinterHelper.Form(); + PrinterHelper.Print(); + } catch (Exception e) { + e.printStackTrace(); + + } + System.out.println("print finish"); + + + } + + + private static void printBarCode(String code,String x,String y) { + + String width = "1.5"; + if (code.length() > 18) { + width = "1"; + } + try { + PrinterHelper.Barcode(PrinterHelper.BARCODE, PrinterHelper.code128, width, "0", "130", x, y, true, "15", "15", "15", code); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void PrintNoPlanNew(String code, String barCode, String num, String sum, + int offset) { + + System.out.println("print start"); + + try { + PrinterHelper.printAreaSize("0", "200", "200", "800", "1"); + PrinterHelper.Box("0","10","575","790","3"); + + + PrinterHelper.Line("0", "160", "575", "160", "3"); + PrinterHelper.Line("0", "400", "575", "400", "3"); + + PrinterHelper.Line("380", "360", "540", "200", "3"); + PrinterHelper.Line("333", "160", "333", "400", "3"); + + PrinterHelper.SetBold("3"); + PrinterHelper.SetMag("5","5"); + PrinterHelper.Align(PrinterHelper.CENTER); + PrinterHelper.Text(PrinterHelper.TEXT, "1", "0", "0", "55", code); + + +// PrinterHelper.Text(PrinterHelper.TEXT, "50", "0", "100", "0", code); +// PrinterHelper.Text(PrinterHelper.TEXT, "24", "0", "200", "0", code); + + + + closeBold(); + PrinterHelper.SetMag("1","1"); + + + + PrinterHelper.Align(PrinterHelper.LEFT); + PrinterHelper.SetBold("3"); + + PrinterHelper.SetMag("4","4"); + PrinterHelper.Text(PrinterHelper.TEXT,"55" , "0","40","245", "未建计划"); + + closeBold(); + PrinterHelper.SetMag("1","1"); + + PrinterHelper.SetBold("2"); + PrinterHelper.SetMag("2.5","2.5"); + PrinterHelper.Text(PrinterHelper.TEXT, "50", "0", "390", "210", num); + + PrinterHelper.Text(PrinterHelper.TEXT, "50", "0", String.valueOf(500-sum.length()*10), String.valueOf(315), sum); + closeBold(); + PrinterHelper.SetMag("1","1"); + + PrinterHelper.Align(PrinterHelper.CENTER); + printBarCode(barCode,"0","520"); + + + + PrinterHelper.Form(); + PrinterHelper.Print(); + } catch (Exception e) { + e.printStackTrace(); + + } + System.out.println("print finish"); + } + + public int getStatus() throws Exception { + return PrinterHelper.getstatus(); + } } diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 4862fcd..615abf9 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -25,7 +25,7 @@ apply plugin: 'com.android.application' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { - compileSdkVersion 30 + compileSdkVersion 33 compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 @@ -54,3 +54,8 @@ android { flutter { source '../..' } +dependencies{ + implementation fileTree(dir: "libs", include: ["*.jar"]) + implementation files('libs/CPCL_SDK_V1.12.jar') + +} \ No newline at end of file diff --git a/example/android/app/libs/CPCL_SDK_V1.12.jar b/example/android/app/libs/CPCL_SDK_V1.12.jar new file mode 100644 index 0000000..83c8e5b Binary files /dev/null and b/example/android/app/libs/CPCL_SDK_V1.12.jar differ diff --git a/example/android/app/libs/arm64-v8a/libLZO.so b/example/android/app/libs/arm64-v8a/libLZO.so new file mode 100644 index 0000000..a8b7a72 Binary files /dev/null and b/example/android/app/libs/arm64-v8a/libLZO.so differ diff --git a/example/android/app/libs/armeabi-v7a/libLZO.so b/example/android/app/libs/armeabi-v7a/libLZO.so new file mode 100644 index 0000000..ffd0aeb Binary files /dev/null and b/example/android/app/libs/armeabi-v7a/libLZO.so differ diff --git a/example/android/app/libs/armeabi/libLZO.so b/example/android/app/libs/armeabi/libLZO.so new file mode 100644 index 0000000..53cee5a Binary files /dev/null and b/example/android/app/libs/armeabi/libLZO.so differ diff --git a/example/android/app/libs/mips/libLZO.so b/example/android/app/libs/mips/libLZO.so new file mode 100644 index 0000000..02c3838 Binary files /dev/null and b/example/android/app/libs/mips/libLZO.so differ diff --git a/example/android/app/libs/mips64/libLZO.so b/example/android/app/libs/mips64/libLZO.so new file mode 100644 index 0000000..2896f0b Binary files /dev/null and b/example/android/app/libs/mips64/libLZO.so differ diff --git a/example/android/app/libs/x86/libLZO.so b/example/android/app/libs/x86/libLZO.so new file mode 100644 index 0000000..4f13511 Binary files /dev/null and b/example/android/app/libs/x86/libLZO.so differ diff --git a/example/android/app/libs/x86_64/libLZO.so b/example/android/app/libs/x86_64/libLZO.so new file mode 100644 index 0000000..c8abbd3 Binary files /dev/null and b/example/android/app/libs/x86_64/libLZO.so differ diff --git a/example/android/app/src/debug/AndroidManifest.xml b/example/android/app/src/debug/AndroidManifest.xml index 4622f1a..296a333 100644 --- a/example/android/app/src/debug/AndroidManifest.xml +++ b/example/android/app/src/debug/AndroidManifest.xml @@ -4,4 +4,10 @@ to allow setting breakpoints, to provide hot reload, etc. --> + + + + + diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index d6a0d05..0592249 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -1,5 +1,5 @@ - + @@ -9,6 +9,7 @@ android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" + android:requestLegacyExternalStorage="true" android:windowSoftInputMode="adjustResize"> + + + + + diff --git a/example/lib/main.dart b/example/lib/main.dart index 2340eeb..f74e3ee 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,8 +1,12 @@ import 'dart:async'; +import 'dart:convert'; import 'package:flutter/material.dart'; +import 'package:hy_printer/device.dart'; +import 'package:hy_printer/dj_printer.dart'; import 'package:hy_printer/hy_printer.dart'; import 'package:hy_printer_example/scan_page.dart'; +import 'package:permission_handler/permission_handler.dart'; void main() { runApp(const MyApp()); @@ -16,95 +20,186 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { + List devices = []; @override void initState() { super.initState(); initPlatformState(); } + // Future initPlatformState() async { + // if (!mounted) return; + // setState(() {}); + // } Future initPlatformState() async { - if (!mounted) return; - setState(() {}); - } + 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(); + } - @override - Widget build(BuildContext context) { - return const MaterialApp( - home: Home(), - ); - } -} + DjPrinter().init(); + DjPrinter().addDiscoveryListen(onReceive: (data) { + var js = json.decode(data.toString()); + if ((js['name'] as String).startsWith('HM')) { + devices.add(Device( + name: js['name'], + address: js['address'], + isPaired: js['isPaired'])); + } -class Home extends StatefulWidget { - const Home({Key? key}) : super(key: key); - @override - _HomeState createState() => _HomeState(); -} -class _HomeState extends State { - int status = -1; + setState(() {}); + }, onStart: () { + print("————————————————————————"); + }, onFinish: () { + print('——————————————————————————————'); + DjPrinter().cancelDiscovery(); + }); + DjPrinter().addConnectListen(onConnect: () { + print("connected"); + }, onDisconnect: () { + print('disconnected'); + }); + } + + @override - Widget build(BuildContext context) { - return Scaffold( + Widget build(BuildContext context) {return MaterialApp( + home: Scaffold( appBar: AppBar( title: const Text('Plugin example app'), ), body: Center( - child: Column( - children: [ - TextButton( - onPressed: () { - Navigator.push(context, MaterialPageRoute(builder: (context) { - return const ScanPage(); - })); - }, - child: const Text('扫描设备')), - TextButton( - onPressed: () async { - await HyPrinter.disConnect(); - }, - child: const Text('断开连接')), - TextButton( - onPressed: () async { - status = await HyPrinter.getStatus(); - setState(() {}); - }, - child: Text('$status')), - TextButton( - onPressed: () async { - await HyPrinter.printAsOrder( - code: 'ASASNB2021121200011', - fbaCode: '', - channel: "", - country: '', - count: "2/10", - hasPlan: false); - setState(() {}); - }, - child: const Text('打印面单')), - TextButton( - onPressed: () async { - await HyPrinter.printAsOrder( - code: 'ASASNB2021121200010002', - fbaCode: 'FBA15RY33MN8U00001', - channel: "欧洲特快", - country: '法国', - count: "2/10", - hasPlan: true); - setState(() {}); - }, - child: const Text('打印FBA面单')), - TextButton( - onPressed: () async { - await HyPrinter.printBarCode(0, 0, "2", "1", "130", "45", "26", - true, "8", "14", "12345678"); - setState(() {}); - }, - child: const Text('打印条码')), - ], - )), - ); + child: Column( + children: [ + TextButton( + onPressed: () { + devices.clear(); + setState(() {}); + DjPrinter().startSearch; + }, + child: const Text('扫描设备')), + // TextButton(onPressed: () {}, child: const Text('打印')), + const SizedBox( + height: 20, + ), + ...devices + .map((e) => TextButton( + onPressed: () { + [Permission.bluetoothConnect,].request().then((value) async { + print("===============${value[Permission.bluetoothConnect]}"); + if(value[Permission.bluetoothConnect]!=PermissionStatus.denied){ + await DjPrinter().connect(e.address); + } + }); + // + + }, + child: Column( + children: [ + Text(e.name), + Text(e.address), + ], + ))) + .toList(), + const SizedBox( + height: 20, + ), + TextButton( + onPressed: () { + + // DjPrinter().printNewAScode( + // code: 'ASSZ000000002', + // barCode: 'ASSZ0000000020001', + // channel: '加拿大温哥华海派快线-', + // country: '美国', + // num: '2', + // sum:'9', + // offset: 0, + // hasPlan: true, ); + + DjPrinter().printNewAScode( + code: 'ASSZ000000002', + barCode: 'ASSZ0000000020001', + channel: '加拿大温哥华海派快线-卡派 / UPS派送', + country: '美国啊啊', + num: '2', + sum:'999', + offset: 0, + hasPlan: true, ); + // DjPrinter().printNewAScode( + // code: 'ASSZ000000002', + // barCode: 'ASSZ0000000020001', + // channel: '加拿大温哥华海派快线-卡派 / UPS派送', + // country: '捷克斯洛伐克', + // num: '2', + // sum:'99', + // offset: 0, + // hasPlan: true, ); + }, + child: const Text('打印')), + TextButton( + onPressed: () { + + // DjPrinter().printNewAScode( + // code: 'ASSZ000000002', + // barCode: 'ASSZ0000000020001', + // channel: '加拿大温哥华海派快线-', + // country: '美国', + // num: '2', + // sum:'9', + // offset: 0, + // hasPlan: true, ); + + DjPrinter().printNewAScode( + code: 'ASSZ000000002', + barCode: 'ASSZ0000000020001', + channel: '加拿大温哥华海派快线-卡派 / UPS派送', + country: '美国啊啊', + num: '2', + sum:'999', + offset: 0, + hasPlan: false, ); + // DjPrinter().printNewAScode( + // code: 'ASSZ000000002', + // barCode: 'ASSZ0000000020001', + // channel: '加拿大温哥华海派快线-卡派 / UPS派送', + // country: '捷克斯洛伐克', + // num: '2', + // sum:'99', + // offset: 0, + // hasPlan: true, ); + }, + child: const Text('noplan打印')), + const SizedBox( + height: 20, + ), + TextButton( + onPressed: () { + DjPrinter().disposeConnect(); + }, + child: const Text('取消链接')), + + ], + ), + ), + ), + ); } } + + diff --git a/example/lib/scan_page.dart b/example/lib/scan_page.dart index 8883961..086ff23 100644 --- a/example/lib/scan_page.dart +++ b/example/lib/scan_page.dart @@ -1,51 +1,52 @@ -import 'package:flutter/material.dart'; -import 'package:hy_printer/device.dart'; -import 'package:hy_printer/hy_printer.dart'; - -class ScanPage extends StatefulWidget { - const ScanPage({Key? key}) : super(key: key); - - @override - _ScanPageState createState() => _ScanPageState(); -} - -class _ScanPageState extends State { - List _devices = []; - - @override - void initState() { - init(); - super.initState(); - } - - Future init() async { - _devices = await HyPrinter.getDeiveces(); - setState(() {}); - } - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: const Text('设备列表'), - ), - body: ListView( - padding: const EdgeInsets.all(10), - children: _devices - .map((e) => TextButton( - onPressed: () async { - var result = await HyPrinter.connect(e.address); - if (result == 0) { - Navigator.pop(context); - } - }, - child: Column( - children: [ - Text(e.name), - Text(e.address), - ], - ))) - .toList()), - ); - } -} +// import 'package:flutter/material.dart'; +// import 'package:hy_printer/device.dart'; +// import 'package:hy_printer/hy_printer.dart'; +// +// class ScanPage extends StatefulWidget { +// const ScanPage({Key? key}) : super(key: key); +// +// @override +// _ScanPageState createState() => _ScanPageState(); +// } +// +// class _ScanPageState extends State { +// List _devices = []; +// +// @override +// void initState() { +// HyPrinter.init(); +// init(); +// super.initState(); +// } +// +// Future init() async { +// _devices = await HyPrinter.getDeiveces(); +// setState(() {}); +// } +// +// @override +// Widget build(BuildContext context) { +// return Scaffold( +// appBar: AppBar( +// title: const Text('设备列表'), +// ), +// body: ListView( +// padding: const EdgeInsets.all(10), +// children: _devices +// .map((e) => TextButton( +// onPressed: () async { +// var result = await HyPrinter.connect('04:7F:0E:95:FD:A1'); +// if (result == 0) { +// Navigator.pop(context); +// } +// }, +// child: Column( +// children: [ +// Text(e.name), +// Text(e.address), +// ], +// ))) +// .toList()), +// ); +// } +// } diff --git a/example/pubspec.lock b/example/pubspec.lock index e9bf57f..e4ca30f 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -7,7 +7,7 @@ packages: name: async url: "https://pub.flutter-io.cn" source: hosted - version: "2.7.0" + version: "2.8.2" boolean_selector: dependency: transitive description: @@ -21,7 +21,7 @@ packages: name: characters url: "https://pub.flutter-io.cn" source: hosted - version: "1.1.0" + version: "1.2.0" charcode: dependency: transitive description: @@ -42,47 +42,47 @@ packages: name: collection url: "https://pub.flutter-io.cn" source: hosted - version: "1.15.0" + version: "1.16.0" convert: dependency: transitive description: name: convert url: "https://pub.flutter-io.cn" source: hosted - version: "3.0.1" + version: "3.0.2" cupertino_icons: dependency: "direct main" description: name: cupertino_icons url: "https://pub.flutter-io.cn" source: hosted - version: "1.0.3" + version: "1.0.5" fake_async: dependency: transitive description: name: fake_async url: "https://pub.flutter-io.cn" source: hosted - version: "1.2.0" + version: "1.3.0" fixnum: dependency: transitive description: name: fixnum url: "https://pub.flutter-io.cn" source: hosted - version: "1.0.0" + version: "1.0.1" flutter: dependency: "direct main" description: flutter source: sdk version: "0.0.0" - flutter_blue: + flutter_blue_plus: dependency: transitive description: - name: flutter_blue + name: flutter_blue_plus url: "https://pub.flutter-io.cn" source: hosted - version: "0.8.0" + version: "1.3.1" flutter_lints: dependency: "direct dev" description: @@ -115,35 +115,84 @@ packages: name: matcher url: "https://pub.flutter-io.cn" source: hosted - version: "0.12.10" + version: "0.12.11" + material_color_utilities: + dependency: transitive + description: + name: material_color_utilities + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.1.4" meta: dependency: transitive description: name: meta url: "https://pub.flutter-io.cn" source: hosted - version: "1.4.0" + version: "1.7.0" path: dependency: transitive description: name: path url: "https://pub.flutter-io.cn" source: hosted - version: "1.8.0" + version: "1.8.1" + permission_handler: + dependency: "direct main" + description: + name: permission_handler + url: "https://pub.flutter-io.cn" + source: hosted + version: "10.0.0" + permission_handler_android: + dependency: transitive + description: + name: permission_handler_android + url: "https://pub.flutter-io.cn" + source: hosted + version: "10.0.0" + permission_handler_apple: + dependency: transitive + description: + name: permission_handler_apple + url: "https://pub.flutter-io.cn" + source: hosted + version: "9.0.4" + permission_handler_platform_interface: + dependency: transitive + description: + name: permission_handler_platform_interface + url: "https://pub.flutter-io.cn" + source: hosted + version: "3.7.0" + permission_handler_windows: + dependency: transitive + description: + name: permission_handler_windows + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.1.0" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.1.2" protobuf: dependency: transitive description: name: protobuf url: "https://pub.flutter-io.cn" source: hosted - version: "2.0.0" + version: "2.1.0" rxdart: dependency: transitive description: name: rxdart url: "https://pub.flutter-io.cn" source: hosted - version: "0.26.0" + version: "0.27.5" sky_engine: dependency: transitive description: flutter @@ -155,7 +204,7 @@ packages: name: source_span url: "https://pub.flutter-io.cn" source: hosted - version: "1.8.1" + version: "1.8.2" stack_trace: dependency: transitive description: @@ -190,21 +239,21 @@ packages: name: test_api url: "https://pub.flutter-io.cn" source: hosted - version: "0.4.1" + version: "0.4.9" typed_data: dependency: transitive description: name: typed_data url: "https://pub.flutter-io.cn" source: hosted - version: "1.3.0" + version: "1.3.1" vector_math: dependency: transitive description: name: vector_math url: "https://pub.flutter-io.cn" source: hosted - version: "2.1.0" + version: "2.1.2" sdks: - dart: ">=2.12.0 <3.0.0" - flutter: ">=1.20.0" + dart: ">=2.17.0-0 <3.0.0" + flutter: ">=2.8.0" diff --git a/example/pubspec.yaml b/example/pubspec.yaml index cc5f02a..d0b091f 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -17,7 +17,7 @@ environment: dependencies: flutter: sdk: flutter - + permission_handler: 10.0.0 hy_printer: # When depending on this package from a real application you should use: # hy_printer: ^x.y.z diff --git a/hy_printer.iml b/hy_printer.iml index a966544..e5eda02 100644 --- a/hy_printer.iml +++ b/hy_printer.iml @@ -12,6 +12,7 @@ + diff --git a/lib/device.dart b/lib/device.dart index 5f34321..545e705 100644 --- a/lib/device.dart +++ b/lib/device.dart @@ -1,9 +1,11 @@ class Device { - final String name; - final String address; + String name; + String address; + bool isPaired; - const Device({ + Device({ required this.name, required this.address, + required this.isPaired, }); } diff --git a/lib/dj_printer.dart b/lib/dj_printer.dart new file mode 100644 index 0000000..465eec1 --- /dev/null +++ b/lib/dj_printer.dart @@ -0,0 +1,199 @@ +import 'dart:async'; +import 'dart:convert'; +import 'dart:typed_data'; +import 'dart:ui'; + + +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:hy_printer/status_enum.dart'; + +class DjPrinter { + static late final DjPrinter _instance = DjPrinter._(); + + DjPrinter._(); + + factory DjPrinter() => _instance; + static const MethodChannel _channel = MethodChannel('hy_printer'); + static const EventChannel _deviceChannel = + EventChannel("com.discovery.devices"); + StreamSubscription? _discoveryStream; + + StreamSubscription addDiscoveryListen( + {required void Function(dynamic data) onReceive, + void Function()? onStart, + void Function()? onFinish}) { + if (_discoveryStream == null) { + return _deviceChannel.receiveBroadcastStream().listen((data) { + if (data == "start" && onStart != null) { + onStart(); + } else if (data == "finish" && onFinish != null) { + onFinish(); + } else { + onReceive(data); + } + }); + } else { + return _discoveryStream!; + } + } + + void cancelDiscovery() { + _discoveryStream?.cancel(); + _discoveryStream = null; + print('结束搜索'); + disposeDiscovery(); + } + + static const EventChannel _connectChannel = EventChannel("com.connect"); + StreamSubscription? _connectStream; + + StreamSubscription addConnectListen({required void Function() onConnect, + required void Function() onDisconnect}) { + if (_connectStream == null) { + return _connectChannel.receiveBroadcastStream().listen((data) { + if (data == 'connected') { + onConnect(); + } else if (data == 'disconnected') { + onDisconnect(); + } + }); + } else { + return _connectStream!; + } + } + + void cancelConnect() { + if (_connectStream != null) { + _connectStream!.cancel(); + _connectStream = null; + } + } + + void get startSearch { + final res = _channel.invokeMethod('startSearch'); + } + + bool _hasInit = false; + + bool get hasInit => _hasInit; + + 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); + }); + return true; + } + + void disposeDiscovery() { + print('disposeDiscovery'); + final res = _channel.invokeMethod('disposeDiscovery'); + } + + Future disposeConnect() async { + final res = await _channel.invokeMethod('disposeConnect'); + return res; + } + + Future init() async { + final res = await _channel.invokeMethod('init'); + _hasInit = true; + return res; + } + + //0 normal + //1 busy + //2 paper empty + //4 cover open + //8 battery low + // Future getStatus() async { + // final res = await _channel.invokeMethod('getStatus'); + // switch (res) { + // case 0: + // return PRINT_STATUS.normal; + // case 1: + // return PRINT_STATUS.busy; + // case 2: + // return PRINT_STATUS.paperEmpty; + // case 4: + // return PRINT_STATUS.coverOpen; + // case 8: + // return PRINT_STATUS.batteryLow; + // default: + // return null; + // } + // } + + + + //0 normal + //-1 发送失败 + //2 paper empty + //6 cover open + + Future getStatus() async { + final res = await _channel.invokeMethod('getStatus'); + switch (res) { + case 0: + return PRINT_STATUS.normal; + case -1: + return PRINT_STATUS.fail; + case 2: + return PRINT_STATUS.paperEmpty; + case 6: + return PRINT_STATUS.coverOpen; + default: + return null; + } + } + + Future printNewAScode({required String code, + required String barCode, + required String channel, + required String country, + required String num, + required String sum, + required int offset, + required bool hasPlan}) async { + final res = await _channel.invokeMethod('newPrint', { + 'code': code, + 'barCode':barCode, + 'channel': channel, + 'country': country, + 'num': num, + 'sum': sum, + 'offset': offset, + 'hasPlan': hasPlan, + }); + return res; + } + + + + + Future printAScode({required String code, + required String channel, + required String country, + required String countStr, + required int offset, + required bool hasPlan}) async { + final res = await _channel.invokeMethod('print', { + 'code': code, + 'channel': channel, + 'country': country, + 'countStr': countStr, + 'offset': offset, + 'hasPlan': hasPlan, + }); + return res; + } + + + + + + } + diff --git a/lib/hy_printer.dart b/lib/hy_printer.dart index 607db8b..8b2277a 100644 --- a/lib/hy_printer.dart +++ b/lib/hy_printer.dart @@ -1,141 +1,155 @@ -import 'dart:async'; - -import 'package:flutter/services.dart'; -import 'package:flutter_blue/flutter_blue.dart'; -import 'package:hy_printer/device.dart'; - -class HyPrinter { - static const MethodChannel _channel = MethodChannel('hy_printer'); - static FlutterBlue flutterBlue = FlutterBlue.instance; - - static void init() {} - - static Future> getDeiveces() async { - var devices = []; - await flutterBlue.startScan(timeout: const Duration(seconds: 4)); - flutterBlue.scanResults.listen((results) { - for (ScanResult r in results) { - //过滤名称为空的蓝牙 - if (r.device.name.isNotEmpty) { - devices.add(Device(name: r.device.name, address: r.device.id.id)); - } - } - }); - await flutterBlue.stopScan(); - return devices; - } - - static Future blutIsOn() async { - var result = await flutterBlue.isOn; - return result; - } - - ///0:连接成功 - ///-1:连接超时 - ///-2:地址格式错误 - ///-3:打印机与sdk不匹配 - ///-4:连接失败 - static Future connect(String address) async { - int result = -4; - await Future.delayed(const Duration(milliseconds: 2000), () async { - result = await _channel.invokeMethod('connect', {'address': address}); - }); - // int result = await _channel.invokeMethod('connect', {'address': address}); - return result; - } - - static Future disConnect() async { - bool result = await _channel.invokeMethod('disConnect'); - return result; - } - - ///0:打印机正常 - ///-1:发送失败 - ///2:缺纸 - ///3:开盖 - static Future getStatus() async { - int result = await _channel.invokeMethod( - 'getStatus', - ); - return result; - } - - static Future printBarCode( - int direction, - int type, - String width, - String ratio, - String height, - String x, - String y, - bool undertext, - String size, - String offset, - String data, - ) async { - int result = await _channel.invokeMethod('printBarCode', { - 'direction': direction, - 'type': type, - 'width': width, - 'ratio': ratio, - 'height': height, - 'x': x, - 'y': y, - 'undertext': undertext, - 'size': size, - 'offset': offset, - 'data': data, - }); - return result; - } - - static Future printLine( - String x0, - String y0, - String x1, - String y1, - ) async { - int result = await _channel - .invokeMethod('printLine', {'x0': x0, 'x1': x1, 'y0': y0, 'y1': y1}); - return result; - } - - static Future align(String align) async { - int result = await _channel.invokeMethod('align', {'align': align}); - return result; - } - - static Future setBold(String bold) async { - int result = await _channel.invokeMethod('setBold', {'bold': bold}); - return result; - } - - static Future prefeed(String prefeed) async { - int result = await _channel.invokeMethod('setBold', {'prefeed': prefeed}); - return result; - } - - /// 'code': code, 箱号 - // 'fbaCode': fbaCode, fba号/非fba传空字符串 - // 'channel': channel,渠道名称 - // 'country': country,目的国 - // 'count': count,货件数量 - // 'hasPlan':hasPlan 有无计划 - static Future printAsOrder( - {required String code, - required String fbaCode, - required String channel, - required String country, - required String count, - required bool hasPlan}) async { - int result = await _channel.invokeMethod('printAsOrder', { - 'code': code, - 'fbaCode': fbaCode, - 'channel': channel, - 'country': country, - 'count': count, - 'hasPlan': hasPlan - }); - return result; - } -} +// import 'dart:async'; +// +// import 'package:flutter/services.dart'; +// // import 'package:flutter_blue/flutter_blue.dart'; +// import 'package:flutter_blue_plus/flutter_blue_plus.dart'; +// import 'package:hy_printer/device.dart'; +// import 'package:permission_handler/permission_handler.dart'; +// +// class HyPrinter { +// static const MethodChannel _channel = MethodChannel('hy_printer'); +// static FlutterBluePlus flutterBlue = FlutterBluePlus.instance; +// static void init() async { +// var per = await Permission.bluetooth.isGranted; +// if (!per) { +// Permission.bluetooth.request(); +// } +// var pers = await Permission.locationWhenInUse.isGranted; +// if (!pers) { +// Permission.locationWhenInUse.request(); +// } +// } +// +// static Future> getDeiveces() async { +// var devices = []; +// await flutterBlue.startScan(timeout: const Duration(seconds: 4)); +// flutterBlue.scanResults.listen((results) { +// for (ScanResult r in results) { +// //过滤名称为空的蓝牙 +// if(r.device.name.contains('HM')){ +// devices.add(Device(name: r.device.name, address: r.device.id.id)); +// } +// +// // if (r.device.name.isNotEmpty) { +// // devices.add(Device(name: r.device.name, address: r.device.id.id)); +// // } +// } +// }); +// await flutterBlue.stopScan(); +// return devices; +// } +// +// static Future blutIsOn() async { +// var result = await flutterBlue.isOn; +// return result; +// } +// +// ///0:连接成功 +// ///-1:连接超时 +// ///-2:地址格式错误 +// ///-3:打印机与sdk不匹配 +// ///-4:连接失败 +// static Future connect(String address) async { +// int result = -4; +// await Future.delayed(const Duration(milliseconds: 2000), () async { +// result = await _channel.invokeMethod('connect', {'address': address}); +// }); +// // int result = await _channel.invokeMethod('connect', {'address': address}); +// return result; +// } +// +// static Future disConnect() async { +// bool result = await _channel.invokeMethod('disConnect'); +// return result; +// } +// +// ///0:打印机正常 +// ///-1:发送失败 +// ///2:缺纸 +// ///3:开盖 +// static Future getStatus() async { +// int result = await _channel.invokeMethod( +// 'getStatus', +// ); +// return result; +// } +// +// static Future printBarCode( +// int direction, +// int type, +// String width, +// String ratio, +// String height, +// String x, +// String y, +// bool undertext, +// String size, +// String offset, +// String data, +// ) async { +// int result = await _channel.invokeMethod('printBarCode', { +// 'direction': direction, +// 'type': type, +// 'width': width, +// 'ratio': ratio, +// 'height': height, +// 'x': x, +// 'y': y, +// 'undertext': undertext, +// 'size': size, +// 'offset': offset, +// 'data': data, +// }); +// return result; +// } +// +// static Future printLine( +// String x0, +// String y0, +// String x1, +// String y1, +// ) async { +// int result = await _channel +// .invokeMethod('printLine', {'x0': x0, 'x1': x1, 'y0': y0, 'y1': y1}); +// return result; +// } +// +// static Future align(String align) async { +// int result = await _channel.invokeMethod('align', {'align': align}); +// return result; +// } +// +// static Future setBold(String bold) async { +// int result = await _channel.invokeMethod('setBold', {'bold': bold}); +// return result; +// } +// +// static Future prefeed(String prefeed) async { +// int result = await _channel.invokeMethod('setBold', {'prefeed': prefeed}); +// return result; +// } +// +// /// 'code': code, 箱号 +// // 'fbaCode': fbaCode, fba号/非fba传空字符串 +// // 'channel': channel,渠道名称 +// // 'country': country,目的国 +// // 'count': count,货件数量 +// // 'hasPlan':hasPlan 有无计划 +// static Future printAsOrder( +// {required String code, +// required String fbaCode, +// required String channel, +// required String country, +// required String count, +// required bool hasPlan}) async { +// int result = await _channel.invokeMethod('printAsOrder', { +// 'code': code, +// 'fbaCode': fbaCode, +// 'channel': channel, +// 'country': country, +// 'count': count, +// 'hasPlan': hasPlan +// }); +// return result; +// } +// } diff --git a/lib/status_enum.dart b/lib/status_enum.dart new file mode 100644 index 0000000..4373b41 --- /dev/null +++ b/lib/status_enum.dart @@ -0,0 +1,13 @@ +// enum PRINT_STATUS { +// normal, +// busy, +// paperEmpty, +// coverOpen, +// batteryLow, +// } +enum PRINT_STATUS { + normal, + fail, + paperEmpty, + coverOpen, +} diff --git a/pubspec.lock b/pubspec.lock index ed36223..d295394 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -7,7 +7,7 @@ packages: name: async url: "https://pub.flutter-io.cn" source: hosted - version: "2.7.0" + version: "2.8.2" boolean_selector: dependency: transitive description: @@ -21,7 +21,7 @@ packages: name: characters url: "https://pub.flutter-io.cn" source: hosted - version: "1.1.0" + version: "1.2.0" charcode: dependency: transitive description: @@ -42,40 +42,40 @@ packages: name: collection url: "https://pub.flutter-io.cn" source: hosted - version: "1.15.0" + version: "1.16.0" convert: dependency: transitive description: name: convert url: "https://pub.flutter-io.cn" source: hosted - version: "3.0.1" + version: "3.0.2" fake_async: dependency: transitive description: name: fake_async url: "https://pub.flutter-io.cn" source: hosted - version: "1.2.0" + version: "1.3.0" fixnum: dependency: transitive description: name: fixnum url: "https://pub.flutter-io.cn" source: hosted - version: "1.0.0" + version: "1.0.1" flutter: dependency: "direct main" description: flutter source: sdk version: "0.0.0" - flutter_blue: + flutter_blue_plus: dependency: "direct main" description: - name: flutter_blue + name: flutter_blue_plus url: "https://pub.flutter-io.cn" source: hosted - version: "0.8.0" + version: "1.3.1" flutter_lints: dependency: "direct dev" description: @@ -101,35 +101,84 @@ packages: name: matcher url: "https://pub.flutter-io.cn" source: hosted - version: "0.12.10" + version: "0.12.11" + material_color_utilities: + dependency: transitive + description: + name: material_color_utilities + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.1.4" meta: dependency: transitive description: name: meta url: "https://pub.flutter-io.cn" source: hosted - version: "1.4.0" + version: "1.7.0" path: dependency: transitive description: name: path url: "https://pub.flutter-io.cn" source: hosted - version: "1.8.0" + version: "1.8.1" + permission_handler: + dependency: "direct main" + description: + name: permission_handler + url: "https://pub.flutter-io.cn" + source: hosted + version: "10.0.0" + permission_handler_android: + dependency: transitive + description: + name: permission_handler_android + url: "https://pub.flutter-io.cn" + source: hosted + version: "10.0.0" + permission_handler_apple: + dependency: transitive + description: + name: permission_handler_apple + url: "https://pub.flutter-io.cn" + source: hosted + version: "9.0.4" + permission_handler_platform_interface: + dependency: transitive + description: + name: permission_handler_platform_interface + url: "https://pub.flutter-io.cn" + source: hosted + version: "3.7.0" + permission_handler_windows: + dependency: transitive + description: + name: permission_handler_windows + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.1.0" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.1.2" protobuf: dependency: transitive description: name: protobuf url: "https://pub.flutter-io.cn" source: hosted - version: "2.0.0" + version: "2.1.0" rxdart: dependency: transitive description: name: rxdart url: "https://pub.flutter-io.cn" source: hosted - version: "0.26.0" + version: "0.27.5" sky_engine: dependency: transitive description: flutter @@ -141,7 +190,7 @@ packages: name: source_span url: "https://pub.flutter-io.cn" source: hosted - version: "1.8.1" + version: "1.8.2" stack_trace: dependency: transitive description: @@ -176,21 +225,21 @@ packages: name: test_api url: "https://pub.flutter-io.cn" source: hosted - version: "0.4.1" + version: "0.4.9" typed_data: dependency: transitive description: name: typed_data url: "https://pub.flutter-io.cn" source: hosted - version: "1.3.0" + version: "1.3.1" vector_math: dependency: transitive description: name: vector_math url: "https://pub.flutter-io.cn" source: hosted - version: "2.1.0" + version: "2.1.2" sdks: - dart: ">=2.12.0 <3.0.0" - flutter: ">=1.20.0" + dart: ">=2.17.0-0 <3.0.0" + flutter: ">=2.8.0" diff --git a/pubspec.yaml b/pubspec.yaml index 530bee8..c094594 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,8 +10,8 @@ environment: dependencies: flutter: sdk: flutter - flutter_blue: ^0.8.0 - + flutter_blue_plus: ^1.3.1 + permission_handler: 10.0.0 dev_dependencies: flutter_test: sdk: flutter