接上篇文章Android之BLE(低功耗)蓝牙开发,本篇文章针对上篇博文中提出的两款打印机的开发流程进行记录。
首先不管时佳博打印机还是汉印打印机,都是先对他们各自的lib进行导入,如图:
一定要记得进行sync project。一切准备就绪后接着就是根据他们各自提供的文档说明进行开发。本文不做详细说明,下面先贴汉印的打印关键代码(他们家的比较简单):
public void printCPCL(View view) {
final int maxPrintNum = 1;
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < maxPrintNum; i++) {
try {
PrinterHelper.PortClose();
Log.e(TAG, "printCPCL: =======>436");
openBT = PrinterHelper.PortOpenBT("00:15:83:FD:6F:E0");
Log.e(TAG, "printCPCL: =======>440");
if (openBT == 0) {
Log.e(TAG, "printCPCL: =======>443");
if (PrinterHelper.IsOpened()) {
Log.e(TAG, "run: " + PrinterHelper.IsOpened());
//连接成功
PrinterHelper.Align(PrinterHelper.CENTER);
PrinterHelper.SetBold("1");
PrinterHelper.SetMag("2", "2");
PrinterHelper.Text(PrinterHelper.TEXT, "8", "0", "10", "30", "啦啦啦")
PrinterHelper.SetMag("1", "1");
PrinterHelper.SetBold("0");
PrinterHelper.InverseLine("0", "0", "700", "0", "100");
PrinterHelper.Text(PrinterHelper.TEXT, "8", "0", "0", "130", "啦啦啦");
PrinterHelper.Text(PrinterHelper.TEXT, "3", "0", "0", "160", "啦啦啦");
PrinterHelper.Text(PrinterHelper.TEXT, "3", "0", "0", "190", "啦啦啦");
PrinterHelper.PrintQR(PrinterHelper.BARCODE, "10", "240", "2", "16", "eyJxcl90eXBlIjoic3RhZmYiLCJvc19pZCI6Nn0=");
PrinterHelper.Form();
PrinterHelper.Print();
}
} catch (Exception e) {
Log.e(TAG, "printCPCL: =======>553" + e.getMessage());
e.printStackTrace();
}
}
}
}).start();
}
相关的方法在厂家集成文档中都有详细的描述,我就不在这里做过多的赘述了。效果如下图:
佳博的话也一样,下面给出打印代码:
public void printSend(View view) {
threadPool = ThreadPool.getInstantiation();
threadPool.addTask(new Runnable() {
@Override
public void run() {
if (DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id] == null ||
!DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].getConnState()) {
mHandler.obtainMessage(CONN_PRINTER).sendToTarget();
return;
}
if (DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].getCurrentPrinterCommand() == PrinterCommand.TSC) {
sendLabel();
} else {
mHandler.obtainMessage(PRINTER_COMMAND_ERROR).sendToTarget();
}
}
});
}
void sendLabel() {
LabelCommand tsc = new LabelCommand();
/* 设置标签尺寸,按照实际尺寸设置 */
tsc.addSize(40, 30);
/* 设置标签间隙,按照实际尺寸设置,如果为无间隙纸则设置为0 */
tsc.addGap(2);
/* 设置打印方向 */
tsc.addDirection(LabelCommand.DIRECTION.FORWARD, LabelCommand.MIRROR.NORMAL);
/* 设置原点坐标 */
tsc.addReference(0, 0);
/* 撕纸模式开启 */
tsc.addTear(EscCommand.ENABLE.ON);
/* 清除打印缓冲区 */
tsc.addCls();
// 绘制简体中文
tsc.addText(10, 20, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
"标准科室名称:药剂科");
tsc.addText(10, 50, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
"标准科室代码:wsd8965325");
tsc.addText(10, 80, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
"实际科室名称:药剂科");
tsc.addText(10, 110, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
"重量" + tv_weight.getText().toString().trim());
tsc.addQRCode(150, 150, LabelCommand.EEC.LEVEL_L, 4, LabelCommand.ROTATION.ROTATION_0, "12345ss67");
/* 打印标签 */
tsc.addPrint(1, 1);
/* 打印标签后 蜂鸣器响 */
tsc.addSound(1, 100);
tsc.addCashdrwer(LabelCommand.FOOT.F5, 255, 255);
Vector datas = tsc.getCommand();
/* 发送数据 */
if (DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id] == null) {
Log.d(TAG, "sendLabel: 打印机为空");
return;
}
DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].sendDataImmediately(datas);
}
效果图想象一下超时小票的那种就可以了(,,ԾㅂԾ,,)或者如上图(汉印)效果
完整的demo有需要的话可以留下你的邮箱哟~