在Epson TM-U220 USB上打印时出错

时间:2015-06-01 13:42:03

标签: c# android xamarin

我遇到过爱普生POS打印机的情况。爱普生提供的示例按预期工作,但当我尝试在Xamarin BOOM中使用时!

我创建了一个Android Java Bindings Library,添加了一个“Jars”文件夹,并将ePos-Prin.jar文件复制到其中,选项EmbeddedJarBuild Action

在我的Android项目中,我将.so文件(本机库)放在“jni / armeabi”文件夹树中,并将Build Action选择为AndroidNativeLibrary。同样在Android项目中,我有一个简单的MainActivity类,它将尝试查找打印机,但不能抛出异常。

我正在使用以下代码行:

using System;    
using Android.App;
using Android.Content;
using Android.Widget;
using Android.OS;
using Com.Epson.Epsonio; //Library from JBL
using Com.Epson.Eposprint; //Library from JBL

namespace EpsonPrint
{
    [Activity(MainLauncher=true)]
    public class MainActivity : Activity
    {
        const int SEND_TIMEOUT = 10 * 1000;
        DeviceInfo[] mDeviceList;
        Context mContext;

        protected override void OnCreate(Bundle savedInstanceState) 
        {
            //Java.Lang.JavaSystem.Load("$APP/jni/libeposprint.so"); //Doesn't work

            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.Main);

            var find = (Button)FindViewById(Resource.Id.find);
            var print = (Button)FindViewById(Resource.Id.print);

            mContext = this;

            print.Click += delegate
                {
                    try {
                        Print();
                    } catch (EpsonIoException e) {
                        e.PrintStackTrace();
                        Toast.MakeText(mContext, e.ToString(), ToastLength.Long).Show();
                    }
                };

            find.Click += delegate
            {
                    try {
                        GetDevices(); //BOOM! here...
                    } catch (EpsonIoException e) {
                        e.PrintStackTrace();
                        Toast.MakeText(mContext, e.ToString(), ToastLength.Long).Show();
                    }
            };
        }

        private void GetDevices() {

            try {
                Finder.Start(this, DevType.Usb, "null");
            } catch (EpsonIoException e) {
                if (e.Status == IoStatus.ErrIllegal) {
                    Toast.MakeText(this, "SEARCH ALREADY IN PROGRESS", ToastLength.Long).Show();
                } else if (e.Status == IoStatus.ErrProcessing) {
                    Toast.MakeText(this, "COULD NOT EXECUTE PROCESS", ToastLength.Long).Show();
                } else if (e.Status == IoStatus.ErrParam) {
                    Toast.MakeText(this, "INVALID PARAMETER PASSED", ToastLength.Long).Show();
                } else if (e.Status == IoStatus.ErrMemory) {
                    Toast.MakeText(this, "COULD NOT ALLOCATE MEMORY", ToastLength.Long).Show();
                } else if (e.Status == IoStatus.ErrFailure) {
                    Toast.MakeText(this, "UNSPECIFIED ERROR", ToastLength.Long).Show();
                }
            }
        }

        private void Print()
        {
            mDeviceList = Finder.GetDeviceInfoList(Finder.FilterNone);

            var status = new int[1];

            if (mDeviceList.Length > 0)
            {
                Finder.Stop();
            }
            else
            {
                Toast.MakeText(mContext, "List is null", ToastLength.Long).Show();
            }

            String deviceName = mDeviceList[0].DeviceName;
            String printerName = mDeviceList[0].PrinterName;
            int deviceType = mDeviceList[0].DeviceType;
            String macAddress = mDeviceList[0].MacAddress;
            Print printer = new Print(ApplicationContext);

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

            try
            {

                //Print Data Builder
                var builder = new Builder("TM-U220", Builder.ModelAnk, ApplicationContext);
                builder.AddText("ESPON PRINT TEST");
                builder.AddCut(Builder.CutFeed);

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

                //Printer Test Builder
                var confirmBuilder = new Builder("TM-U220", Builder.ModelAnk, ApplicationContext);

                //Open printer
                printer.OpenPrinter(DevType.Usb, deviceName);

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

                //Check printer Status
                if ((status[0] & Com.Epson.Eposprint.Print.StOffLine) != Com.Epson.Eposprint.Print.StOffLine)
                {
                    //If online send print data
                    //Log.i("PRINTER NOT OFFLINE", "");
                    printer.SendData(builder, SEND_TIMEOUT, status);

                    //Check if data sent successfully
                    if ((status[0] & Com.Epson.Eposprint.Print.StPrintSuccess) == Com.Epson.Eposprint.Print.StPrintSuccess)
                    {
                        builder.ClearCommandBuffer();
                        Toast.MakeText(this, "DATA SENT SUCCESSFULLY", ToastLength.Long).Show();
                    }
                    printer.ClosePrinter();
                }
                else if ((status[0] & Com.Epson.Eposprint.Print.StOffLine) == Com.Epson.Eposprint.Print.StOffLine)
                {
                    Toast.MakeText(this, "PRINTER OFFLINE", ToastLength.Long).Show();
                }
                else
                {
                    Toast.MakeText(this, "OTHER PRINTER ERROR", ToastLength.Long).Show();
                }
            }
            catch (EposException e)
            {
                e.PrintStackTrace();

                Toast.MakeText(mContext, e.ToString(), ToastLength.Long).Show();
            }
        }
    }
}

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我没有爱普生USB打印机,但我在各种打印机中尝试了以下简单的代码,我可以告诉你这样做。 看看你是否可以以某种方式创建一个你想要打印的文件,然后尝试下面这个简单的方法,也许它也适用于那台打印机:

public void print () {
        Desktop desktop = Desktop.getDesktop();
        try {
            desktop.print(new File("Docfile.pdf"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }