0

I've been trying to print with Brother Print SDK 3.5.1 on Android 8.1.0. I keep getting ERROR_WRONG_LABEL.

This is the code I use

    void printPdf() {

        // Specify printer
        final Printer printer = new Printer();
        PrinterInfo settings = printer.getPrinterInfo();
        settings.printerModel = PrinterInfo.Model.QL_810W;
        settings.port = PrinterInfo.Port.NET;
        settings.ipAddress = "192.168.1.73";
        settings.workPath = "storage/emulated/0/Download/";

        // Print Settings
        settings.labelNameIndex = LabelInfo.QL700.W62RB.ordinal();
        settings.printMode = PrinterInfo.PrintMode.FIT_TO_PAGE;
        settings.orientation = PrinterInfo.Orientation.PORTRAIT;
        settings.isAutoCut = true;
        printer.setPrinterInfo(settings);

        // Connect, then print
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (printer.startCommunication()) {
                    PrinterStatus result = printer.printPdfFile("/storage/emulated/0/Download/hello world red.pdf", 1);
                    if (result.errorCode != PrinterInfo.ErrorCode.ERROR_NONE) {
                        Log.d("TAG", "ERROR - " + result.errorCode);
                    }
                    printer.endCommunication();
                }
            }
        }).start();
    }

My printer model is QL-810W and I use the black and red W62 roll.

  • I've tried the Sample Application, where setting W62RB in labelNameIndex prints fine.
  • Changing the roll for different one with different width didn't help.
  • I've also tried iterating over numbers 0 to 50 and using them as labelNameIndex.
  • Based on this thread, I thought that the issue may be in specifying the workPath attribute. Setting workPath to getContext().getCacheDir().getPath() results in ERROR_OUT_OF_MEMORY instead of ERROR_WRONG_LABEL. Not specifying workPath and adding <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> to AndroidManifest.xml results in ERROR_WRONG_LABEL

EDIT

I've modified the Brother Sample app and uploaded it to GitHub. The app now launches Activity_PrintPdf.java by default where I inserted my printing code with hardcoded values at the beginning of onCreate method - this works fine and prints the PDF file as expected.

Then I created a new Empty Activity project in Android Studio, copy pasted the library, added the imports to build.gradle and copy pasted the permissions into AndroidManifest.xml. Then I copy pasted the printing code at the beginning of onCreate method in MainActivity.java. Running the app results in ERROR_WRONG_LABEL.

This is the modified working example app and this is the one that results in the error. I want to use the code as native module that I call from my React Native app, so it's important that I manage to set up the printing code from scratch rather than modifying the existing example app.

EDIT 2

I've inspected the library with a debugger: when executing printer.setPrinterInfo(mPrinterInfo) the library internally calls private boolean createWorkPath(String dirPath) of Printer object. On return from this method, the debugger shows Source code doesn't match the bytecode and seems to forget the created directory. This also internally sets mResult.errorCode = ErrorCode.ERROR_WORKPATH_NOT_SET. However, instead of rising any error here the code just silently proceeds, which later results in ERROR_WRONG_LABEL when trying to print. Running the same code snipper in the modified Sample app works fine.

I'd be grateful if you could help or suggest what to try next.

Thank you!

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
Dan
  • 31
  • 3

1 Answers1

0

I've now fixed the issue, which was that the Brother library silently failed to create a temporarily folder and instead of reporting an error, it continued and failed later to read the label information. Based on this thread, it is now required to specify runtime file read and write permissions as opposed to the compile-time ones in AndroidManifest.xml. Adding

ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);

at the beginning of onCreate before the printing code fixed the issue.

Dan
  • 31
  • 3
  • Hi Dan, I'm having the same problem with Brother SDK that I'm using in my react native mobile app. In iOS all works correctly. In Android I can print but I receive always the error ERROR_WORKPATH_NOT_SET. It's very strange because I set the workPath and the printer print what I send. Do you have some advice? I'm using the example took from https://support.brother.com/g/s/es/htmldoc/mobilesdk/guide/getting-started/getting-started-android.html. Thanks – Christian Cascone Apr 24 '21 at 07:26