Epson ePOS Printer not printing - Android SDK

DJ-DOO picture DJ-DOO · Feb 4, 2015 · Viewed 7.4k times · Source

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);
        }
    }
}

Answer

DJ-DOO picture DJ-DOO · Feb 5, 2015

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.