0

I'm using the Brother Print SDK for Android. My code is based on the example code as shown in the manual:

void printTemplate(int templateKey, String newText) {
// Specify Printer
final Printer printer = new Printer();
PrinterInfo settings = printer.getPrinterInfo();
settings.printerModel = Model.QL_1110NWB;
settings.ipAddress = "your-printer-ip";

// Connect, then print
new Thread(new Runnable() {
    @Override
    public void run() {
        if (printer.startCommunication()) {
            // Specify the template key and the printer encode
            if (startPTTPrint(templateKey, null)) {
                // Replace text object with new text
                replaceText(newText);

                // Start print
                PrinterStatus result = printer.flushPTTPrint();
                if (result.errorCode != ErrorCode.ERROR_NONE) {
                    Log.d("TAG", "ERROR - " + result.errorCode);
                }
            }
            printer.endCommunication();
        }
    }
}).start();

}

When the printer has its cover open, the flushPTTPrint() function does immediately return with a status of ERROR_COVER_OPEN. That is great.

When the printer is out of paper, the flushPTTPrint() function only returns after about three minutes with a status of ERROR_COMMUNICATION_ERROR. Not so great.

QUESTION: how can I detect when the printer is out of paper? Any method would be fine, either be getting flushPTTPrint() to return immediately with a out of paper staus or by querying the printer actively beforehand.

EDIT (in response to Matt Clark's suggestion)

One can set a handler to process status messages from the printer. On a regular printout (and also on the last printout before the paper is empty) the following messages arrive in this order:

  • MESSAGE_START_COMMUNICATION
  • MESSAGE_START_CONNECT
  • MESSAGE_END_CONNECTED
  • MESSAGE_START_SEND_STATUS_REQUEST
  • MESSAGE_END_READ_PRINTER_STATUS
  • MESSAGE_START_SEND_DATA
  • MESSAGE_END_SEND_DATA

When the last paper was used, the printer immediately turns a red led on and shows on its display the out of paper notice. When trying to print in this situation, the following messages arrive:

  • MESSAGE_START_COMMUNICATION
  • MESSAGE_START_CONNECT
  • MESSAGE_END_CONNECTED
  • MESSAGE_START_SEND_STATUS_REQUEST

...about three minutes later...

  • MESSAGE_START_SOCKET_CLOSE
  • MESSAGE_END_SOCKET_CLOSE

The out of paper message (MESSAGE_PAPER_EMPTY) is never seen.

EDIT 2

I just figured out that this problem only happens when connecting via Bluetooth. When using WiFi the above mentioned function immediately returns with an error code ERROR_PAPER_EMPTY.

user1195883
  • 654
  • 4
  • 19

2 Answers2

1

Perhaps try calling printer.getPrinterStatus() before attempting to print?

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • Thanks! Unfortunately that function is also only returning after about three minutes with an error code of ERROR_COMMUNICATION_ERROR when out of paper. – user1195883 Sep 01 '21 at 14:40
  • I am facing a similar issue: https://stackoverflow.com/questions/71625102/how-to-pass-bluetoothdevice-object-to-bluetoothadapter – Chaudhry Talha Mar 27 '22 at 08:14
1

Reading the documentation, it appears as if the printer is capable of sending asynchronous messages back to your application. This might be useful to catch a variety of things, including determining when the printing is actually complete.

From the documentation:

Section 4.1.2.3 shows a method which you can use to register a callback for these messages received:

void setMessageHandle(Handler handler, int MsgType)

Section 4.2.2.13 has a list of all the message types available, one of them being:

MESSAGE_PAPER_EMPTY

I imagine you would get this message as soon as the printer detects the out of paper state.

Matt Clark
  • 27,671
  • 19
  • 68
  • 123
  • This is a great answer, thank you very much for pointing me to that part of the documentation. I've tried it but unfortunately it does not seem to detect the out of paper situation; please see my edit of the original question. – user1195883 Sep 01 '21 at 14:28