Android Question Inline java code

Marco Verdata

New Member
Hi all

We are developing an app for an Android device that has an integrated thermal printer. This device includes .aar files that we are adding, and through a static Java class, we call the necessary methods to perform the printing. Everything has been working perfectly, except for one method that checks whether the printer has paper or not. This particular method is returning the printer's status at the moment the application is loaded. If there are any changes to the paper status during the app's usage, it does not reflect them.

We have an example developed in Flutter that uses the same .aar file, and in that case, it works perfectly. It seems that the method's result is being cached or something similar. We understand that, since this is a third-party library, there could be other factors at play, but if possible, we would appreciate any assistance.

Thank you in advance.

Java definition:
#if Java
import com.elgin.e1.Impressora.Termica;
import com.elgin.e1.Comunicacao.ConUSB;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import android.app.Activity;
import com.elgin.minipdvm8.PrinterManager;

public boolean _onCreateOptionsMenu(android.view.Menu menu) {
     processBA.raiseEvent(null, "create_menu", menu);
     return true;   
}

public String getAndroidVersion() {
    String release = android.os.Build.VERSION.RELEASE;
    int sdkVersion = android.os.Build.VERSION.SDK_INT;
    //return "Android SDK: " + sdkVersion + " (" + release +")";
    return release;
}

public static class PrinterService {
    public boolean isPrinterInternSelected;
    public Activity obj;

    public PrinterService() {
    }

    public void setActivity(Activity param) {
        obj = param;
    }

    public int printerInternalImpStart() {
        printerStop();
            
        Termica.setContext(obj);
        int result = Termica.AbreConexaoImpressora(5, "", "", 0);

        if (result == 0) this.isPrinterInternSelected = true;

        return result;
    }

    public int printerExternalImpStartByBT(String model, String address) {
        printerStop();

        Termica.setContext(obj);
        int result = Termica.AbreConexaoImpressora(4, model, address, 0);

        if (result == 0) this.isPrinterInternSelected = false;

        return result;
    }

    public int printerExternalImpStartByIP(String model, String ip, int port) {
        printerStop();
        try {

               Termica.setContext(obj);
            int result = Termica.AbreConexaoImpressora(3, model, ip, port);

            if (result == 0) this.isPrinterInternSelected = false;

            return result;
        } catch (Exception e) {
            return printerInternalImpStart();
        }
    }

    public int printerExternalImpStartByUSB(String model) {
        printerStop();
        try {
        
            Termica.setContext(obj);
            int result = Termica.AbreConexaoImpressora(1, model, "USB", 0);

            if (result == 0) this.isPrinterInternSelected = false;

            return result;
        } catch (Exception e) {
            return printerInternalImpStart();
        }
    }

    public void printerStop() {
        Termica.FechaConexaoImpressora();
    }

    public int AvancaLinhas(int lines) {
        return Termica.AvancaPapel(lines);
    }

    public int cutPaper(int lines) {
        return Termica.Corte(lines);
    }

    public int imprimeTexto(String text, String align, String font, int fontSize, boolean isUnderline, boolean isBold) {
        int result = 0;
        int alignValue = 0;
        int styleValue = 0;

        // ALINHAMENTO VALUE
        switch (align) {
            case "Esquerda":
                alignValue = 0;
                break;
            case "Centralizado":
                alignValue = 1;
                break;
            default:
                alignValue = 2;
                break;
        }

        //STILO VALUE
        if (font.equals("FONT B")) {
            styleValue += 1;
        }
        if (isUnderline) {
            styleValue += 2;
        }
        if (isBold) {
            styleValue += 8;
        }

        result = Termica.ImpressaoTexto(text, alignValue, styleValue, fontSize);
        return result;
    }

    private int codeOfBarCode(String barCodeName) {
        switch (barCodeName) {
            case "UPC-A":
                return 0;
            case "UPC-E":
                return 1;
            case "EAN 13":
            case "JAN 13":
                return 2;
            case "EAN 8":
            case "JAN 8":
                return 3;
            case "CODE 39":
                return 4;
            case "ITF":
                return 5;
            case "CODE BAR":
                return 6;
            case "CODE 93":
                return 7;
            case "CODE 128":
                return 8;
            default:
                throw new AssertionError(barCodeName);
        }
    }

    public int imprimeBarCode(int barCodeType, String text, int height, int width, String align) {
        final int hri = 4; // NO PRINT
        final int result;
        final int alignValue;

        // ALINHAMENTO VALUE
        switch (align) {
            case "Esquerda":
                alignValue = 0;
                break;
            case "Centralizado":
                alignValue = 1;
                break;
            default:
                alignValue = 2;
                break;
        }

        Termica.DefinePosicao(alignValue);

        result = Termica.ImpressaoCodigoBarras(barCodeType, text, height, width, hri);

        return result;
    }

    public int imprimeQR_CODE(int size, String text, String align) {
        final int nivelCorrecao = 2;
        final int result;
        final int alignValue;

        // ALINHAMENTO VALUE
        switch (align) {
            case "Esquerda":
                alignValue = 0;
                break;
            case "Centralizado":
                alignValue = 1;
                break;
            default:
                alignValue = 2;
                break;
        }

        Termica.DefinePosicao(alignValue);

        result = Termica.ImpressaoQRCode(text, size, nivelCorrecao);
        return result;
    }

    public int imprimeXMLNFCe(String xmlNFCe, int indexcsc, String csc, int param) {
        return Termica.ImprimeXMLNFCe(xmlNFCe, indexcsc, csc, param);
    }

    public int imprimeXMLSAT(String xml, int param) {
        return Termica.ImprimeXMLSAT(xml, param);
    }

    //public int imprimeCupomTEF(String base64) {
    //    return Termica.ImprimeCupomTEF(base64);
    //}

    public int statusGaveta() {
        return Termica.StatusImpressora(1);
    }

    public int abrirGaveta() {
        return Termica.AbreGavetaElgin();
    }

    public int statusSensorPapel() {
        return Termica.StatusImpressora(3);
    }
}

#End If

Call to method:
    Dim act As JavaObject
    act.InitializeContext
            
    '= Me       
    Dim jo As JavaObject
    jo.InitializeNewInstance("com.verdata.pdv.main.PrinterService", Null)
            
    jo.RunMethod("setActivity", Array(act))
            
    jo.RunMethod("printerInternalImpStart", Null)

    'verificar impressora sem papel
    Dim status As Int
    status = jo.RunMethod("statusSensorPapel", Null)
    
    MsgboxAsync(status, "Status")
    
    jo.RunMethod("printerStop", Null)
 
Solution
Hello again.

After a few attempts, we managed to get a response from the manufacturer.

Although in the Flutter example the connection with the printer was opened, the paper check command was sent, and then the connection was closed, the flow suggested by the manufacturer was to open the connection with the printer when the app starts and keep it constantly connected.

After making this change, the method started working correctly.

Thank you for your help.

DonManfred

Expert
Licensed User
Longtime User
Last edited:
Upvote 0

Marco Verdata

New Member
Hello again.

After a few attempts, we managed to get a response from the manufacturer.

Although in the Flutter example the connection with the printer was opened, the paper check command was sent, and then the connection was closed, the flow suggested by the manufacturer was to open the connection with the printer when the app starts and keep it constantly connected.

After making this change, the method started working correctly.

Thank you for your help.
 
Upvote 0
Solution

drgottjr

Expert
Licensed User
Longtime User
betwixt and between; it's portuguese
 
Upvote 0
Top