蓝牙便携式打印机的种类繁多,支持的打印格式也不尽相同。按照指令集可划分为:ESC指令集、CPCL指令集、TSC指令集,实现原理基本相同,我这里以找了个公司的便携式打印机为例,下面是演示流程。先蓝牙搜索配对,发送打印指令,指令结果反馈。
目录
一、检测蓝牙是否开启,然后搜索
二、连接蓝牙打印机
三、开启线程,发送打印指令
四、打印指令结果反馈
五、便携式打印机操作指令
完整代码地址在最后:
代码已实现,先上打印效果图:
![](https://img-blog.csdnimg.cn/479ff33fb0cc44c899511aaa78e288ca.png)
一、检测蓝牙是否开启,然后搜索
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 |