前言
由于自己工作原因,涉及到热敏打印机开发,因此自己写了一套热敏打印机SDK,目前暂时只支持蓝牙打印
我生成了jar包的形式,方便大家使用。里面代码的实现原理现在暂无时间去讲解,主要以如何使用为主。
SDK
源码地址
链接:/875831461/ThomasPrinter
注意事项
目前只适配了蓝牙打印机,58型号80型号的均可以打印
已完成功能
扫描蓝牙
打印一行,打印一行两列,打印图片,打印条形码,二维码(需要打印机支持),倒置打印等。。
未完成功能
标签打印
效果图
步骤
如何导入jar包我就不说了。权限
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /><uses-permission android:name="android.permission.BLUETOOTH" /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
当然了,如果你操作的API大于26,你还需要动态请求此权限,否则你会找不到蓝牙设备
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {if((checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) ){String[] permission = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};requestPermissions(permission, 11);}}
代码
使用此方式可以注册监听/解绑监听打印机的所有状态
ThomasPrinterManage.getInstance().registerOnThomasPrinterChangeListener()ThomasPrinterManage.getInstance().unregisterOnThomasPrinterChangeListener()
连接打印机
1.这种可以在打印机掉线时,如果打印机再次开机会自动连接,但是有时候会有问题
ThomasPrinterManage.getInstance().connectBluetoothDevice(this,address);
2.推荐使用这种方式连接打印机
ThomasPrinterManage.getInstance().connectBluetoothDevice(address);
发送数据
Vector<Byte> data = new Vector<>();ThermalPrinter thermalPrinter = new ThermalPrinter();thermalPrinter.setCommand(data);thermalPrinter.initPrint();// according to your paper sizethermalPrinter.setPageSizeEighty();//thermalPrinter.setPageSizeFiftyEight();thermalPrinter.setWhiteOnBlack();thermalPrinter.setAlignment(Alignment.CENTER);thermalPrinter.setFontSize(FontSize.LARGER);thermalPrinter.setText(getTextByte("White On Black"));thermalPrinter.cancelWhiteOnBlack();thermalPrinter.printAndFeedLine();thermalPrinter.putText(getTextByte("content"), FontSize.NORMAL, Alignment.CENTER,false);thermalPrinter.printAndFeedLine();thermalPrinter.putText(getTextByte("content"), FontSize.NORMAL, Alignment.RIGHT,true);thermalPrinter.printAndFeedLine();thermalPrinter.putText(getTextByte("content"), FontSize.BIG, Alignment.RIGHT,false);thermalPrinter.printAndFeedLine();thermalPrinter.putText(getTextByte("content"), FontSize.LARGER, Alignment.LEFT,false);thermalPrinter.printAndFeedLine();thermalPrinter.setUnderline(true);thermalPrinter.setText(getTextByte("content"));thermalPrinter.setUnderline(false);thermalPrinter.printAndFeedLine();thermalPrinter.putTextColumn(getTextByte("column one"), getTextByte("column two"),FontSize.NORMAL);thermalPrinter.putTextColumn(getTextByte("column one"), getTextByte("column two"),FontSize.NORMAL);thermalPrinter.putTextColumn(getTextByte("column one"), getTextByte("column two"),FontSize.BIG);thermalPrinter.putTextColumn(getTextByte("column one"), getTextByte("column two"),FontSize.NORMAL);// not useful for XML imagesBitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.smart);thermalPrinter.putBitmap(bitmap);thermalPrinter.printBarCode("12345684",Alignment.CENTER,3,80);// this need printer supportthermalPrinter.printQrCode(Alignment.CENTER,3,48,"thomas printer");thermalPrinter.setCutPaper();// if you connect more than one devices you can use this methodThomasPrinterManage.getInstance().writeSendDataAllDevice(data);