更新记录
1.3.7(2026-09-07)
- 优化 uni-app x:入口将对象字面量归一化为 Option,业务页可直接传对象调用 API,无需再写
as XxxOption - 使用须制作自定义基座,并走云端传统打包(不支持离线打包、安心打包)
- 不传参行为与上一版兼容
1.3.6(2026-09-06)
- 修复协议 EG/状态解析/Hex:ByteArray.size 与下标 Number.from
- 修复协议 concat:ByteArray.size 先 Number.from 再拼接
- 修复 App 端成功回调与驱动稳定性:去掉 undefined、可变属性强制解包改为局部拷贝;iOS BLE 状态/特性用 Number.from
- 修复 Android BLE 分片打印:ByteArray.size 先 Number.from 再与 offset 比较
- 修复 Android 运行时 ClassCastException:成功回调结果改为 UTSJSONObject 别名,避免强转为独立 SuccessResult 类型
- 修复 invoke 回调判空调用;可选 number 去掉 undefined 联合;部分 Android 原生返回值改 Number.from、可变属性局部拷贝
- 使用须制作自定义基座,并走云端传统打包(不支持离线打包、安心打包)
- 【务必使用此版本及以上版本打包使用,低版本存在缺陷】
1.3.4(2026-09-04)
- 修复 Android 云打包:可选 number 先 Number.from 再比较返回,避免 compareTo / expected Number actual Any
- 使用须制作自定义基座,并走云端传统打包(不支持离线打包、安心打包)
- 不传参行为与上一版兼容
- 【务必使用此版本及以上版本打包使用,低版本存在缺陷】
平台兼容性
uni-app(4.11)
| Vue2 | Vue3 | Vue3插件版本 | Chrome | Safari | app-vue | app-vue插件版本 | app-nvue | app-nvue插件版本 | Android | Android插件版本 | iOS | iOS插件版本 | 鸿蒙 | 鸿蒙插件版本 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| × | √ | 1.3.6 | × | × | √ | 1.3.6 | √ | 1.3.6 | 5.0 | 1.3.6 | 12 | 1.3.6 | 12 | 1.3.6 |
| 微信小程序 | 微信小程序插件版本 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 小红书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2.22.0 | 1.3.6 | × | × | × | × | × | × | × | × | - | × | × |
uni-app x(4.11)
| Chrome | Safari | Android | Android插件版本 | iOS | iOS插件版本 | 鸿蒙 | 鸿蒙插件版本 | 微信小程序 | 微信小程序插件版本 |
|---|---|---|---|---|---|---|---|---|---|
| × | × | 5.0 | 1.3.6 | 12 | 1.3.6 | 12 | 1.3.6 | 2.22.0 | 1.3.6 |
breao-cpclprint 使用说明
轻量 CPCL 面单打印插件,适用于快递面单、仓储物流等 CPCL 热敏打印机。
当前版本:1.3.7
- Android:BLE、经典蓝牙 SPP、局域网 TCP(默认 9100)
- iOS:BLE + 局域网
- 鸿蒙:BLE + 局域网
- 微信小程序:仅 BLE
- 能力:
printJob/sendRaw/getPrinterStatus/getBondedDevices;encoding(GBK/UTF-8);短打印队列与 busy;dryRun免 init - 不做 ZPL/TSPL 同包、不做品牌官方 SDK 全家桶、不做固件升级、不做云模板
建议调用顺序:init → 扫描/连接 → printJob / sendRaw / getPrinterStatus → disconnect / destroy。各异步方法均支持 success / fail / complete;fail 含 errCode / errMsg。插件为单例连接语义:再次 init 会销毁上一实例。连续 printJob/sendRaw 进入短队列;队列执行中 getConnectionState 可能返回 busy。dryRun: true 无需 init/连接。
坐标说明: 面单 widthMm / heightMm 为毫米;元素 x / y 等为点(dot)。换算:dots = Math.round(mm × dpi ÷ 25.4),默认 dpi 为 203(会话头分辨率 200);亦可选 300。image.width 为 EG 字节宽,image.height 为行数。
1. 环境要求
- HBuilderX 4.11 及以上
- uni-app Vue3(App-vue / App-nvue)或 uni-app x(App;微信能力与 uni-app 一致)
- 支持:App-Android、App-iOS、App-鸿蒙、微信小程序;uni-app x 含鸿蒙
- 不支持:H5 及其它小程序
- Android 最低 API:21;iOS 最低 12;鸿蒙最低 API:12;微信小程序基础库建议 2.22.0 及以上
- App 真机调试须制作自定义基座;正式发版须购买授权后走云端传统打包
- 授权绑定唯一 appid + 包名
2. 安装与引入
将插件目录放入工程的 uni_modules/breao-cpclprint,或从插件市场导入后同步。
import {
init,
destroy,
startBluetoothScan,
stopBluetoothScan,
connectBluetooth,
connectLan,
disconnect,
printJob,
sendRaw,
getPrinterStatus,
getBondedDevices,
getConnectionState,
buildPrintJobBytes,
mmToDots,
dpiToRes,
buildCpclSession,
buildCpclPageWidth,
buildCpclText,
buildCpclBarcode,
buildCpclQrcode,
buildCpclLine,
buildCpclBox,
buildCpclEg,
buildCpclForm,
buildCpclPrint,
buildCpclStatusQuery,
parseCpclStatusResponse,
normalizeEncoding,
} from '@/uni_modules/breao-cpclprint'
3. API
3.1 init / destroy
init({
transport: 'lan', // ble | spp(仅 Android) | lan
dpi: 203,
encoding: 'gbk', // gbk | utf-8
debug: false,
onDisconnected(res) {
console.log('disconnected', res)
},
success() {},
fail(err) {
console.error(err.errCode, err.errMsg)
},
})
destroy({ success() {}, fail() {} })
3.2 蓝牙扫描与连接
startBluetoothScan({
timeout: 10000,
nameHints: ['CPCL', 'HM-', 'Label'],
success(res) {
// res.devices: [{ name, address, rssi?, bonded? }]
},
fail(err) {},
})
getBondedDevices({ success(res) {}, fail(err) {} }) // Android / 鸿蒙已配对;其它端返回不支持
connectBluetooth({
address: 'XX:XX:XX:XX:XX:XX',
success() {},
fail(err) {},
})
3.3 局域网连接
connectLan({
host: '192.168.1.100',
port: 9100,
success() {},
fail(err) {},
})
3.4 printJob / sendRaw
printJob({
widthMm: 76,
heightMm: 130,
dpi: 203,
copies: 1,
dryRun: false, // true:无需 init/连接,success 返回 length / hex / bytes
elements: [
{ type: 'text', x: 30, y: 40, content: '收件人', font: '4', xMul: 0, rotation: 0 },
{ type: 'barcode', x: 30, y: 100, content: 'SF1234567890', height: 80, codeType: '128' },
{ type: 'qrcode', x: 400, y: 40, content: 'https://example.com', cellWidth: 6 },
{ type: 'box', x: 20, y: 20, x2: 560, y2: 900, thickness: 2 },
{ type: 'line', x: 20, y: 200, x2: 560, y2: 200, thickness: 2 },
{ type: 'image', x: 0, y: 0, width: 48, height: 40, base64: '<单色行数据 base64>' },
],
success(res) {},
fail(err) {},
})
sendRaw({ data: arrayBuffer, dryRun: false, success() {}, fail() {} })
// dryRun: true 时 success 同样返回 length(字节数)、hex(十六进制预览)、bytes(ArrayBuffer)
详见 §4 元素字段;image.path 在 App 端可读本地文件时自动转 base64。
3.5 状态与断开
getPrinterStatus({
success(res) {
// res.online: 是否已连接
// res.rawStatus: 能回读解析则为值,否则 -1;探测仍下发
// res.transport / res.address
},
fail(err) {},
})
getConnectionState({
success(res) {
// res.state: disconnected | connecting | connected | busy
// res.busy
},
fail(err) {},
})
disconnect({ success() {}, fail() {} })
getPrinterStatus 以连接态为主:online === (state === 'connected')。已连接时会下发探测指令 ! U1 getvar "power.percent"\r\n(Zebra/兼容常见电量查询)。多数廉价 CPCL 机无可靠回读或忽略该指令;本插件不解析回包,故 rawStatus 恒为 -1。边界:仅保证「已连接则尝试探测」;电量/纸仓等细粒度状态请用 sendRaw 按机型手册自行查询。
3.6 协议辅助(同步)
| 函数 | 说明 |
|---|---|
| mmToDots | 毫米转 dot:round(mm * dpi / 25.4) |
| dpiToRes | 203→200、300→300(写入会话头) |
| buildCpclSession / PageWidth / Form / Print | 会话与页宽指令 |
| buildCpclText / Barcode / Qrcode / Line / Box / Eg | 元素与位图指令 |
| buildCpclStatusQuery / parseCpclStatusResponse | 状态探测与回读解析 |
| buildPrintJobBytes | 组装完整任务(与 printJob 相同逻辑) |
| normalizeEncoding | 规范化 encoding 别名:空→gbk;utf8/utf-8→utf-8;gb2312/gbk→gbk |
| DEFAULT_LAN_PORT | 常量 9100 |
| DEFAULT_DPI | 常量 203 |
4. 可配置项
| 字段 | 位置 | 默认 | 说明 |
|---|---|---|---|
| transport | init | ble | ble / spp(Android) / lan |
| debug | init | false | 调试日志 |
| dpi | init | 203 | 203 或 300;影响 mm 换算与会话分辨率 |
| encoding | init / printJob | gbk | 文本编码提示;utf-8 可选;端不支持请用 sendRaw |
| bleChunkSize | init | iOS 180 / 微信 20 / Android BLE 20 | BLE 分片字节数;SPP/LAN 忽略 |
| bleWriteInterval | init | iOS 25 / 微信 20 | BLE 写间隔毫秒;传 0 尽量无延迟;SPP/LAN 忽略 |
| bleServiceUuid | init | 空 | BLE Service UUID;空则宽松匹配;SPP/LAN 忽略 |
| bleCharacteristicUuid | init | 空 | BLE 写特征 UUID;空则宽松匹配;SPP/LAN 忽略 |
| lanPort | init | 9100 | 局域网默认端口 |
| onDisconnected | init | 无 | 被动断开回调 |
| timeout | startBluetoothScan | 10000 | 扫描超时毫秒 |
| includeBonded | startBluetoothScan | true | 合并已配对设备 |
| nameHints | startBluetoothScan | 内置关键字 | CPCL、HM-、Label、Printer 等 |
| address | connectBluetooth | 必填 | MAC 或 BLE deviceId |
| timeout | connectBluetooth | 15000 | 蓝牙/SPP 连接超时毫秒 |
| host | connectLan | 必填 | 打印机 IP |
| port | connectLan | init.lanPort 或 9100 | TCP 端口 |
| timeout | connectLan | 10000 | 局域网 TCP 连接超时毫秒 |
| widthMm / heightMm | printJob | 必填 | 面单宽高(毫米) |
| gapMm | printJob | 2 | 保留字段,CPCL 忽略 |
| density / speed | printJob | 无 | 保留字段,CPCL 忽略 |
| copies | printJob | 1 | 会话头打印份数 |
| elements | printJob | 必填 | 见下表 |
| dryRun | printJob / sendRaw | false | true 时无需 init/连接;success 返回 length、hex、bytes |
| data | sendRaw | 必填 | 原始 CPCL 字节 ArrayBuffer |
不传参即用默认值。Android 经典 SPP 忽略全部 BLE 间隔/UUID/分片参数。
4.1 transport 与端能力
| transport | Android | iOS | 鸿蒙 | 微信 |
|---|---|---|---|---|
| ble | 支持 | 支持 | 支持 | 支持 |
| spp | 支持 | 不支持(9120009) | 不支持(9120009) | 不支持(9120009) |
| lan | 支持 | 支持 | 支持 | 不支持(9120007) |
4.2 printJob 元素字段(坐标均为 dot)
| type | 常用字段 |
|---|---|
| text | x, y, content, font(默认 "4"), xMul(映射 size,默认 0), rotation(0/90/180/270) |
| barcode | x, y, content, codeType(默认 128), height, thickness(窄条), width(宽窄比) |
| qrcode | x, y, content, cellWidth(单元尺寸,默认 6) |
| line | x, y, x2, y2, thickness |
| box | x, y, x2, y2 必填, thickness |
| image | x, y, width(字节宽), height(行数), base64 或 path(App) |
5. 完整示例
5.1 局域网打印面单
import { init, connectLan, printJob, disconnect, destroy } from '@/uni_modules/breao-cpclprint'
init({
transport: 'lan',
success() {
connectLan({
host: '192.168.1.100',
success() {
printJob({
widthMm: 76,
heightMm: 130,
elements: [
{ type: 'text', x: 30, y: 40, content: '收件人:张三' },
{ type: 'barcode', x: 30, y: 100, content: 'SF1234567890', height: 70 },
],
success() {
disconnect({ success() { destroy({}) } })
},
})
},
})
},
})
5.2 Android 经典蓝牙 SPP
import { init, startBluetoothScan, connectBluetooth, printJob } from '@/uni_modules/breao-cpclprint'
init({
transport: 'spp',
success() {
startBluetoothScan({
success(res) {
const dev = res.devices && res.devices[0]
if (!dev) return
connectBluetooth({
address: dev.address,
success() {
printJob({
widthMm: 76,
heightMm: 130,
dryRun: true,
elements: [{ type: 'text', x: 30, y: 40, content: 'Preview' }],
success(res) {
console.log(res.hex)
},
})
},
})
},
})
},
})
6. 权限
请在应用 manifest / 隐私弹窗中按需声明,并说明用于连接 CPCL 面单打印机与发送打印数据。
| 平台 | 权限 | 说明 |
|---|---|---|
| Android | android.permission.BLUETOOTH、android.permission.BLUETOOTH_ADMIN | API≤30 蓝牙 |
| Android | android.permission.BLUETOOTH_SCAN、android.permission.BLUETOOTH_CONNECT | Android 12+ 蓝牙 |
| Android | android.permission.ACCESS_FINE_LOCATION、android.permission.ACCESS_COARSE_LOCATION | 蓝牙扫描辅助 |
| Android | android.permission.INTERNET、android.permission.ACCESS_NETWORK_STATE、android.permission.ACCESS_WIFI_STATE | 局域网 TCP |
| iOS | NSBluetoothAlwaysUsageDescription、NSBluetoothPeripheralUsageDescription | 蓝牙打印 |
| iOS | NSLocalNetworkUsageDescription | 局域网打印机 |
| 鸿蒙 | ohos.permission.INTERNET、ACCESS_BLUETOOTH、DISCOVER_BLUETOOTH、MANAGE_BLUETOOTH | 局域网与蓝牙 |
| 微信 | openBluetoothAdapter、startBluetoothDevicesDiscovery、createBLEConnection、getBLEDeviceServices、getBLEDeviceCharacteristics、writeBLECharacteristicValue | BLE 扫描连接写入 |
7. 错误码(912)
| 码 | 含义 |
|---|---|
| 9120001 | 成功 |
| 9120002 | 失败(忙碌、连接失败、发送失败等) |
| 9120003 | 未 init |
| 9120004 | 蓝牙不可用 |
| 9120005 | 未连接打印机 |
| 9120006 | 参数非法(含 image.path 读文件失败) |
| 9120007 | 当前平台不支持 |
| 9120008 | 驱动创建失败 |
| 9120009 | 当前端不支持该 transport |
| 9120010 | 尚未实现 |
8. 平台注意
- uni-app x(
.uvue):1.3.7 起可直接传对象字面量调用 API,无需as XxxOption;须使用 1.3.7+ 并重打自定义基座 - 局域网面单机多数默认端口 9100;厂商不同请改
lanPort/connectLan.port - 毫米与 dot 勿混用:
widthMm等为毫米;elements[].x/y等为 dot,可用mmToDots换算 image为 CPCL EG 单色行数据 base64;完整 PNG 解码需业务侧预处理;width为字节宽、height为行数- 文本
rotation为 90/180/270 时分别生成 T90/T180/T270 - 请先扫描再连接(未扫描直接 connect 会返回中文错误);iOS 须用扫描返回的 UUID
- Android 部分老款机仅支持 SPP,请
transport: 'spp';BLE 机用ble getPrinterStatus的online即连接态;探测指令为 U1 getvar,廉价机可能无响应;细粒度状态请sendRawdryRun: true无需 init/连接即可预览 length/hex/bytes- 元素校验:越界坐标、缺 x2/y2、条码高度等会直接 fail,少打废纸
- 不同固件对字体编号、条码类型关键字可能有差异,可用
sendRaw兜底 - 短打印队列深度为 32;
printJob/sendRaw串行入队 - 队列已满时
fail返回 9120002,errMsg为「打印队列已满,请稍后重试」 init(含再次 init)、disconnect、destroy会清空尚未执行的队列任务

收藏人数:
购买普通授权版(
试用
赞赏(0)
下载 26
赞赏 0
下载 12575273
赞赏 1949
赞赏
京公网安备:11010802035340号