我的爱普生POS打印机出了点问题。爱普生提供的示例可以正常工作,但是当我尝试在Xamarin中使用的时候!
我创建了一个Android Java Bindings Library,添加了一个文件夹"Jars“,并复制了其中的ePos-Prin.jar文件,选项为EmbeddedJar as Build 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();
}
}
}
}有人能帮我吗?
发布于 2015-06-10 22:45:47
我没有爱普生USB打印机,但我在不同的打印机上尝试了下面这段简单的代码,我可以告诉你它是有效的。看看你是否能以某种方式创建一个你想要打印的文件,然后尝试下面这个简单的方法,也许它也适用于该打印机:
public void print () {
Desktop desktop = Desktop.getDesktop();
try {
desktop.print(new File("Docfile.pdf"));
} catch (IOException e) {
e.printStackTrace();
}
}https://stackoverflow.com/questions/30575144
复制相似问题