5

I'm working with an Epson ePOS printer TM-T20 and I've run the SDK sample and it works, however, I'm trying to write a small application to print.

The printer is found successfully and the data is sent successfully but it doesn't print. If anyone could help I'd appreciate it.

Here is my code:

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.garydoolin.newepsontest" >
<uses-permission android:name="android.permission.USB"></uses-permission>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MyActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
            android:resource="@xml/device_filter" />
    </activity>
</application>

Here is my main activity

package com.example.test.newepsontest;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.epson.eposprint.Builder;
import com.epson.eposprint.EposException;
import com.epson.eposprint.Print;
import com.epson.epsonio.DevType;
import com.epson.epsonio.DeviceInfo;
import com.epson.epsonio.EpsonIoException;
import com.epson.epsonio.Finder;
import com.epson.epsonio.IoStatus;


public class MyActivity extends Activity {

    static final int SEND_TIMEOUT = 10 * 1000;
    private DeviceInfo[] mDeviceList;
    private Context mContext;

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

        Button find = (Button)findViewById(R.id.findDevice);
        Button print = (Button)findViewById(R.id.print);

        mContext = this;

        print.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    Print();
                } catch (EpsonIoException e) {
                    e.printStackTrace();
                    ShowMsg.showException(e, "Print ",mContext);
                }
            }
        });

        find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    getDevices();
                } catch (EpsonIoException e) {
                    e.printStackTrace();
                    ShowMsg.showException(e, "Get Devices", mContext);
                }
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void getDevices() throws EpsonIoException {

        try {
            Finder.start(this, DevType.USB, null);
        } catch (EpsonIoException e) {
            ShowMsg.showException(e, "getDevices", mContext);
            if (e.getStatus() == IoStatus.ERR_ILLEGAL) {
                Toast.makeText(this, String.valueOf("SEARCH ALREADY IN PROGRESS"), Toast.LENGTH_SHORT).show();
            } else if (e.getStatus() == IoStatus.ERR_PROCESSING) {
                Toast.makeText(this, String.valueOf("COULD NOT EXECUTE PROCESS"), Toast.LENGTH_SHORT).show();
            } else if (e.getStatus() == IoStatus.ERR_PARAM) {
                Toast.makeText(this, String.valueOf("INVALID PARAMETER PASSED"), Toast.LENGTH_SHORT).show();
            } else if (e.getStatus() == IoStatus.ERR_MEMORY) {
                Toast.makeText(this, String.valueOf("COULD NOT ALLOCATE MEMORY"), Toast.LENGTH_SHORT).show();
            } else if (e.getStatus() == IoStatus.ERR_FAILURE) {
                Toast.makeText(this, String.valueOf("UNSPECIFIED ERROR"), Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void Print() throws EpsonIoException {
        mDeviceList = Finder.getDeviceInfoList(Finder.FILTER_NONE);

        int[] status = new int[1];

        if(mDeviceList.length>0){Finder.stop();}else{Toast.makeText(getBaseContext(), "List is null", Toast.LENGTH_SHORT).show();}

        String deviceName = mDeviceList[0].getDeviceName();
        String printerName = mDeviceList[0].getPrinterName();
        int deviceType = mDeviceList[0].getDeviceType();
        String macAddress = mDeviceList[0].getMacAddress();
        Print printer = new Print(getApplicationContext());

        Log.i("Device Name: " + deviceName +"\n" + "Printer Name: " + printerName + "\n" + "Device Type: " + String.valueOf(deviceType) + "\n" + "MAC: " +macAddress, "");

        try {

            //Print Data Builder
            Builder builder = new Builder("TM-T20", Builder.MODEL_ANK, getApplicationContext());
            builder.addText("ESPON PRINT TEST");
            builder.addCut(Builder.CUT_FEED);

            if(builder!=null) {
                Log.i("BUILDER NOT NULL", "");
            }

            //Printer Test Builder
            Builder confirmBuilder = new Builder("TM-T20", Builder.MODEL_ANK, getApplicationContext());

            //Open printer
            printer.openPrinter(Print.DEVTYPE_USB, deviceName);

            //Send Test Builder
            printer.sendData(confirmBuilder, SEND_TIMEOUT, status);

            //Check printer Status
            if((status[0] & Print.ST_OFF_LINE) != Print.ST_OFF_LINE) {
                //If online send print data
                Log.i("PRINTER NOT OFFLINE", "");
                printer.sendData(builder, SEND_TIMEOUT, status);
                
                //Check if data sent successfully
                if((status[0] & Print.ST_PRINT_SUCCESS) == Print.ST_PRINT_SUCCESS) {
                    builder.clearCommandBuffer();
                    Toast.makeText(this, "DATA SENT SUCCESSFULLY", Toast.LENGTH_SHORT).show();
                }
                printer.closePrinter();
            } else if ((status[0] & Print.ST_OFF_LINE) == Print.ST_OFF_LINE) {
                Toast.makeText(this, "PRINTER OFFLINE", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this, "OTHER PRINTER ERROR", Toast.LENGTH_SHORT).show();
            }
        } catch (EposException e) {
            e.printStackTrace();
            ShowMsg.showException(e,"Print", mContext);
        }
    }
}
Abed
  • 3,999
  • 1
  • 17
  • 28
DJ-DOO
  • 4,545
  • 15
  • 58
  • 98
  • 1
    Thanks for this code example ! – gdlin Jul 08 '15 at 23:46
  • 3
    @gdlin if it helped perhaps you could upvote my answer – DJ-DOO Jul 09 '15 at 09:33
  • @DJ-DOO I have added the ePOS2 jar file in Android studio and used your code to get the list of Printers, but this isn't working. Can you please guide me to implement the printer list in my android app. – Bhavna Mar 05 '19 at 07:09

1 Answers1

5

I'm not sure if anyone is interested but it appears that my code was indeed 100% correct! For whatever reason it didn't like my test string...when I put in actual text that I will be printing it printed no problem...I'm not sure what the problem is/was but it works.

DJ-DOO
  • 4,545
  • 15
  • 58
  • 98
  • erm, that's my code in my op...that is the code to search and open the printer. Ensure that your epson printer is compatible with the sdk, these are listed in the sdk documentation – DJ-DOO Feb 06 '15 at 10:36
  • Printer is compatible with SDK. I am unable to find printer – Ashok Kateshiya Feb 06 '15 at 10:56
  • is your android device have usb host capabilities? If not, it won't recognise any usb printer...are you running over usb? – DJ-DOO Feb 06 '15 at 11:02
  • 1
    Have you read through the epson sdk docs?? This may also be the issue, from the docs: _"Since the printer search takes time to complete, you might not receive any search results if you call the Finder class's getDeviceInfoList immediately after you call start."_ – DJ-DOO Feb 06 '15 at 11:10
  • I am using LAN printer – Ashok Kateshiya Feb 06 '15 at 12:32
  • TM-T81 thermal receipt printer – Ashok Kateshiya Feb 06 '15 at 12:33
  • 1
    you haven't told me if you've read the docs...I think you'll need to figure it out... – DJ-DOO Feb 06 '15 at 12:37
  • hey can you tell me, how could you arrange you're data (text to be print). I need to maintain data similar to some receipt, And also need to print one image. Can you help me out in this..... ? – LuminiousAndroid Aug 11 '15 at 12:40
  • 1
    @LuminiousAndroid Depending on the width of your printer roll I set up a switch case and padded the text to suit eg. Date: ---padding--- 11/08/15 – DJ-DOO Aug 11 '15 at 14:17
  • I did not get you on this @DJ I need proper alignment of data like 1**************Pineapple**************$10 2**************Apple**************$20 PL: replace * with blank spaces – LuminiousAndroid Aug 11 '15 at 15:56
  • hope I was able to make you understand receipt structure – LuminiousAndroid Aug 11 '15 at 15:57
  • So you need to find out from the client the width of the printer roll (receipt). Then if you have to cater for more than one width you will use the switch statement. So you will have: 1-(right padding on '1')-Apple-(right padding on 'Apple'-$20). You will need to use trial and error. I suggest going through the pos command docs https://www.sparkfun.com/datasheets/Components/General/Driver%20board.pdf on page 6. Alignment. Don't forget to upvote these if you find them useful – DJ-DOO Aug 11 '15 at 16:02