但是,当我运行 AndroidSample 测试应用程序时出现错误。
当我按下“获取打印机状态”按钮时,我得到“打印机在线”。这个按钮似乎有效。
然而:
按“从打印机读取数据”会产生“失败。无法读取固件名称。”
按“打印收据”会导致应用程序挂起 3 秒。然后什么都没有。
按“Print Checked Block Receipt”会产生“打印成功”或大挂起(有时强制关闭)。在任何情况下,都不会打印任何内容。
但是,当我运行 AndroidSample 测试应用程序时出现错误。
当我按下“获取打印机状态”按钮时,我得到“打印机在线”。这个按钮似乎有效。
然而:
按“从打印机读取数据”会产生“失败。无法读取固件名称。”
按“打印收据”会导致应用程序挂起 3 秒。然后什么都没有。
按“Print Checked Block Receipt”会产生“打印成功”或大挂起(有时强制关闭)。在任何情况下,都不会打印任何内容。
It took me forever to figure out how to get it to work. I'll give you what I can to get you started. I'm fairly new to android, so feel free to point out things I am be doing incorrectly. It does occasionally incorrectly print by moving the content up, thus cutting off the top and adds a lot space at the bottom. If anyone can figure that out I would be much appreciated.
Here you go:
Requires these files from the APK: Don't believe i modified them anyway: 1. RasterDocument.java 2. StarBitmap.java
Main printing method:
public static void PrintReceipt(Context context, RelativeLayout layout){
String portName = "tcp:10.1.250.20"; //ip address of your printer
String portSettings = "";
//have to measure the layout for it to print correctly, otherwise sizes are zero
layout.measure(View.MeasureSpec.makeMeasureSpec(layout.getLayoutParams().width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(layout.getLayoutParams().height, View.MeasureSpec.EXACTLY));
layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(layout.getWidth(),layout.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
layout.draw(canvas);
int maxWidth = 576; //default width of tsp100 receipt
RasterDocument rasterDoc = new RasterDocument(RasterDocument.RasSpeed.Full, RasterDocument.RasPageEndMode.FeedAndFullCut, RasterDocument.RasPageEndMode.FeedAndFullCut, RasterDocument.RasTopMargin.Standard, 0, 0, 0);
StarBitmap starbitmap = new StarBitmap(bitmap, false, maxWidth);
StarIOPort port = null;
try
{
/*
using StarIOPort3.1.jar (support USB Port)
Android OS Version: upper 2.2
*/
port = StarIOPort.getPort(portName, portSettings, 10000, context);
/*
using StarIOPort.jar
Android OS Version: under 2.1
port = StarIOPort.getPort(portName, portSettings, 10000);
*/
try
{
Thread.sleep(500);
}
catch(InterruptedException e) {}
byte[] command = rasterDoc.BeginDocumentCommandData();
port.writePort(command, 0, command.length);
command = starbitmap.getImageRasterDataForPrinting();
port.writePort(command, 0, command.length);
command = rasterDoc.EndDocumentCommandData();
port.writePort(command, 0, command.length);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e) {}
}
catch (StarIOPortException e)
{
ShowAlertMessage(context, "Failure", "Failed to connect to printer. " + e.getMessage());
}
finally
{
if(port != null)
{
try {
StarIOPort.releasePort(port);
} catch (StarIOPortException e) {}
}
}
}
private static void ShowAlertMessage(final Context context, final String alertTitle, final String message){
try {
((Activity)context).runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setNegativeButton("Ok", null);
AlertDialog alert = dialog.create();
alert.setTitle(alertTitle);
alert.setMessage(message);
alert.show();
}});
} catch (final Exception e) {
Log.e(PrinterFunctions.class.getName(), e.getMessage());
}
}
THEN: In another method, send PrintReceipt a relativelayout. Set the current context in the constructor of the class.
Context currentContext;
RelativeLayout relativeLayout;
private final int receiptWidth = 576;
private Typeface typeFace = Typeface.DEFAULT;
private int normalFontSize = 23;
private int largeFontSize = 28;
public SetupReceiptClass(Context context){
currentContext = context;
}
public void SetupReceipt(String customerName){
//Create layout for receipt
relativeLayout = new RelativeLayout(currentContext);
RelativeLayout.LayoutParams params;
params = new RelativeLayout.LayoutParams(receiptWidth, ViewGroup.LayoutParams.MATCH_PARENT);
relativeLayout.setLayoutParams(params);
relativeLayout.setId(R.id.ReceiptLayout);
//Create whatever views you want here and add them to the RelativeLayout to make up your receipt
relativeLayout.addView(whateverViewsYouCreate);
//Finally, Print the receipt.
new Thread(new Runnable() {
@Override
public void run() {
PrintReceipt(currentContext, relativeLayout);
}
}).start();
}
Again, I'm new to the android thing, and this may not be the best way, but it is printing. Anything cool you find out I'd love to hear them.
阅读 iOS 和 Android StarIO SDK 的文档后,我发现 STAR TSP100LAN 要求它处于“光栅模式”。不幸的是,iPhone 和 Android SDK 提供的示例仅用于在“行模式”下打印。虽然这不是一个答案,但希望它能帮助您指出正确的方向:)
我将尝试自己联系 Star,看看是否可以直接从他们那里获得一些示例代码,祝我好运,我会在这里报告我得到的任何回复!
你加了你Activity
的AndroidManifest
吗?并添加您的应用需要允许它们的权限?
Star 添加了一个新的 Android SDK 包,其功能比这个线程中提到的旧版本(我相信是 V1.0)更多。新的可以在这里找到
如上所述,TSP100LAN 需要接收光栅命令,因为它本身就是一个图形打印机。最新的 SDK 包有一个更新的示例应用程序,可让您测试许多不同的打印机功能,包括一些光栅命令。
在撰写此答案时可供下载的 SDK 包是 V2.3。手册 (README_StarIO_POSPrinter_Android_SDK.pdf) 说明 TSP100LAN 可以使用 Open Cash Drawer、获取状态、打印光栅图形文本(装饰文本/将其发送到打印机)和图像文件打印(优惠券)功能。
Star 还有一本编程手册,其中包含详细说明其他功能的光栅命令。见第 3.4 节