How to know when Bluetooth data transfer completes to a linkOS Printer

// Expert user has replied.
L Laks Chanamolu 3 years 2 months ago
3 5 0

We do "Store and Recall print" to ZQ320 printers from Android tablets over Bluetooth. Looking at http://link-os.github.io/android/v2.9.2275/documentation/com/zebra/sdk/…, there is a comment stating we have to wait to make sure the data got to printer before closing connection.

// Make sure the data got to the printer before closing the connection
Thread.sleep(500);

Is there a better way to know when printer received all data? Waiting doesn't give confidence that the printer received data. We send files of different sizes, so waiting certain period of time for all files is not ideal.

Please Register or Login to post a reply

5 Replies

S Steven Si

The time used for the delay in the example code does appear arbitrary. But the essence is to ensure all the data are sent out and then received by the printer, before we close the connection. This is because that the Android OS doesn't tell the application whether the data has been sent and received completely.

A better way is to check the status of the printer (i.e. getCurrentStatus()) to make sure the following conditions are met before closing the connection.

boolean isConnectionClosed = false;
while (!isConnectionClosed) {
    PrinterStatus status = printer.getCurrentStatus();
    if (!status.isReceiveBufferFull && !status.isPartialFormatInProgress) {
        isConnectionClosed = true;
        printer.getConnection().close();
    } else {
        Thread.sleep(100); // Pause for 100ms
    }
}

 

L Laks Chanamolu

Thanks @SSi1. Let me try that and get back to you.

L Laks Chanamolu

Hi @SSi1, we tried the solution, when the Bluetooth Data Transfer is in progress, printer.getCurrentStatus() is throwing Malformed status exception. So we are trying something like this and it seems to work. Please confirm checking for status multiple times during Bluetooth Data transfer does not impede the actual data transfer.

boolean fileTransferComplete = false;
int counter = 0;
try {
    while (!fileTransferComplete) {

        try {
            status = zebraPrinter.getCurrentStatus();
        } catch (ConnectionException e) {
            counter += 1;
            Thread.sleep(100); // Pause for 100ms..
            if (counter > MAXIMUM_WAIT_COUNTER) {
                return false;
            }
            continue;
        }

        if (!status.isReceiveBufferFull && !status.isPartialFormatInProgress) {
            fileTransferComplete = true;
        } else {
            counter += 1;

            if (counter > MAXIMUM_WAIT_COUNTER) {
                return false;
            }
            Thread.sleep(100); // Pause for 100ms
        }

Also it would be nice to have the samples updated with this.

S Steven Si

Hi Laks, It's ok to call the getCurrentStatus() multiple times when it hits Malformed status response. The code flow looks good.

L Laks Chanamolu

Thanks for confirming SSi1.

CONTACT
Can’t find what you’re looking for?