更新记录
1.0.0(2026-08-12)
首发正式版。 Android 经典蓝牙 SPP + 局域网 TCP; iOS BLE + 局域网; 鸿蒙局域网; 微信 BLE。 支持 printText/printBytes、对齐/加粗/走纸/切纸; LAN 默认端口 9100; BLE 分片与 UUID、nameHints 可配。 无品牌帧、无 UDP 发现、无条码二维码高阶 API。
平台兼容性
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-escposprint 使用说明
轻量 ESC/POS 热敏打印插件,适用于收银、餐饮小票等通用热敏机场景。
当前版本:1.0.0
- Android / iOS:蓝牙 + 局域网(默认 TCP 9100)
- 鸿蒙:仅局域网
- 微信小程序:仅 BLE
- 能力:文本/字节打印、对齐加粗、走纸切纸、ESC 组帧辅助
- 不做全协议万能机、品牌官方 SDK 合集、USB 直连
建议调用顺序:init → 扫描/连接 → 打印指令 → disconnect / destroy。各异步方法均支持 success / fail / complete;fail 含 errCode / errMsg。插件为单例连接语义:再次 init 会销毁上一实例。
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-escposprint,或从插件市场导入后同步。
import {
init,
destroy,
startBluetoothScan,
stopBluetoothScan,
connectBluetooth,
connectLan,
disconnect,
printText,
printBytes,
setAlign,
setBold,
feed,
cut,
getConnectionState,
buildEscInit,
buildEscAlign,
buildEscCut,
} from '@/uni_modules/breao-escposprint'
3. API
3.1 init / destroy
init({
transport: 'lan', // 或 bluetooth;详见 §4
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,
success(res) {
// res.devices: [{ name, address, rssi?, bonded? }]
},
fail(err) {},
})
stopBluetoothScan({ success() {}, fail() {} })
connectBluetooth({
address: 'XX:XX:XX:XX:XX:XX',
success() {},
fail(err) {},
})
说明:iOS / 微信请先扫描再连接;参数默认值见 §4。
3.3 局域网连接
connectLan({
host: '192.168.1.100',
success() {},
fail(err) {},
})
3.4 打印与便捷指令
printText({ content: 'Hello ESC/POS', success() {}, fail() {} })
printBytes({ data: arrayBuffer, success() {}, fail() {} })
setAlign({ align: 'center', success() {}, fail() {} })
setBold({ bold: true, success() {}, fail() {} })
feed({ lines: 3, success() {}, fail() {} })
cut({ mode: 'partial', success() {}, fail() {} })
3.5 状态与断开
getConnectionState({
success(res) {
// res.state: disconnected | connecting | connected
},
fail(err) {},
})
disconnect({ success() {}, fail() {} })
| 字段 | 说明 |
|---|---|
| state | 连接状态 |
| address | 已连接设备地址或 IP(可能返回) |
| transport | bluetooth 或 lan(可能返回) |
3.6 协议辅助(同步)
可在业务侧自组报文后 printBytes:
| 函数 | 说明 |
|---|---|
| buildEscInit | ESC @ 初始化 |
| buildEscAlign | 对齐 left / center / right |
| buildEscBold | 加粗开关 |
| buildEscFeed | 走纸行数 |
| buildEscCut | 切纸 full / partial |
| buildEscTextAscii | ASCII 文本(中文请自编码) |
| concatEscBytes | 拼接多段 ByteArray |
| DEFAULT_LAN_PORT | 常量 9100 |
4. 可配置项
| 字段 | 位置 | 默认 | 说明 |
|---|---|---|---|
| transport | init | bluetooth | bluetooth 或 lan;鸿蒙仅 lan;微信仅 bluetooth |
| debug | init | false | 调试日志 |
| bleChunkSize | init | 端默认 | BLE 分片字节数;经典蓝牙 SPP 忽略 |
| bleWriteInterval | init | 端默认 | BLE 写间隔毫秒;经典蓝牙 SPP 忽略 |
| bleServiceUuid | init | 空 | BLE Service UUID;空则宽松匹配 |
| bleCharacteristicUuid | init | 空 | BLE Characteristic UUID;空则宽松匹配 |
| lanPort | init | 9100 | 局域网默认端口;可被 connectLan.port 覆盖 |
| encoding | init / printText | gbk | 文本编码提示;端不支持时请用 printBytes |
| onDisconnected | init | 无 | 被动断开回调 |
| timeout | startBluetoothScan | 10000 | 扫描超时毫秒 |
| includeBonded | startBluetoothScan | true | 是否合并系统已配对设备 |
| nameHints | startBluetoothScan | 内置关键字 | 名称过滤;默认含 POS、Printer、TP、BT 等 |
| address | connectBluetooth | 必填 | 设备 MAC 或 BLE deviceId |
| timeout | connectBluetooth | 15000 | 连接超时毫秒 |
| host | connectLan | 必填 | 打印机 IP |
| port | connectLan | init.lanPort 或 9100 | TCP 端口 |
| timeout | connectLan | 10000 | 连接超时毫秒 |
| content | printText | 必填 | 打印文本 |
| encoding | printText | 覆盖 init.encoding | 本次打印编码 |
| newline | printText | true | 打印后是否追加换行 |
| data | printBytes | 必填 | 原始字节 ArrayBuffer |
| align | setAlign | 必填 | left / center / right |
| bold | setBold | 必填 | 是否加粗 |
| lines | feed | 3 | 走纸行数 |
| mode | cut | partial | partial 半切 / full 全切 |
不传参即用默认值。
4.1 transport 与端能力
| transport | Android | iOS | 鸿蒙 | 微信 |
|---|---|---|---|---|
| bluetooth | 支持 | 支持 | 不支持(9060009) | 支持 |
| lan | 支持 | 支持 | 支持 | 不支持(9060009) |
5. 完整示例
5.1 局域网
import {
init,
connectLan,
printText,
setAlign,
feed,
cut,
disconnect,
destroy,
} from '@/uni_modules/breao-escposprint'
init({
transport: 'lan',
success() {
connectLan({
host: '192.168.1.100',
success() {
setAlign({
align: 'center',
success() {
printText({
content: 'Store Receipt',
success() {
feed({
lines: 2,
success() {
cut({
mode: 'partial',
success() {
disconnect({ success() { destroy({}) } })
},
})
},
})
},
})
},
})
},
fail(err) {
console.error(err.errCode, err.errMsg)
},
})
},
})
5.2 蓝牙
import { init, startBluetoothScan, connectBluetooth, printText, destroy } from '@/uni_modules/breao-escposprint'
init({
transport: 'bluetooth',
success() {
startBluetoothScan({
success(res) {
const dev = res.devices && res.devices[0]
if (!dev) return
connectBluetooth({
address: dev.address,
success() {
printText({ content: '蓝牙小票' })
},
})
},
})
},
})
6. 权限
请在应用 manifest / 隐私弹窗中按需声明,并说明用于连接 ESC/POS 热敏打印机与发送打印数据。
| 平台 | 权限 | 说明 |
|---|---|---|
| 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. 错误码(906)
| 码 | 含义 |
|---|---|
| 9060001 | 成功 |
| 9060002 | 失败(忙碌、连接失败、打印失败等) |
| 9060003 | 未 init 或配置缺失 |
| 9060004 | 蓝牙未开启或不可用 |
| 9060005 | 未连接打印机 |
| 9060006 | 参数非法 |
| 9060007 | 当前平台不支持 |
| 9060008 | 驱动创建失败或不匹配 |
| 9060009 | 当前端不支持该 transport |
| 9060010 | 能力尚未实现 |
8. 平台注意
- 局域网热敏多数默认端口 9100;若厂商不同请改
lanPort/connectLan.port - 中文小票常见 GBK;若端侧编码不可用,请业务侧转 GBK 后
printBytes - 连接后不会自动发送 ESC @;需要时可
printBytes(buildEscInit()) - iOS / 微信 BLE 请先
startBluetoothScan再connectBluetooth,以便缓存外设 - Android 12+ 蓝牙扫描通常需要定位权限;请在运行时申请并引导用户开启蓝牙
- BLE UUID 留空时自动匹配可写特征;特殊机型请在 init 填写厂商 UUID
- 切换
transport或彻底释放资源时,请destroy后再重新init

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