蓝牙便携式打印机的种类繁多,支持的打印格式也不尽相同。按照指令集可划分为:ESC指令集、CPCL指令集、TSC指令集,实现原理基本相同,我这里以找了个公司的便携式打印机为例,下面是演示流程。先蓝牙搜索配对,发送打印指令,指令结果反馈。
目录
完整代码地址在最后:
代码已实现,先上打印效果图:
一、检测蓝牙是否开启,然后搜索
private void initBluetooth() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
toast("该设备不支持蓝牙");
} else {
//检测蓝牙未开启,主动开启
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
} else {
manager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
//检测定位是否开启
if (EasyPermissions.hasPermissions(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
if ((Build.VERSION.SDK_INT >= 29) && !manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
gpsDialog();
} else {
searchBlueTooth();
}
} else {
gpsDialog();
}
}
}
}
二、连接蓝牙打印机
private void connectBluetooth(String mac) {
showLoading();
//连接打印机
PrinterDevices blueTooth = new PrinterDevices.Build()
.setContext(this)
.setConnMethod(ConnMethod.BLUETOOTH)
.setMacAddress(mac)
.setCommand(Command.CPCL)
.setCallbackListener(this)
.build();
// TODO 连接设备
Printer.connect(blueTooth);
}
三、开启线程,发送打印指令
public void startPrintDate() {
if(mTotal<1){
toast("打印模板数据为空");
return;
}
ThreadPoolManager.getInstance().addTask(new Runnable() {
@Override
public void run() {
try {
if (Printer.getPortManager() == null) {
sendHandlerMsg(WHAT_PRINT_NOT_CONNECTED,0);
return;
}
//****************** 查询状态********************
Command command = Printer.getPortManager().getCommand();
int status = Printer.getPrinterState(command);
if (status != 0) {
//打印机异常状态
Log.e("OkGo-print","---------打印机状态status="+status+"------mIndex="+mIndex);
sendHandlerMsg(WHAT_PRINT_STATUS,status);
return;
}
//Thread.sleep(1500);
//******************调用打印*********************
Vector<Byte> data;
if(mIndex<mTotal){
if(mPrintData.get(mIndex).getType()==1){
//客户联标签
data =PrintContent.getCustomerTag(PrintMainActivity.this,mPrintData.get(mIndex));
}else {
//货物标签
data =PrintContent.getGoodsTag(PrintMainActivity.this, mPrintData.get(mIndex),mIndex,mTotal-1 );
}
boolean result = Printer.getPortManager().writeDataImmediately(data);
//发送结果
Log.e("OkGo-print","---------打印进度mIndex="+mIndex+"-----"+result);
sendHandlerMsg(WHAT_PRINT_RESULT,result?0:1);
}
} catch (Exception e) {
//发送异常
Log.e("OkGo-print","---------打印出现异常");
sendHandlerMsg(WHAT_PRINT_FAIL,0);
}
}
});
}
四、打印指令结果反馈
打印结果:成功或失败,对应错误码
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
//打印机状态
case WHAT_PRINT_STATUS:
if(msg.arg1==-1){//获取状态失败
toast("打印机状态获取失败,请检查打印机是否缺纸或开盖");
}else if(msg.arg1==1){//系统走纸
if(isShowToast){
toast("状态走纸、打印");
isShowToast=false;
}
//startPrintDate();
if(tvPrint!=null){
tvPrint.postDelayed(new Runnable() {
@Override
public void run() {
//出现异常,继续调用打印
startPrintDate();
}
},2000);
}
}else if(msg.arg1==-2){//状态缺纸
toast("状态缺纸");
}else if(msg.arg1==-3){//状态开盖
toast("状态开盖");
}else if(msg.arg1==-4){//状态过热
toast("状态过热");
}else if(msg.arg1==0){//正常
toast("状态正常");
}
break;
//打印结果
case WHAT_PRINT_RESULT:
if(msg.arg1==0){
if(mIndex==mTotal-1){
isShowToast=true;
toast("打印完成");
}else {
//打印下一条
mIndex++;
startPrintDate();
}
// tvPrintNumber.setText("打印进度: "+(mCurrentIndex+1)+"/"+mPrintData.size());
}else {
toast("打印指令发送失败");
}
break;
//打印失败
case WHAT_PRINT_FAIL:
if(msg.arg1==0){
toast("打印失败");
}
break;
case WHAT_PRINT_NOT_CONNECTED:
if(msg.arg1==0){
toast("打印机未连接");
initBluetooth();
}
break;
default:
break;
}
}
};
五、便携式打印机操作指令
public static Vector<Byte> getGoodsTag(Context context, PrintDataBean bean,int index,int total){
CpclCommand cpcl=new CpclCommand();
cpcl.addUserCommand("\r\n");
cpcl.addInitializePrinter(900, 1);
// x 设置标签内存宽度
cpcl.addPagewidth(572);
//TODO
int y=10;
cpcl.addJustification(CpclCommand.ALIGNMENT.LEFT);
cpcl.addSetbold(CpclCommand.BOLD.OFF);
cpcl.addSetmag(0, 0);//放大1-16倍
cpcl.addText(CpclCommand.TEXT_FONT.FONT_7, 4, y, "园区同城配送");
cpcl.addJustification(CpclCommand.ALIGNMENT.CENTER);
cpcl.addSetbold(CpclCommand.BOLD.OFF);
cpcl.addSetmag(0, 0);
cpcl.addText(CpclCommand.TEXT_FONT.FONT_7, 0, y, "服务热线:"+bean.getTelephone());
cpcl.addJustification(CpclCommand.ALIGNMENT.RIGHT);
cpcl.addSetbold(CpclCommand.BOLD.OFF);
cpcl.addSetmag(0, 0);
cpcl.addText(CpclCommand.TEXT_FONT.FONT_7, 4, y, index+"/"+total);
y=y+35;
cpcl.addBox(4,y,576,y+3,3);
//TODO 货位号
cpcl.addJustification(CpclCommand.ALIGNMENT.LEFT);
cpcl.addSetbold(CpclCommand.BOLD.ON);//加粗
cpcl.addSetmag(2, 2);//放大1-16倍
cpcl.addText(CpclCommand.TEXT_FONT.FONT_3, 4, 80, bean.getPlatFormCode());
cpcl.addBox(196,45,199,145,3);
y=y+15;
cpcl.addJustification(CpclCommand.ALIGNMENT.RIGHT);
cpcl.addBarcode(CpclCommand.COMMAND.BARCODE, CpclCommand.CPCLBARCODETYPE.CODE128, 1,
CpclCommand.BARCODERATIO.Point1, 50, 4, y, bean.getCode());
//单号
y=y+50;
cpcl.addJustification(CpclCommand.ALIGNMENT.RIGHT);
cpcl.addSetbold(CpclCommand.BOLD.OFF);
cpcl.addSetmag(0, 0);
cpcl.addText(CpclCommand.TEXT_FONT.FONT_4, 45, y, bean.getCode());
y=y+35;
cpcl.addBox(4,y,576,y+3,3);
//TODO 寄方
y=y+10;
cpcl.addJustification(CpclCommand.ALIGNMENT.LEFT);
cpcl.addSetbold(CpclCommand.BOLD.ON);//加粗
cpcl.addSetmag(0, 0);
cpcl.addText(CpclCommand.TEXT_FONT.FONT_4, 4, y, "寄方:");
//TODO
y=y+38;
cpcl.addJustification(CpclCommand.ALIGNMENT.LEFT);
cpcl.addSetbold(CpclCommand.BOLD.OFF);
cpcl.addSetmag(0, 0);
cpcl.addText(CpclCommand.TEXT_FONT.FONT_4, 4, y, bean.getSendPerson());
//TODO 收方
y=y+45;
cpcl.addJustification(CpclCommand.ALIGNMENT.LEFT);
cpcl.addSetbold(CpclCommand.BOLD.ON);//加粗
cpcl.addSetmag(0, 0);
cpcl.addText(CpclCommand.TEXT_FONT.FONT_4, 4, y, "收方:");
//TODO 收方姓名+公司名称------------
y=y+40;
cpcl.addJustification(CpclCommand.ALIGNMENT.LEFT);
cpcl.addSetbold(CpclCommand.BOLD.OFF);//正常
cpcl.addSetmag(0, 0);//放大1-16倍
cpcl.addText(CpclCommand.TEXT_FONT.FONT_4, 4, y, bean.getReceivePerson());
//TODO 收方地址
y=y+40;
cpcl.addJustification(CpclCommand.ALIGNMENT.LEFT);
cpcl.addSetbold(CpclCommand.BOLD.OFF);
cpcl.addSetmag(0, 0);
cpcl.addText(CpclCommand.TEXT_FONT.FONT_5, 4, y, bean.getToAddress());
//划线4
y=y+35;
cpcl.addBox(4,y,576,y+3,3);
y=y+10;
//TODO 增值服务
String service= bean.getAddValueServiceTypeTag();
if(!TextUtils.isEmpty(service)){
cpcl.addJustification(CpclCommand.ALIGNMENT.LEFT);
cpcl.addSetbold(CpclCommand.BOLD.OFF);
cpcl.addSetmag(0, 0);
cpcl.addText(CpclCommand.TEXT_FONT.FONT_4, 6,y, "增值服务:"+service);
y=y+38;
}
cpcl.addPrint();
Vector<Byte> data = cpcl.getCommand();
return data;
}
各位看官,麻烦点个赞,感谢 !!!