mazenrashed / Printooth

A well documented, high-level Android interface that makes printing via bluetooth printers easier
Mozilla Public License 2.0
400 stars 114 forks source link

Thermal printer in TPCL, RCT mode issue!!! #39

Closed darroite closed 4 years ago

darroite commented 4 years ago

I developed an app using Printooth, everything is fine when the printer is in ESC/POS mode, but if I switch the printer's configuration to TPCL or RCT (for label paper feed control) it looks like my app send the command, the printer sign as receiving the command, but just doesn't print.

If anyone have ever developed Android App for Thermal label printer, please share your experience.

mazenrashed commented 4 years ago

Sorry, I don't have an experience with this kind of printers

darroite commented 4 years ago

The printer's configuration is not a big issue, the printer I'm using actually controls the feed of the label paper when installed on windows with a proper driver, but when the print command is sent from an android app it seams that the printer only works in ESC/POS mode.

Now I'm trying to control the feed of label paper by software, I have founded a big collection of ESC/POS commands that controls the paper feed, paper cut, paper stop position, etc... I just can't properly apply this commands on my code.

darroite commented 4 years ago

Here is my activity code:

package com.example.warehousev3;

import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;

import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity;

import com.mazenrashed.printooth.Printooth; import com.mazenrashed.printooth.data.printable.Printable; import com.mazenrashed.printooth.data.printable.RawPrintable; import com.mazenrashed.printooth.data.printable.TextPrintable; import com.mazenrashed.printooth.data.printer.DefaultPrinter; import com.mazenrashed.printooth.ui.ScanningActivity; import com.mazenrashed.printooth.utilities.Printing; import com.mazenrashed.printooth.utilities.PrintingCallback;

import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar;

public class MainActivity extends AppCompatActivity implements PrintingCallback { DatabaseHelper myDb; Button btnPrint, btn_unpair_pair; EditText barcodefield = null;

Printing printing;

private Calendar calendar;
private SimpleDateFormat dateFormat;
private String date;

private final static char ESC_CHAR = 0x1B; // to be used on esc/pos commands

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    calendar = Calendar.getInstance();
    dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    date = dateFormat.format(calendar.getTime());

    myDb = new DatabaseHelper(this);
    btnPrint = findViewById(R.id.cmd_print);
    barcodefield = findViewById(R.id.txt_barcode);
    btn_unpair_pair = findViewById(R.id.btnPiarUnpair);

    btnPrint.setVisibility(View.GONE); //Set Print Button Invisible

    viewAll();
}

public void viewAll() {
    if (printing != null)
        printing.setPrintingCallback(this);

    btn_unpair_pair.setOnClickListener(view -> {
        if (Printooth.INSTANCE.hasPairedPrinter())
            Printooth.INSTANCE.removeCurrentPrinter();
        else {
            startActivityForResult(new Intent(MainActivity.this, ScanningActivity.class), ScanningActivity.SCANNING_FOR_PRINTER);
            changePairAndUpair();
        }
    });

/ //Print Button function btnPrint.setOnClickListener(view -> { if (!Printooth.INSTANCE.hasPairedPrinter()) startActivityForResult(new Intent(MainActivity.this, ScanningActivity.class), ScanningActivity.SCANNING_FOR_PRINTER); else printText(); barcodefield.setText(""); }); /

    barcodefield.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            for (int i = 0; i < s.length(); i++) {
                if(barcodefield.getText().toString().length() == 13) {
                    if (!Printooth.INSTANCE.hasPairedPrinter())
                        startActivityForResult(new Intent(MainActivity.this, ScanningActivity.class), ScanningActivity.SCANNING_FOR_PRINTER);
                    else
                        printText();
                        barcodefield.setText("");
                }
            }
        }
    });
    changePairAndUpair();
}

private void changePairAndUpair() {
    if (Printooth.INSTANCE.hasPairedPrinter())
        btn_unpair_pair.setText(new StringBuilder("Unpair ").append(Printooth.INSTANCE.getPairedPrinter().getName()).toString());
    else
        btn_unpair_pair.setText("Pair with Printer");
}

@Override
public void connectingWithPrinter() {
    Toast.makeText(this, "Connectiong to printer", Toast.LENGTH_SHORT).show();
}

@Override
public void connectionFailed(String s) {
    Toast.makeText(this, "Failed: "+s, Toast.LENGTH_SHORT).show();
}

@Override
public void onError(String s) {
    Toast.makeText(this, "Error: "+s, Toast.LENGTH_SHORT).show();

}

@Override
public void onMessage(String s) {
    Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
}

@Override
public void printingOrderSentSuccessfully() {
    Toast.makeText(this, "Order sent to printer", Toast.LENGTH_SHORT).show();
}

private void printText() {
    ArrayList<Printable> printables = new ArrayList<>();
    //printables.add(new RawPrintable.Builder(new byte[]{27, 100, 4}).build());
    printables.add(new RawPrintable.Builder(new byte[]{0x1B,'d', 5}).build()); // FEED 5 LINES WORKING!!!!!!!!
    //printables.add(new RawPrintable.Builder(new byte[]{'L','H', 0x56}).build()); // FEED 10 LINES WORKING!!!!!!!!
    //printables.add(new RawPrintable.Builder(new byte[]{0x0C}).build()); // lane for test

    myDb.setBarcodcheck(String.valueOf(barcodefield.getText()));
    Cursor res = myDb.getAllData();
    if (res.getCount() == 0) {
        showMessage("Error", "Nothing Found");
        return;
    }
    StringBuffer buffer = new StringBuffer();
    while (res.moveToNext()) {
        buffer.append(res.getString(0)+"\n");
        buffer.append(res.getString(1)+"\n");
        buffer.append(res.getString(2)+"\n");
        buffer.append(date+"\n");
        buffer.append("\n");
    }
    printables.add(new TextPrintable.Builder()
            .setText(buffer.toString()) // Original code
            //.setText(String.valueOf(buffer))
            .setCharacterCode(DefaultPrinter.Companion.getCHARCODE_PC1252())
            .setLineSpacing(DefaultPrinter.Companion.getLINE_SPACING_60())
            .setEmphasizedMode(DefaultPrinter.Companion.getEMPHASIZED_MODE_BOLD())
            .setFontSize(DefaultPrinter.Companion.getFONT_SIZE_LARGE())
            //.setNewLinesAfter(1)
            .build());
    printables.add(new RawPrintable.Builder(new byte[]{0x1B,'d', 5}).build()); // FEED 5 LINES WORKING!!!!!!!!
    //printables.add(new RawPrintable.Builder(new byte[]{0x1b, 0x40}).build()); // Line for test
    Printooth.INSTANCE.printer().print(printables);
}

public void showMessage(String title, String Message) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(Message);
    builder.show();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == ScanningActivity.SCANNING_FOR_PRINTER && resultCode == Activity.RESULT_OK)
        initPrinting();
        changePairAndUpair();
    }

private void initPrinting() {
    if (!Printooth.INSTANCE.hasPairedPrinter())
        printing = Printooth.INSTANCE.printer();
    if (printing != null)
        printing.setPrintingCallback(this);
}

}