xGprinterPlus

论坛示例

为帮助用户更好更快的使用插件,论坛维护了一个示例,示例中包含示例代码供您参考。

概述

xGprinterPlus装了佳博热敏打印机官方2.2.1版SDK,可通过蓝牙、usb、网口连接打印机,同时支持ESC和TSC打印2中模式打印。

固件要求

Android:4.0及以上

插件接口

connectDevices

连接多设备

connectDevices(callback(ret, err))

callback(ret, err)

ret:

  • 类型:JSON对象
  • 内部字段:
{
    status: true,   //布尔型;true||false
}

err:

  • 类型:JSON对象
  • 内部字段:
{
    msg: ''
}

示例代码

var demo = api.require('xGprinterPlus');
demo.connectDevices(function(ret, err) {
    api.alert({msg: JSON.stringify(ret)});
});

可用性

Android系统

可提供的1.0.5及更高版本

connectBle

蓝牙连接(仅单设备时使用)

connectBle(callback(ret, err))

callback(ret, err)

ret:

  • 类型:JSON对象
  • 内部字段:
{
    status: true,   //布尔型;true||false
}

err:

  • 类型:JSON对象
  • 内部字段:
{
    msg: ''
}

示例代码

var demo = api.require('xGprinterPlus');
demo.connectBle(function(ret, err) {
    api.alert({msg: JSON.stringify(ret)});
});

可用性

Android系统

可提供的1.0.5及更高版本

connectUsb

USB连接(仅单设备时使用)

connectUsb(callback(ret, err))

callback(ret, err)

ret:

  • 类型:JSON对象
  • 内部字段:
{
    status: true,   //布尔型;true||false
}

err:

  • 类型:JSON对象
  • 内部字段:
{
    msg: ''
}

示例代码

var demo = api.require('xGprinterPlus');
demo.connectUsb(function(ret, err) {
    api.alert({msg: JSON.stringify(ret)});
});

可用性

Android系统

可提供的1.0.5及更高版本

disConnect

断开连接(仅单设备时使用)

disConnect(callback(ret, err))

callback(ret, err)

ret:

  • 类型:JSON对象
  • 内部字段:
{
    status: true,   //布尔型;true||false
}

err:

  • 类型:JSON对象
  • 内部字段:
{
    msg: ''
}

示例代码

var demo = api.require('xGprinterPlus');
demo.disConnect(function(ret, err) {
    api.alert({msg: JSON.stringify(ret)});
});

可用性

Android系统

可提供的1.0.5及更高版本

printTest

打印测试页

printTest({params},callback(ret, err))

params

xh:

  • 类型:数字型
  • 描述:(可选项)指定打印机0-3。
  • 默认值:0

callback(ret, err)

ret:

  • 类型:JSON对象
  • 内部字段:
{
    status: true   //布尔型;true||false
}

err:

  • 类型:JSON对象
  • 内部字段:
{
    msg: ''
}

示例代码

var demo = api.require('xGprinterPlus');
demo.printTest(function(ret, err) {
    api.alert({msg: JSON.stringify(ret)});
});

可用性

Android系统

可提供的1.0.5及更高版本

getPrinterStatus

查询打印机状态

getPrinterStatus({params},callback(ret, err))

params

xh:

  • 类型:数字型
  • 描述:(可选项)指定打印机0-3。
  • 默认值:0

callback(ret, err)

ret:

  • 类型:JSON对象
  • 内部字段:
{
    status: true,   //布尔型;true||false
    message : "打印机正常"     // 打印机状态描述
}

err:

  • 类型:JSON对象
  • 内部字段:
{
    msg: ''
}

示例代码

var obj = api.require('xGprinterPlus');
obj.getPrinterStatus(function(ret, err) {
    api.alert({msg: JSON.stringify(ret)});
});

可用性

Android系统

可提供的1.0.5及更高版本

getConnectState

查询打印机连接状态

getConnectState()

##ret:

  • 类型:JSON对象
  • 内部字段:
{
    status: true,   //布尔型;true||false
    state : [false,false,false,false]     // 0-3个打印机连接状态
}

示例代码

var obj = api.require('xGprinterPlus');
var ret = obj.getConnectState();
api.alert({msg: JSON.stringify(ret)});

可用性

Android系统

可提供的1.0.5及更高版本

setPrinterCommandType

切换打印模式

setPrinterCommandType({params},callback(ret, err))

params

xh:

  • 类型:数字型
  • 描述:(可选项)指定打印机0-3。
  • 默认值:0

commandType:

  • 类型:数字型
  • 描述:(必输项)0 : 面单模式 1 :票据模式 2 :标签模式。

callback(ret, err)

ret:

  • 类型:JSON对象
  • 内部字段:
{
    status: true,   //布尔型;true||false
}

err:

  • 类型:JSON对象
  • 内部字段:
{
    msg: ''
}

示例代码

var obj = api.require('xGprinterPlus');
obj.setPrinterCommandType(function(ret, err) {
    api.alert({msg: JSON.stringify(ret)});
});

可用性

Android系统

可提供的1.0.5及更高版本

printReceipt

打印票据(ESC模式打印,确保打印机处于票据模式)

printReceipt({params}, callback(ret, err))

params

xh:

  • 类型:数字型
  • 描述:(可选项)指定打印机0-3。
  • 默认值:0

rows:

  • 类型:数组
  • 描述:(必输项)打印数据。
[{
    lineType: 'lines',//打印空行,要传行数
    data : 0   //要打印的空行行数
},{
    lineType: 'style',   //设置样式
    data : [0,0,0,0,0]    //字体A/B:(0:A|1:B);是否加粗:(0:关闭|1:开启);倍高:(0:关闭|1:开启);倍宽:(0:关闭|1:开启);下划线:(0:关闭|1:开启)
},{
    lineType: 'align',   //设置字体位置
    data : 'center',    //left|center|right
},{
    lineType: 'text',   //设置打印内容
    data : '打印文本'
},{
    lineType: 'lineFeed'   //设置空行,不需要设置行数
},{
    lineType: 'image',  //设置打印图片
    data : 'widget://image/huibo_logo.png'  //支持 fs:// widget:// 路径
},{
    lineType: 'barcodePosition',  //设置条码可识别字符位置
    data: 'below'   //设置打印位置 above|aboveAndBelow|below|noPrint
},{
    lineType: 'barcodeHeight',  //设置条码高度
    data : 60  //条码高度
},{
    lineType: 'barcodeWidth',  //设置条码单元宽度
    data : 1 //宽度为1点
},{
    lineType: 'code128',  //打印Code128码
    data : '123456789'
},{
    lineType: 'levelForQRCode',  //设置纠错等级
    data : 31
},{
    lineType: 'moduleForQRCode',  //设置qrcode插件大小
    data : 3
},{
    lineType: 'qrcode',  //设置qrcode内容
    data : 'qrcode内容' 
},{
    lineType: 'openBox'  //开钱箱
},{
    lineType: 'ImmediatelyOpenBox'  //立即开钱箱
},{
    lineType: 'cutPaper'  //开启切刀
}]

callback(ret, err)

ret:

  • 类型:JSON对象
  • 内部字段:
{
    status: true   //布尔型;true||false
}

err:

  • 类型:JSON对象
  • 内部字段:
{
    msg:""
}

示例代码

var obj = api.require('xGprinterPlus');
demo.printReceipt({
    rows : [{
        lineType : 'lines',
        data : 3
    },{//设置打印居中
        lineType : 'align',
        data : 'center'
    },{//设置为倍高倍宽
        lineType : 'style',
        data : [0,0,1,1,0]
    },{// 打印文字
        lineType : 'text',
        data : 'Sample\n'
    },{
        lineType : 'lineFeed'
    },{//取消倍高倍宽
        lineType : 'style',
        data : [0,0,0,0,0]
    },{//设置打印左对齐
        lineType : 'align',
        data : 'left'
    },{// 打印文字
        lineType : 'text',
        data : 'Print text\n'
    },{// 打印文字
        lineType : 'text',
        data : 'Welcome to use printer!\n'
    },{
        lineType : 'lineFeed'
    },{// 打印文字
        lineType : 'text',
        data : 'Print bitmap!\n'
    },{
        lineType : 'image',
        data : 'widget://image/huibo_logo.png'
    },{// 打印文字
        lineType : 'text',
        data : 'Print code128\n'
    },{//设置条码可识别字符位置在条码下方
        lineType : 'barcodePosition',
        data : 'below'
    },{//设置条码高度为60点
        lineType : 'barcodeHeight',
        data : 60
    },{//设置条码单元宽度为1点
        lineType : 'barcodeWidth',
        data : 1
    },{//打印Code128码
        lineType : 'code128',
        data : '123456789'
    },{
        lineType : 'lineFeed'
    },{// 打印文字
        lineType : 'text',
        data : 'Print QRcode\n'
    },{//设置纠错等级
        lineType : 'levelForQRCode',
        data : 31
    },{//设置qrcode插件大小
        lineType : 'levelForQRCode',
        data : 3
    },{//设置qrcode内容
        lineType : 'qrcode',
        data : "www.printer.com.cn"
    },{
        lineType : 'lineFeed'
    },{//设置打印居中
        lineType : 'align',
        data : 'center'
    },{// 打印文字
        lineType : 'text',
        data : 'Completed!\r\n'
    },{//开钱箱
        lineType : 'openBox'
    }]
},function(ret, err){
    alert(JSON.stringify(ret)+"   "+ JSON.stringify(err));
});

可用性

Android系统

可提供的1.0.5及更高版本

printLabel

打印标签(TSC模式打印,确保打印机处于标签模式)

printLabel({params}, callback(ret, err))

params

xh:

  • 类型:数字型
  • 描述:(可选项)指定打印机0-3。
  • 默认值:0

rows:

  • 类型:数组
  • 描述:(必输项)打印数据。
[{
    lineType: 'size',//设置标签尺寸,按照实际尺寸设置
    width : 80,
    height : 72
},{
    lineType: 'gap',   //设置标签间隙,按照实际尺寸设置
    data : 0    //如果为无间隙纸则设置为0
},{
    lineType: 'direction',   //设置打印方向
    direction : 'backward',   //backward|forward
    mirror : 'normal'         //normal|mirror
},{
    lineType: 'reference',   /设置原点坐标
    x : 0 ,
    y : 0
},{
    lineType: 'tear',   //撕纸模式开启
    data : true
},{
    lineType: 'cls'  // 清除打印缓冲区
},{
    lineType: 'text',  //绘制简体中文
    x : 20 ,
    y : 20,
    rotation : 0, // 0|90|180|270
    xscal : 1,    //1~10
    yscal : 1,     //1~10
    data : '要打印的文字'
},{
    lineType: 'image',  //绘制图片
    x : 20,
    y : 50,
    data : 'widget://image/huibo_logo.png' //支持fs:// widget://
},{
    lineType: 'qrcode',  绘制qrcode
    x : 250 ,
    y : 80 ,
    level : 'L',  //L|M|H|Q
    cellwith : 5,
    rotation : 0, // 0|90|180|270
    data : '要打印的二维码内容'
},{
    lineType: 'code128',  //绘制一维条码
    x : 250 ,
    y : 80 ,
    height : 100,
    rotation : 0, // 0|90|180|270
    data : '123456789'
},{
    lineType: 'bar',  //画线
    x : 0 ,
    y : 0 ,
    width : 0
    height : 0
}]

openBox:

  • 类型:布尔型
  • 描述:(可选项)是否打开钱箱。
  • 默认值:true

callback(ret, err)

ret:

  • 类型:JSON对象
  • 内部字段:
{
    status: true   //布尔型;true||false
}

err:

  • 类型:JSON对象
  • 内部字段:
{
    msg:""
}

示例代码

var obj = api.require('xGprinterPlus');
demo.printLabel({
    rows : [{
        lineType : 'size',
        width : 80,
        height : 72
    },{
        lineType : 'gap',
        data : 0
    },{
        lineType : 'cls'
    },{
        lineType : 'gap',
        data : 0
    },{// 左边框
        lineType : 'bar',
        x : 16,
        y :  0,
        width : 4,
        height : 556
    },{// 上边框
        lineType : 'bar',
        x : 16,
        y :  0,
        width : 608,
        height : 8
    },{// 右边框
        lineType : 'bar',
        x : 624,
        y :  0,
        width : 4,
        height : 556
    },{// 下边框
        lineType : 'bar',
        x : 16,
        y :  556,
        width : 608,
        height : 4
    },{// 图片单元格竖线
        lineType : 'bar',
        x : 16 + 130,
        y :  0,
        width : 2,
        height : 106
    },{//绘制图片
        lineType : 'image',
        x : 16 + 8,
        y :  8,
        data : 'widget://image/huibo_logo.png'
    },{//绘制文本
        lineType : 'text',
        x : 16 + 8,
        y :  8,
        rotation : 0,
        xscal : 1,
        yscal : 1,
        data : '青岛汇博通商贸有限公司'
    },{// 图片单元格横线
        lineType : 'bar',
        x : 16 + 130,
        y :  56,
        width : 640 - 130 - 16 - 16,
        height : 2
    },{// 二维码旁边竖线
        lineType : 'bar',
        x : 16 + 320,
        y :  106,
        width : 2,
        height : 556 - 106
    },{// 横线
        lineType : 'bar',
        x : 16 ,
        y :  106,
        width : 608,
        height : 2
    },{//绘制文本
        lineType : 'text',
        x : 16 + 4,
        y :  106 + 2,
        rotation : 0,
        xscal : 1,
        yscal : 2,
        data : '捆包号:R103571234'
    },{//绘制文本
        lineType : 'text',
        x : 16 + 320 + 2,
        y :  106 + 2,
        rotation : 0,
        xscal : 1,
        yscal : 2,
        data : '钢厂资源号:ML123456789'
    },{// 横线
        lineType : 'bar',
        x : 16 + 320 + 2 ,
        y :  156,
        width : 608,
        height : 2
    },{//绘制code128
        lineType : 'code128',
        x : 16 + 16,
        y :  156 + 2,
        height : 48,
        rotation : 0,
        data : '1234567890'
    },{//绘制文本
        lineType : 'text',
        x : 16 + 320 + 2,
        y :  156 + 2,
        rotation : 0,
        xscal : 1,
        yscal : 2,
        data : '母卷号:P123456789'
    },{// 横线
        lineType : 'bar',
        x : 16 ,
        y :  206,
        width : 608,
        height : 2
    },{//绘制文本
        lineType : 'text',
        x : 16 + 4,
        y :  206 + 2,
        rotation : 0,
        xscal : 1,
        yscal : 2,
        data : '品名:冷轧卷'
    },{//绘制文本
        lineType : 'text',
        x : 16 + 320 + 2,
        y :  206 + 2,
        rotation : 0,
        xscal : 1,
        yscal : 2,
        data : '牌号:DC06'
    },{// 横线
        lineType : 'bar',
        x : 16 ,
        y :  256,
        width : 608,
        height : 2
    },{//绘制文本
        lineType : 'text',
        x : 16 + 4,
        y :  256 + 2,
        rotation : 0,
        xscal : 1,
        yscal : 2,
        data : '规格:1.2*1250*C'
    },{//绘制文本
        lineType : 'text',
        x : 16 + 320 + 2,
        y :  256 + 2,
        rotation : 0,
        xscal : 1,
        yscal : 2,
        data : '净重:10.5t'
    },{// 横线
        lineType : 'bar',
        x : 16 ,
        y :  306,
        width : 608,
        height : 2
    },{//绘制文本
        lineType : 'text',
        x : 16 + 4,
        y :  306 + 2,
        rotation : 0,
        xscal : 1,
        yscal : 2,
        data : '毛重:10.8t'
    },{//绘制文本
        lineType : 'text',
        x : 16 + 320 + 2,
        y :  306 + 2,
        rotation : 0,
        xscal : 1,
        yscal : 2,
        data : '件数:1'
    },{// 横线
        lineType : 'bar',
        x : 16 ,
        y :  356,
        width : 608,
        height : 2
    },{//绘制文本
        lineType : 'text',
        x : 16 + 4,
        y :  356 + 2,
        rotation : 0,
        xscal : 1,
        yscal : 2,
        data : '库区:原料区'
    },{//绘制qrcode
        lineType : 'qrcode',
        x : 16 + 320 + 2 + 100,
        y :  356 + 2 + 25,
        level : 'L',
        cellwith : 5,
        rotation : 0,
        data : '捆包号|钢厂资源号|母卷号|品名|牌号|规格|净重|毛重'
    },{// 横线
        lineType : 'bar',
        x : 16 ,
        y :  406,
        width : 320,
        height : 2
    },{//绘制文本
        lineType : 'text',
        x : 16 + 4,
        y :  406 + 2,
        rotation : 0,
        xscal : 1,
        yscal : 2,
        data : '库位:YC-02'
    },{// 横线
        lineType : 'bar',
        x : 16 ,
        y :  456,
        width : 320,
        height : 2
    },{//绘制文本
        lineType : 'text',
        x : 16 + 4,
        y :  456 + 2,
        rotation : 0,
        xscal : 1,
        yscal : 2,
        data : '收货单位:英大'
    },{// 横线
        lineType : 'bar',
        x : 16 ,
        y :  506,
        width : 320,
        height : 2
    },{//绘制文本
        lineType : 'text',
        x : 16 + 4,
        y :  506 + 2,
        rotation : 0,
        xscal : 1,
        yscal : 2,
        data : '日期:2016.06.12'
    }]
},function(ret, err){
    alert(JSON.stringify(ret)+"   "+ JSON.stringify(err));
});

可用性

Android系统

可提供的1.0.5及更高版本

connectBleByMacAddress

根据mac地址连接蓝牙设备(仅单设备时使用)

connectBleByMacAddress(callback(ret, err))

params

macAddress:

  • 类型:字符串型
  • 描述:(必填项)指定需要连接的蓝牙设备的mac地址。

callback(ret, err)

ret:

  • 类型:JSON对象
  • 内部字段:
{
    status: true,   //布尔型;true||false
}

err:

  • 类型:JSON对象
  • 内部字段:
{
    msg: ''
}

示例代码

var demo = api.require('xGprinterPlus');
demo.connectBleByMacAddress({
    macAddress : ''
},function(ret, err) {
    api.alert({msg: JSON.stringify(ret)});
});

可用性

Android系统

可提供的1.0.5及更高版本

是否仍需要帮助? 请保持联络!
最后更新于 2024/07/22