蓝牙打印
打印其实是软件与硬件之间的交互,而这部分恰恰是sdk的职责所在,所以留给我们的任务已经不多了,也就相对简单很多。
1.通过线程池添加打印任务
public void printLabel() { Log.i("TAG", "准备打印"); threadPool = ThreadPool.getInstantiation(); threadPool.addTask(new Runnable() { @Override public void run() { ... } }); }
因为打印是耗时任务,所以不管是为了避免卡顿还是ANR,我们应该用线程池进行优化,为了方便再用单例封装起来。
2.打印之前也要先做蓝牙状态的判断,只有做到足够的严谨,才能看起来万无一失。。
public void printLabel() { Log.i("TAG", "准备打印"); 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; } ... } }); }
这里呢,因为是在子线程,所以通过handler在主线程更新ui或者给个提示
@SuppressLint("HandlerLeak") private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case CONN_STATE_DISCONN: if (DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id] != null || !DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].getConnState()) { DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].closePort(id); Toast.makeText(MainActivity.this, "成功断开连接", Toast.LENGTH_SHORT).show(); } break; case PRINTER_COMMAND_ERROR: Toast.makeText(MainActivity.this, "请选择正确的打印机指令", Toast.LENGTH_SHORT).show(); break; case CONN_PRINTER: Toast.makeText(MainActivity.this, "请先连接打印机", Toast.LENGTH_SHORT).show(); break; } } };
3.一切正常,我们开始走打印流程
public void printLabel() { Log.i("TAG", "准备打印"); 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) { Log.i("TAG", "开始打印"); sendLabel(); } else { mHandler.obtainMessage(PRINTER_COMMAND_ERROR).sendToTarget(); } } }); }
4.设置打印数据,发送打印数据
private void sendLabel() { LabelCommand tsc = new LabelCommand(); tsc.addSize(40, 30); // 设置标签尺寸,按照实际尺寸设置 tsc.addGap(1); // 设置标签间隙,按照实际尺寸设置,如果为无间隙纸则设置为0 tsc.addDirection(LabelCommand.DIRECTION.FORWARD, LabelCommand.MIRROR.NORMAL);// 设置打印方向 tsc.addQueryPrinterStatus(LabelCommand.RESPONSE_MODE.ON);//开启带Response的打印,用于连续打印 tsc.addReference(0, 0);// 设置原点坐标 tsc.addTear(EscCommand.ENABLE.ON); // 撕纸模式开启 tsc.addCls();// 清除打印缓冲区 // 绘制简体中文 tsc.addText(30, 30, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1, "这是标题"); tsc.addText(200, 30, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1, "序号:" + "1"); tsc.addText(30, 90, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1, "价格:" + "99.00"); tsc.addText(30, 140, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1, "数量:" + "99"); tsc.addText(30, 190, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1, "日期:" + "2020-02-02"); // 绘制图片 // Bitmap b = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); // tsc.addBitmap(20, 50, LabelCommand.BITMAP_MODE.OVERWRITE, b.getWidth(), b); //二维码 tsc.addQRCode(200, 90, LabelCommand.EEC.LEVEL_L, 4, LabelCommand.ROTATION.ROTATION_0, "www.baidu.com"); tsc.addPrint(1, 1); // 打印标签 tsc.addSound(2, 100); // 打印标签后 蜂鸣器响 /* 发送数据 */ Vector<Byte> data = tsc.getCommand(); if (DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id] == null) { Log.i("TAG", "sendLabel: 打印机为空"); return; } DeviceConnFactoryManager.getDeviceConnFactoryManagers()[id].sendDataImmediately(data); }
数据及显示的位置,根据自己的需求做调整。
ok,以上基本是核心代码了,注释都有,按照上面这个顺序来走的话思路还是很清晰的
GitHub
https://github.com/yechaoa/PrinterDemo