I have found an API here
However, when I run the AndroidSample test app I get errors.
When I press on the "Get Printer Status" button I get "Printer is online". This button works it seems.
However:
Pressing "Read data from printer" yields "Failure. could not read in the firmware name."
Pressing "Print Receipt" cause the application to hang for 3 seconds. Then nothing.
Pressing "Print Checked Block Receipt" yields either "printing succeeded" or a big hang (sometimes force close). In any case, nothing is printed.
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.