Skip to content

Instantly share code, notes, and snippets.

@grigor-aramyan
Created August 25, 2017 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grigor-aramyan/6b54808e378c550b287dbbb1b1b9fe23 to your computer and use it in GitHub Desktop.
Save grigor-aramyan/6b54808e378c550b287dbbb1b1b9fe23 to your computer and use it in GitHub Desktop.
Finding and sending commands to Star Micronics receipt printer
// Searching for printer with specified MAC address
private class SearchTask extends AsyncTask<ActiveOrderRealm, Void, ActiveOrderRealm> {
private List<PortInfo> mPortList;
private String mMacAddress = "00:11:62:17:19:EF";
private boolean mFound = false;
SearchTask() {
super();
}
@Override
protected ActiveOrderRealm doInBackground(ActiveOrderRealm... activeOrder) {
try {
mPortList = StarIOPort.searchPrinter(PrinterSetting.IF_TYPE_BLUETOOTH, getApplicationContext());
}
catch (StarIOPortException e) {
mPortList = new ArrayList<>();
}
return activeOrder[0];
}
@Override
protected void onPostExecute(ActiveOrderRealm activeOrder) {
for (PortInfo info : mPortList) {
if (info.getMacAddress().equals(mMacAddress)) {
String portName = info.getPortName();
if (info.getPortName().startsWith(PrinterSetting.IF_TYPE_BLUETOOTH)) {
portName = PrinterSetting.IF_TYPE_BLUETOOTH + info.getMacAddress();
}
PrinterSetting setting = new PrinterSetting(getApplicationContext());
setting.write("TSP100", portName, mMacAddress, "", StarIoExt.Emulation.StarGraphic, true);
mFound = true;
break;
}
}
if (mFound) {
new SendCommandTask().execute(activeOrder);
} else {
Toast.makeText(getApplicationContext(), "No specified mac address found!", Toast.LENGTH_LONG).show();
}
}
}
//**********************
// Send commands to found printer
private class SendCommandTask extends AsyncTask<ActiveOrderRealm, Void, Void> {
// mLogo is never used after refactoring, don't pay attention to it
private Bitmap mBitmap = null, mLogo = null;
@Override
protected void onPreExecute() {
// helper function that draws the receipt on bitmap canvas
mBitmap = drawOnImage();
}
@Override
protected Void doInBackground(ActiveOrderRealm... activeOrder) {
ActiveOrderRealm currentActiveOrder = activeOrder[0];
byte[] data = null;
PrinterSetting setting = new PrinterSetting(getApplicationContext());
StarIoExt.Emulation emulation = setting.getEmulation();
data = createCommandsImageNew(emulation, mBitmap, mLogo);
Communication.sendCommands(this, data, setting.getPortName(), setting.getPortSettings(), 10000,
getApplicationContext(), mCallback); // 10000mS!!!
return null;
}
private Communication.SendCallback mCallback = new Communication.SendCallback() {
@Override
public void onStatus(boolean result, Communication.Result communicateResult) {
String msg = "";
switch (communicateResult) {
case Success :
msg = "Success!";
break;
case ErrorOpenPort:
msg = "Fail to openPort";
break;
case ErrorBeginCheckedBlock:
msg = "Printer is offline (beginCheckedBlock)";
break;
case ErrorEndCheckedBlock:
msg = "Printer is offline (endCheckedBlock)";
break;
case ErrorReadPort:
msg = "Read port error (readPort)";
break;
case ErrorWritePort:
msg = "Write port error (writePort)";
break;
default:
msg = "Unknown error";
break;
}
if (!msg.isEmpty())
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
};
}
//**********************************
// function that creates commands for printer
public static byte[] createCommandsImageNew(StarIoExt.Emulation emulation, Bitmap bitmap, Bitmap logo) {
ICommandBuilder builder = StarIoExt.createCommandBuilder(emulation);
builder.beginDocument();
builder.appendBitmap(bitmap, false, PrinterSetting.PAPER_SIZE_THREE_INCH, true);
builder.appendCutPaper(ICommandBuilder.CutPaperAction.FullCutWithFeed);
builder.endDocument();
return builder.getCommands();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment