更新记录

1.0.0(2026-08-13)

首版:轻量 CPCL 面单打印。Android 支持 BLE、经典蓝牙 SPP 与局域网 TCP;iOS 支持 BLE 与局域网;鸿蒙支持局域网;微信小程序支持 BLE。提供 printJob 任务打印(文本、条码、二维码、线条、方框、EG 单色位图)与 sendRaw 原始字节发送;支持 dryRun 预览指令字节。默认 DPI 203,面单尺寸毫米、元素坐标点(dot)。


平台兼容性

uni-app(4.11)

Vue2 Vue3 Vue3插件版本 Chrome Safari app-vue app-vue插件版本 app-nvue app-nvue插件版本 Android Android插件版本 iOS iOS插件版本 鸿蒙 鸿蒙插件版本
× 1.0.0 × × 1.0.0 1.0.0 5.0 1.0.0 12 1.0.0 12 1.0.0
微信小程序 微信小程序插件版本 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 小红书小程序 快应用-华为 快应用-联盟
2.22.0 1.0.0 × × × × × × × × - × ×

uni-app x(4.11)

Chrome Safari Android Android插件版本 iOS iOS插件版本 鸿蒙 微信小程序 微信小程序插件版本
× × 5.0 1.0.0 12 1.0.0 × 2.22.0 1.0.0

breao-cpclprint 使用说明

轻量 CPCL 面单打印插件,适用于快递面单、仓储物流等 CPCL 热敏打印机。

当前版本:1.0.0

  • Android:BLE、经典蓝牙 SPP、局域网 TCP(默认 9100)
  • iOS:BLE + 局域网
  • 鸿蒙:仅局域网
  • 微信小程序:仅 BLE
  • 能力:printJob 任务打印(文本/条码/二维码/线/框/单色位图 EG)、sendRaw 原始字节、dryRun 预览
  • 不做 ZPL/TSPL 同包、不做品牌官方 SDK 全家桶、不做固件升级、不做云模板

建议调用顺序:init → 扫描/连接 → printJobsendRawdisconnect / destroy。各异步方法均支持 success / fail / completefailerrCode / errMsg。插件为单例连接语义:再次 init 会销毁上一实例。

坐标说明: 面单 widthMm / heightMm毫米;元素 x / y 等为点(dot)。换算:dots = Math.round(mm × dpi ÷ 25.4),默认 dpi203(会话头分辨率 200);亦可选 300image.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)、微信小程序
  • 不支持: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,
  getConnectionState,
  buildPrintJobBytes,
  mmToDots,
  dpiToRes,
  buildCpclSession,
  buildCpclPageWidth,
  buildCpclPrint,
} from '@/uni_modules/breao-cpclprint'

3. API

3.1 init / destroy

init({
  transport: 'lan', // ble | spp(仅 Android) | lan
  dpi: 203,
  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) {},
})

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 时不发送,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() {} })

详见 §4 元素字段;image.path 在 App 端可读本地文件时自动转 base64。

3.5 状态与断开

getConnectionState({
  success(res) {
    // res.state: disconnected | connecting | connected
  },
  fail(err) {},
})

disconnect({ success() {}, fail() {} })

3.6 协议辅助(同步)

函数 说明
mmToDots 毫米转 dot:round(mm * dpi / 25.4)
dpiToRes 203→200、300→300(写入会话头)
buildCpclSession / buildCpclPageWidth / buildCpclForm / buildCpclPrint 会话与页宽指令
buildCpclText / buildCpclBarcode / buildCpclQrcode / buildCpclLine / buildCpclBox 元素级指令
buildPrintJobBytes 组装完整任务(与 printJob 相同逻辑)
DEFAULT_LAN_PORT 常量 9100
DEFAULT_DPI 常量 203

4. 可配置项

字段 位置 默认 说明
transport init ble ble / spp(Android) / lan
debug init false 调试日志
dpi init 203 203 或 300;影响 mm 换算与会话分辨率
bleChunkSize init 端默认 BLE 分片字节数;SPP/LAN 忽略
bleWriteInterval init 端默认 BLE 写间隔毫秒
bleServiceUuid init BLE Service UUID;空则宽松匹配
bleCharacteristicUuid init BLE 写特征 UUID;空则宽松匹配
lanPort init 9100 局域网默认端口
onDisconnected init 被动断开回调
timeout startBluetoothScan 10000 扫描超时毫秒
includeBonded startBluetoothScan true 合并已配对设备
nameHints startBluetoothScan 内置关键字 CPCL、HM-、Label、Printer 等
address connectBluetooth 必填 MAC 或 BLE deviceId
host connectLan 必填 打印机 IP
port connectLan init.lanPort 或 9100 TCP 端口
widthMm / heightMm printJob 必填 面单宽高(毫米)
gapMm printJob 2 保留字段,CPCL 忽略
density / speed printJob 保留字段,CPCL 忽略
copies printJob 1 会话头打印份数
elements printJob 必填 见下表
dryRun printJob / sendRaw false 仅预览字节,不发送
data sendRaw 必填 原始 CPCL 字节 ArrayBuffer

4.1 transport 与端能力

transport Android iOS 鸿蒙 微信
ble 支持 支持 不支持(9120009) 支持
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 局域网 TCP
微信 openBluetoothAdapter、startBluetoothDevicesDiscovery、createBLEConnection、getBLEDeviceServices、getBLEDeviceCharacteristics、writeBLECharacteristicValue BLE 扫描连接写入

7. 错误码(912)

含义
9120001 成功
9120002 失败(忙碌、连接失败、发送失败等)
9120003 未 init
9120004 蓝牙不可用
9120005 未连接打印机
9120006 参数非法(含 image.path 读文件失败)
9120007 当前平台不支持
9120008 驱动创建失败
9120009 当前端不支持该 transport
9120010 尚未实现

8. 平台注意

  • 局域网面单机多数默认端口 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
  • iOS / 微信 BLE 建议先扫描再连接,以便缓存外设与可写特征
  • Android 部分老款机仅支持 SPP,请 transport: 'spp';BLE 机用 ble
  • dryRun: true 可用于联调指令,无需连接打印机
  • 不同固件对字体编号、条码类型关键字可能有差异,可用 sendRaw 兜底

隐私、权限声明

1. 本插件需要申请的系统权限列表:

Android: android.permission.BLUETOOTH、android.permission.BLUETOOTH_ADMIN(API≤30); android.permission.BLUETOOTH_SCAN、android.permission.BLUETOOTH_CONNECT(Android 12+); android.permission.ACCESS_FINE_LOCATION、android.permission.ACCESS_COARSE_LOCATION(蓝牙扫描辅助); android.permission.INTERNET、android.permission.ACCESS_NETWORK_STATE、android.permission.ACCESS_WIFI_STATE(局域网 TCP 打印)。 iOS: NSBluetoothAlwaysUsageDescription、NSBluetoothPeripheralUsageDescription、NSLocalNetworkUsageDescription。 鸿蒙: ohos.permission.INTERNET。 微信小程序: 蓝牙能力(openBluetoothAdapter、startBluetoothDevicesDiscovery、createBLEConnection、getBLEDeviceServices、getBLEDeviceCharacteristics、writeBLECharacteristicValue;需用户授权蓝牙)。 用途:CPCL 面单打印机扫描、连接与打印数据传输。

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

不采集、不上传任何数据;打印内容仅经本机蓝牙或局域网发至已连接的面单打印机。协议实现仅用于本地设备通信

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

暂无用户评论。