更新记录
1.0.4(2024-06-05)
- 修复某些android系统监听usb插拔问题
1.0.3(2023-08-11)
- getUsbDeviceList接口返回数据增加vendorId、productId
1.0.2(2023-03-01)
- 修复切纸和自定义指令问题
平台兼容性
Android | Android CPU类型 | iOS |
---|---|---|
适用版本区间:4.4 - 14.0 | armeabi-v7a:未测试,arm64-v8a:未测试,x86:未测试 | × |
原生插件通用使用流程:
- 购买插件,选择该插件绑定的项目。
- 在HBuilderX里找到项目,在manifest的app原生插件配置中勾选模块,如需要填写参数则参考插件作者的文档添加。
- 根据插件作者的提供的文档开发代码,在代码中引用插件,调用插件功能。
- 打包自定义基座,选择插件,得到自定义基座,然后运行时选择自定义基座,进行log输出测试。
- 开发完毕后正式云打包
付费原生插件目前不支持离线打包。
Android 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/android
iOS 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/ios
注意事项:使用HBuilderX2.7.14以下版本,如果同一插件且同一appid下购买并绑定了多个包名,提交云打包界面提示包名绑定不一致时,需要在HBuilderX项目中manifest.json->“App原生插件配置”->”云端插件“列表中删除该插件重新选择
前言
佳博票据、标签、图片打印
var wrsgpsdk = uni.requireNativePlugin("wrs-gpsdk");
- 初始化SDK
wrsgpsdk.init();
- 检查权限
wrsgpsdk.checkPermission();
- 初始化蓝牙
wrsgpsdk.initBluetooth();
- 设置回调
wrsgpsdk.setCallback((resp) => {
var jsonStr = JSON.stringify(resp);
console.log("resp:" + jsonStr);
var opt = resp.opt;
if(opt == 'CONN_STATE_DISCONN') { // 断开连接
} else if(opt == "PRINTER_COMMAND_ERROR") { // 打印机指令错误
} else if(opt == "CONN_PRINTER") { //未连接打印机
} else if(opt == "ACTION_FOUND") { // 扫描到蓝牙
this.bluetoothDevices.push(resp.device);
} else if(opt == "ACTION_DISCOVERY_FINISHED") {
} else if(opt == "ACTION_STATE_CHANGED") {
}
});
- 获取USB列表
var usebList = wrsgpsdk.getUsbDeviceList();
if(isString(usebList)) {
usebList = JSON.parse(usebList);
}
this.usbDevices.push(...usebList.usbs);
- 连接USB设备
var params = {};
params.deviceName = this.connectUsbDevice;
wrsgpsdk.connectUsb(params);
- 开始扫描蓝牙
wrsgpsdk.startScanBluetooth();
- 停止扫描蓝牙
wrsgpsdk.stopScanBluetooth();
- 连接蓝牙
var params = {};
params.address = this.connectBluetoothDevice;
wrsgpsdk.connectBluetooth(params);
- 断开连接
wrsgpsdk.disConnect();
- 打印票据
var image = this.getLocalImage();
var params = {};
params.data = [
{
type: "addPrintAndFeedLines", //打印走纸多少个单位,打印并且走纸多少行,默认为8行,打印完内容后发送
value: 3
},
{
type: "addSelectJustification", //设置打印居中
value: "center"
},
{
type: "addSelectPrintModes", //设置为倍高倍宽
font: "fonta",
emphasized: false,
doublewidth: true,
doubleheight: true,
underline: false
},
{
type: "addText", //打印文字
value: "票据测试\n"
},
{
type: "addPrintAndLineFeed" //打印并换行
},
{
type: "addSelectPrintModes", //设置为倍高倍宽
font: "fonta",
emphasized: false,
doublewidth: false,
doubleheight: false,
underline: false
},
{
type: "addSelectJustification", //设置打印居中
value: "left"
},
{
type: "addText", //打印文字
value: "打印文字测试:\n"
},
{
type: "addText", //打印文字
value: "欢迎使用打印机!\n"
},
{
type: "addPrintAndLineFeed" //打印并换行
},
{
type: "addRastBitImage", //设置打印居中
width: 348,
model: 0,
image: {
type: "url",
value: image
}
},
{
type: "addPrintAndLineFeed" //打印并换行
},
{
type: "addText", //打印文字
value: "打印条码测试:\n"
},
{
type: "addSelectPrintingPositionForHRICharacters",
value: "below"
},
{
type: "addSetBarcodeHeight",
value: 60
},
{
type: "addSetBarcodeWidth",
value: 2
},
{
type: "addCODE128",
value: "barcode128"
},
{
type: "addPrintAndLineFeed" //打印并换行
},
{
type: "addText", //打印文字
value: "打印二维码测试::\n"
},
{
type: "addSelectErrorCorrectionLevelForQRCode", //设置纠错等级
value: 49
},
{
type: "addSelectSizeOfModuleForQRCode", //设置qrcode模块大小
value: 4
},
{
type: "addStoreQRCodeData", //设置qrcode内容
value: "www.smarnet.cc"
},
{
type: "addPrintQRCode" //打印QRCode
},
{
type: "addPrintAndLineFeed" //打印并换行
},
{
type: "addSelectJustification",
value: "center"
},
{
type: "addText", //打印文字
value: "测试完成!\r\n"
},
{
type: "addPrintAndLineFeed" //打印并换行
},
{
type: "addPrintAndFeedLines", //打印走纸n个单位
value: 4
},
{
type: "addGeneratePlus", //开钱箱
foot: "f2",
t1:255,
t2:255
},
{
type: "addCutPaper" //开启切刀
},
{
type: "addUserCommand" ,//添加用户指令
value:"1D7201"
}
];
wrsgpsdk.printReceipt(params);
- 打印标签
var image = this.getLocalImage();
var params = {};
params.data = [
{
type:"addSize", // // 设置标签尺寸宽高,按照实际尺寸设置 单位mm
width: 60,
height: 75
},
{
type: "addGap", // 设置标签间隙,按照实际尺寸设置,如果为无间隙纸则设置为0 单位mm
value: 0
},
{
type: "addDirection", // 设置打印方向
direction: "forward",// forward、backward
mirror: "normal" // mirror、normal
},
{
type: "addQueryPrinterStatus", // // 开启带Response的打印,用于连续打印
value: "on"// on、off、batch
},
{
type: "addReference", // 设置原点坐标
x: 0,
y: 0
},
{
type: "addDensity",//设置浓度
value: "dnesity4"
},
{
type: "addTear", // 撕纸模式开启
value: "on"
},
{
type: "addCls" // 清除打印缓冲区
},
{
type: "addText", // // 绘制简体中文
x: 10,
y: 0,
font: "simplified_chinese",
rotation:"rotation_0",
Xscal: "mul_1",
Yscal: "mul_1",
value: "欢迎使用Printer"
},
{
type: "addUnicodeText", // // 绘制简体中文
x: 10,
y: 32,
font: "traditional_chinese",
rotation:"rotation_0",
Xscal: "mul_1",
Yscal: "mul_1",
value: "BIG5碼繁體中文字元",
textType: "BIG5"
},
{
type: "addUnicodeText", // 打印韩文
x: 10,
y: 60,
font: "korean",
rotation:"rotation_0",
Xscal: "mul_1",
Yscal: "mul_1",
value: "Korean 지아보 하성",
textType: "EUC_KR"
},
{
type: "addBitmap",// 绘制图片
x: 10,
y: 80,
width: 300,
mode:"overwrite",
image:{
type:"url",
value:image
}
},
{
type: "addQRCode",//绘制二维码
x:10,
y:380,
cellWidth: 5,
level: "level_l",
rotation:"rotation_0",
value: "www.smarnet.cc"
},
{
type: "add1DBarcode", // 绘制一维条码
x:10,
y:500,
height:100,
barType: "code128",
readable: "eanbel",
rotation:"rotation_0",
value:"SMARNET"
},
{
type: "addPrint", // 打印标签
m:1,
n: 1
},
{
type: "addSound", // 打印标签后 蜂鸣器响
level: 2,
interval: 100
},
{
type: "addCashdrwer", //开启钱箱
foot: "f5",
t1: 255,
t2: 255
}
];
wrsgpsdk.printLabel(params);
- 简单打印
var image = this.getLocalImage();
var params = {};
params.data = [
{
type: "addInitializePrinter",
height: 1130,
qty: 1
},
{
type: "addJustification",
value: "center"
},
{
type: "addSetmag",
w: 1,
h: 1
},
{
type: "addText",
x:0,
y:30,
value: "Sample",
font: "font_4"
},
{
type: "addEGraphics",
x: 0,
y: 255,
width: 385,
image:{
type: "url",
value:image
}
},
{
type: "addBarcodeText",
font: 5,
offset: 2
},
{
type: "addBarcode",
height: 50,
x: 0,
y:680,
value: "SMARNET",
command: "barcode"
},
{
type: "addPrint"
}
];
wrsgpsdk.printCpcl(params);
- 更改打印模式
var params = {};
params.mode = 0; // 0: CPCL 1:TSC 2:ESC
wrsgpsdk.changeMode(params);