如何让星级TSP100LAN收据打印机与Android配合使用?

时间:2011-07-07 18:13:50

标签: android printing receipt javapos stario-sdk

我找到了一个API here

但是,当我运行AndroidSample测试应用时,我会收到错误。

当我按下“获取打印机状态”按钮时,我得到“打印机在线”。这个按钮看起来很有用。

但是:

  • 按“从打印机读取数据”会产生“失败。无法读取固件名称。”

  • 按“打印收据”会导致应用程序挂起3秒钟。没什么。

  • 按“Print Checked Block Receipt”会产生“打印成功”或大挂(有时强制关闭)。在任何情况下,都不会打印任何内容。

4 个答案:

答案 0 :(得分:6)

我花了很长时间才弄清楚如何让它发挥作用。我会尽我所能让你开始。我对android很新,所以请随意指出我做错的事情。通过向上移动内容偶尔会错误地打印,从而切掉顶部并在底部添加很多空间。如果有人能想出来我会非常感激。

你走了:

要求APK中的这些文件:不要相信我修改了它们: 1. RasterDocument.java 2. StarBitmap.java

主要印刷方法:

    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: 在另一种方法中,向PrintReceipt发送一个relativelayout。在类的构造函数中设置当前上下文。

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

}

同样,我是android的新手,这可能不是最好的方式,但它是打印。你发现任何酷我都喜欢听他们。

答案 1 :(得分:3)

阅读iOS和Android StarIO SDK的文档,我发现STAR TSP100LAN要求它处于“光栅模式”。不幸的是,iPhone和Android SDK提供的样本仅用于“行模式”打印。虽然这不是一个答案,但希望它能帮助您指明正确的方向:)

我将尝试联系Star自己,看看我是否可以直接从他们那里得到一些示例代码,祝我好运,我会在这里报告我得到的任何回复!

答案 2 :(得分:0)

您是否在Activity上添加了AndroidManifest?并添加您的应用程序需要允许的权限?

答案 3 :(得分:0)

Star添加了一个新的Android SDK包,其功能比此主题中提到的旧版本更多(我认为是V1.0)。新的here

可用

如上所述,TSP100LAN需要接收Raster命令,因为它本身就是一个图形打印机。最新的SDK包有一个更新的示例应用程序,可让您测试许多不同的打印机功能,包括一些Raster命令。

在撰写此答案时可供下载的SDK包是V2.3。手册(README_StarIO_POSPrinter_Android_SDK.pdf)声明TSP100LAN可以使用开放式现金抽屉,获取状态,打印光栅图形文本(装饰文本/将其发送到打印机)和图像文件打印(优惠券)功能。

Star还有一个编程手册,其中包含详细说明其他功能的光栅命令。见section 3.4