cordova-plugin-jbprint-jsg
v2.0.0
Published
佳博/JB 标签&小票打印机 Cordova 插件,支持 WIFI/蓝牙(经典+BLE)/USB/串口、TSC/ESC/CPCL 指令
Readme
cordova-plugin-jbprint-jsg
佳博 / JB 标签 & 小票打印机 Cordova 插件,封装官方 TSC SDK V3.2,支持:
- 连接方式:WIFI / 蓝牙(经典 SPP)/ 蓝牙 BLE / USB / 串口
- 指令集:TSC(标签)/ ESC(小票)/ CPCL(面单)
- 蓝牙扫描(含 RSSI、配对状态)
- 状态查询、电量、型号、连接事件订阅
- 打印文本 / Bitmap(base64)/ PDF
- 支持原始字节流(int[]、Base64、CSV)
安装
cordova plugin add cordova-plugin-jbprint-jsg快速开始
// 1. 初始化 + 申请权限
JbPrintService.init();
await JbPrintService.requestPermissions();
// 2. 订阅连接事件
JbPrintService.registerConnectionListener(function (e) {
// e.event: connecting | checking | connected | receive | failure | disconnect
console.log('printer event', e);
});
// 3. WIFI 连接
await JbPrintService.connect({ method: 'wifi', ip: '192.168.1.100', port: 9100, command: 'TSC' });
// 4. 打印
await JbPrintService.testPrint();
await JbPrintService.printText({ text: '欢迎使用打印机', x: 30, y: 20, fontType: 3 });API
初始化 / 权限
| API | 说明 |
| --- | --- |
| init() | 初始化插件 |
| requestPermissions() | 一次性申请运行时权限(位置、蓝牙、外部存储) |
蓝牙
| API | 返回 / 事件 |
| --- | --- |
| isBluetoothSupported() | boolean |
| isBluetoothEnabled() | boolean |
| enableBluetooth() | 弹系统开关对话框,确认即 resolve |
| getBondedDevices() | [{name,mac,bonded}] |
| startBluetoothScan(onEvent) | 持续推送:{event:'started'}、{event:'found',device:{name,mac,rssi,bonded}}、{event:'finished'}、{event:'stateChanged',state} |
| stopBluetoothScan() | 停止扫描 |
JbPrintService.startBluetoothScan(function (evt) {
if (evt.event === 'found') {
console.log(evt.device.name, evt.device.mac, evt.device.rssi);
}
});USB
| API | 返回 |
| --- | --- |
| listUsbDevices() | [{name, vendorId, productId, deviceId, manufacturer?, product?}] |
连接
// 通用:
JbPrintService.connect({
method: 'wifi' | 'bluetooth' | 'ble' | 'usb' | 'serial',
command: 'TSC' | 'ESC' | 'CPCL',
// wifi
ip, port,
// bluetooth / ble
mac, ble,
// usb
name,
// serial
path, baudrate,
});
// 兼容旧接口:
JbPrintService.wifiConnect(ip, ok, err);
JbPrintService.bluetoothConnect(mac);
JbPrintService.bleConnect(mac);
JbPrintService.usbConnect(name);
JbPrintService.serialConnect(path, baudrate);
JbPrintService.disconnect();
JbPrintService.isConnected(); // → boolean
JbPrintService.registerConnectionListener(onEvent);状态 / 元信息
| API | 返回 |
| --- | --- |
| getStatus() | 见下表 |
| getPower() | 0~3 (3=高 0=无) |
| getPrinterModel() | 型号字符串 |
| setCommand('TSC'\|'ESC'\|'CPCL') | — |
| getCurrentCommand() | 'TSC' 等 |
getStatus() 返回值:
| 值 | 含义 | | --- | --- | | 0 | 状态正常 | | 1 | 走纸 / 打印中 | | -1 | 获取状态失败(缺纸或开盖等) | | -2 | 缺纸 | | -3 | 开盖 | | -4 | 过热 |
数据发送
// 字符串 → GB18030 字节数组(处理中文)
JbPrintService.getCommand('中文', function (bytes) { /* number[] */ });
// 推荐 / 现代用法:
await JbPrintService.sendBytes([0x1B, 0x40, ...]);
await JbPrintService.sendBase64(base64String);
// 旧接口(CSV):
JbPrintService.sendCommand([27, 64, 10], ok, err);高级打印
// TSC 标签 — 文本
await JbPrintService.printText({
text: 'Hello 世界',
width: 50, height: 30, gap: 2,
x: 30, y: 20,
fontType: 3, // 0..N,参见 LabelCommand.FONTTYPE
rotation: 0, // 0 / 90 / 180 / 270
scaleX: 1, scaleY: 1, // 1..8
copies: 1,
});
// 任意指令集打印 Bitmap(base64,可带 data:URI 前缀)
await JbPrintService.printBitmap({
base64: 'iVBORw0KGgoAAAA...',
command: 'TSC', // 默认使用当前已连接的指令集
x: 0, y: 0, width: 384,
labelWidth: 50, labelHeight: 30, gap: 2,
copies: 1,
});
// PDF 打印(base64 PDF 内容)
await JbPrintService.printPdf({
base64: pdfBase64,
width: 576,
gap: 0, cut: false, compress: true, limit: false, density: 160,
});权限说明
插件已自动声明所需权限。运行时权限通过 requestPermissions() 一次性申请:
- 位置:Android < 12 蓝牙扫描需要
- BLUETOOTH_SCAN / CONNECT:Android 12+ 蓝牙扫描/连接
- READ_EXTERNAL_STORAGE:PDF 打印场景
串口
串口连接依赖打包到插件中的 libserial_port.so(armeabi-v7a / arm64-v8a / armeabi / x86)。无需额外配置。
License
Apache 2.0 — 内含佳博官方 SDK(SDKLib.jar),版权归原作者所有。
