我正在开发一个 android 应用程序来使用 Brother 标签打印机打印标签。我正在为 Brother 打印机使用 Flutter 和 Native Android SDK,使用 Platform MethodChannel 桥接 Flutter 和 java 之间的代码。
我已按照 Brother SDK 教程页面中的详细说明设置打印机细节,如下所示:
if (call.method.equals("print")) {
String address = call.argument("address");
String base64 = call.argument("bas64");
// Specify printer
printer = new Printer();
settings = new PrinterInfo();
settings = printer.getPrinterInfo();
settings.printerModel = PrinterInfo.Model.QL_810W;
settings.port = PrinterInfo.Port.NET;
settings.ipAddress = address;
// Print Settings
settings.paperSize = PrinterInfo.PaperSize.CUSTOM;
settings.orientation = PrinterInfo.Orientation.LANDSCAPE;
settings.valign = PrinterInfo.VAlign.MIDDLE;
settings.align = PrinterInfo.Align.CENTER;
settings.printMode = PrinterInfo.PrintMode.FIT_TO_PAGE;
settings.numberOfCopies = 1;
settings.labelNameIndex = LabelInfo.QL700.W52H29.ordinal();
//settings.labelNameIndex = LabelInfo.QL700.W62.ordinal();
settings.isAutoCut = true;
settings.isCutAtEnd = false;
settings.isHalfCut = false;
settings.isSpecialTape = false;
printer.setPrinterInfo(settings);
bit = new PrinterStatus();
if (printer.startCommunication()) {
bit = printer.printImage(bmpFromBase64(base64));
if (bit.errorCode != PrinterInfo.ErrorCode.ERROR_NONE) {
Log.d("TAG", "ERROR - " + bit.errorCode);
result.error("ERROR",bit.errorCode.toString(),"Error when Printing");
}
printer.endCommunication();
result.success(0);
}
}else{
result.notImplemented();
}
这是应用程序颤振端的代码
Future<void> printLabel(
BuildContext context, String _ip, String _bs64) async {
String printStatus = '';
printText = '$printText\nPrinter IP: $_ip';
try {
final print = await platform
.invokeMethod('print', {'address': _ip, 'bas64': _bs64});
printStatus = '$print';
} on PlatformException catch (e) {
printStatus = "\nERROR WHEN PRINTING: \n'$e'";
}
setState(() {
printText = '$printText\n$printStatus';
});
}
当我尝试测试代码时,我在 MainActivity.java 文件的控制台中收到以下错误。我曾尝试在线搜索,但我不确定问题所在,因为我似乎在做网上提到的正确事情。
'PlatformException(error, null, null, android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1605)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:389)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
at java.net.Socket.connect(Socket.java:621)
at java.net.Socket.connect(Socket.java:570)
at java.net.Socket.<init>(Socket.java:450)
at java.net.Socket.<init>(Socket.java:250)
at com.brother.ptouch.sdk.connection.WifiConnection.startConnection(WifiConnection.java:182)
at com.brother.ptouch.sdk.connection.WifiConnection.open(WifiConnection.java:47)
at com.brother.ptouch.sdk.Printer.init(Printer.java:2065)
at com.brother.ptouch.sdk.Printer.startCommunication(Printer.java:2200)
at com.abi_selvaraj.fs_date_labels.MainActivity.lambda$configureFlutterEngine$0$MainActivity(MainActivity.java:97)
at com.abi_selvaraj.fs_date_labels.-$$Lambda$MainActivity$g9plek86tvIADNk2OgkRVXdFblc.onMethodCall(Unknown Source:2)
at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:84)
at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:819)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:335)
at android.os.Looper.loop(Looper.java:183)
at android.app.ActivityThread.main(ActivityThread.java:8010)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
)'
请帮助我找到解决此问题的方法,因为问题似乎与与打印机的通信有关。我尝试了多个代码,但最终处于相同的情况。