Code Monkey home page Code Monkey logo

Comments (3)

htooaunghlaing avatar htooaunghlaing commented on July 28, 2024

I still hope for that with callback for device and printer status : CONNECTED, DISCONNECTED, CONNECTING

from escpos-thermalprinter-android.

DantSu avatar DantSu commented on July 28, 2024

All can be do with Exception :

If you read the code of BluetoothPrintersConnections.selectFirstPaired() :

        BluetoothPrintersConnections printers = new BluetoothPrintersConnections();
        BluetoothConnection[] bluetoothPrinters = printers.getList();
        
        if (bluetoothPrinters != null && bluetoothPrinters.length > 0) {
            for (BluetoothConnection printer : bluetoothPrinters) {
                try {
                    return printer.connect();
                } catch (EscPosConnectionException e) {
                   // Printer is disconnected
                    e.printStackTrace();
                }
            }
        }
        return null;

You can see that BluetoothPrintersConnections.selectFirstPaired() return null if no printer is connected to the device.

If you print text and the printer is disconnected, it's throwing an exception, like this :

        try {
            EscPosPrinter printer = new EscPosPrinter(printerConnection, 203, 48f, 32);
            printer .printFormattedText("[L]Some text to print\n[L][R]Some text to print\n");
        } catch (EscPosConnectionException e) {
            // The printer is disconnected !
        } catch (EscPosParserException e) {
            // There is a formatted text problem (Bad syntax)
        } catch (EscPosEncodingException e) {
            // There is an encoding character problem
        } catch (EscPosBarcodeException e) {
            // There is a QRCode or Barcode generation problem
        }

I still hope for that with callback for device and printer status : CONNECTED, DISCONNECTED, CONNECTING

The status "CONNECTING" doesn't exists. Devide is connected or disconnected. With Exception you have the answer of this question.

If you want to display a windows with a loader during the printing, you have to use a separated thread like :

public class AsyncEscPosPrint extends AsyncTask<EscPosPrinter, Integer, Integer>

from escpos-thermalprinter-android.

DantSu avatar DantSu commented on July 28, 2024

https://github.com/DantSu/ESCPOS-ThermalPrinter-Android/blob/master/app/src/main/java/com/dantsu/thermalprinter/MainActivity.java

You can read the asynchronous implementation. It might interrest you.
Git clone or pull the master branch, and test it.

from escpos-thermalprinter-android.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.