I am developing an android app to print labels using a Brother Label printer. I am using Flutter and the Native Android SDK for Brother printers, bridging the code between flutter and java using Platform MethodChannel.
I have set the printer specifics as detailed in the tutorial page for the Brother SDK as follows:
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();
}
And this is the code in the flutter side of the app
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';
});
}
When I try testing the code, I get the following error in the console of the MainActivity.java file. I have tried searching online, but I am unsure of the problem as I seem to be doing the right things as mentioned online.
'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)
)'
Kindly help me find a solution to this problem, as the issue seems to be with communicating with the printer. I have tried multiple codes but end up at the same situation.