xGprinterPlus

connectBle disConnect getPrinterStatus setPrinterCommandType printLabel

论坛示例

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

概述

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

固件要求

Android:4.0及以上

connectDevices

连接多设备

connectDevices(callback(ret, err))

callback(ret, err)

ret:

  • 类型:JSON对象
  • 内部字段:

err:

  • 类型:JSON对象
  • 内部字段:
  1. msg: ''
  2. }

示例代码

  1. var demo = api.require('xGprinterPlus');
  2. demo.connectDevices(function(ret, err) {
  3. alert(JSON.stringify(ret));
  4. });

可用性

Android系统

可提供的1.0.5及更高版本

connectBle

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

connectBle(callback(ret, err))

callback(ret, err)

ret:

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

err:

  • 类型:JSON对象
  • 内部字段:
  1. {
  2. msg: ''
  3. }

示例代码

  1. var demo = api.require('xGprinterPlus');
  2. demo.connectBle(function(ret, err) {
  3. alert(JSON.stringify(ret));
  4. });

可用性

Android系统

可提供的1.0.5及更高版本

connectUsb

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

connectUsb(callback(ret, err))

callback(ret, err)

ret:

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

err:

  • 类型:JSON对象
  • 内部字段:
  1. {
  2. msg: ''
  3. }

示例代码

  1. var demo = api.require('xGprinterPlus');
  2. demo.connectUsb(function(ret, err) {
  3. alert(JSON.stringify(ret));
  4. });

可用性

Android系统

可提供的1.0.5及更高版本

disConnect

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

disConnect(callback(ret, err))

callback(ret, err)

ret:

  • 类型:JSON对象
  • 内部字段:
  1. {
  2. status: true, //布尔型;true||false
  3. }
  • 类型:JSON对象
  • 内部字段:
  1. {
  2. msg: ''
  3. }

示例代码

可用性

Android系统

可提供的1.0.5及更高版本

printTest

打印测试页

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

xh:

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

callback(ret, err)

ret:

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

err:

  • 类型:JSON对象
  • 内部字段:
  1. {
  2. msg: ''
  3. }

示例代码

  1. var demo = api.require('xGprinterPlus');
  2. demo.printTest(function(ret, err) {
  3. alert(JSON.stringify(ret));
  4. });

可用性

Android系统

可提供的1.0.5及更高版本

getPrinterStatus

查询打印机状态

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

params

xh:

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

callback(ret, err)

ret:

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

err:

  • 类型:JSON对象
  • 内部字段:
  1. {
  2. msg: ''
  3. }

示例代码

  1. var obj = api.require('xGprinterPlus');
  2. obj.getPrinterStatus(function(ret, err) {
  3. alert(JSON.stringify(ret));
  4. });

可用性

Android系统

可提供的1.0.5及更高版本

getConnectState

查询打印机连接状态

getConnectState()

ret:

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

示例代码

  1. var obj = api.require('xGprinterPlus');
  2. var ret = obj.getConnectState();
  3. alert(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对象
  • 内部字段:
  1. {
  2. status: true, //布尔型;true||false
  3. }
  • 类型:JSON对象
  • 内部字段:
  1. {
  2. msg: ''
  3. }

可用性

Android系统

可提供的1.0.5及更高版本

printReceipt

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

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

params

xh:

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

rows:

  • 类型:数组
  • 描述:(必输项)打印数据。
  1. [{
  2. lineType: 'lines',//打印空行,要传行数
  3. data : 0 //要打印的空行行数
  4. },{
  5. lineType: 'style', //设置样式
  6. data : [0,0,0,0,0] //字体A/B:(0:A|1:B);是否加粗:(0:关闭|1:开启);倍高:(0:关闭|1:开启);倍宽:(0:关闭|1:开启);下划线:(0:关闭|1:开启)
  7. },{
  8. lineType: 'align', //设置字体位置
  9. data : 'center', //left|center|right
  10. },{
  11. lineType: 'text', //设置打印内容
  12. data : '打印文本'
  13. },{
  14. lineType: 'lineFeed' //设置空行,不需要设置行数
  15. },{
  16. lineType: 'image', //设置打印图片
  17. data : 'widget://image/huibo_logo.png' //支持 fs:// widget:// 路径
  18. },{
  19. lineType: 'barcodePosition', //设置条码可识别字符位置
  20. data: 'below' //设置打印位置 above|aboveAndBelow|below|noPrint
  21. },{
  22. lineType: 'barcodeHeight', //设置条码高度
  23. data : 60 //条码高度
  24. },{
  25. lineType: 'barcodeWidth', //设置条码单元宽度
  26. data : 1 //宽度为1点
  27. },{
  28. lineType: 'code128', //打印Code128码
  29. data : '123456789'
  30. },{
  31. lineType: 'levelForQRCode', //设置纠错等级
  32. data : 31
  33. },{
  34. lineType: 'moduleForQRCode', //设置qrcode模块大小
  35. data : 3
  36. },{
  37. lineType: 'qrcode', //设置qrcode内容
  38. data : 'qrcode内容'
  39. },{
  40. lineType: 'openBox' //开钱箱
  41. },{
  42. lineType: 'ImmediatelyOpenBox' //立即开钱箱
  43. },{
  44. }]

callback(ret, err)

ret:

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

err:

  • 类型:JSON对象
  • 内部字段:
  1. {
  2. msg:""
  3. }

示例代码

  1. var obj = api.require('xGprinterPlus');
  2. demo.printReceipt({
  3. rows : [{
  4. lineType : 'lines',
  5. data : 3
  6. },{//设置打印居中
  7. lineType : 'align',
  8. data : 'center'
  9. },{//设置为倍高倍宽
  10. lineType : 'style',
  11. data : [0,0,1,1,0]
  12. },{// 打印文字
  13. lineType : 'text',
  14. data : 'Sample\n'
  15. },{
  16. lineType : 'lineFeed'
  17. },{//取消倍高倍宽
  18. lineType : 'style',
  19. data : [0,0,0,0,0]
  20. },{//设置打印左对齐
  21. lineType : 'align',
  22. data : 'left'
  23. },{// 打印文字
  24. lineType : 'text',
  25. data : 'Print text\n'
  26. },{// 打印文字
  27. lineType : 'text',
  28. data : 'Welcome to use printer!\n'
  29. },{
  30. lineType : 'lineFeed'
  31. },{// 打印文字
  32. lineType : 'text',
  33. data : 'Print bitmap!\n'
  34. },{
  35. lineType : 'image',
  36. data : 'widget://image/huibo_logo.png'
  37. },{// 打印文字
  38. lineType : 'text',
  39. data : 'Print code128\n'
  40. },{//设置条码可识别字符位置在条码下方
  41. lineType : 'barcodePosition',
  42. data : 'below'
  43. },{//设置条码高度为60点
  44. lineType : 'barcodeHeight',
  45. data : 60
  46. },{//设置条码单元宽度为1点
  47. lineType : 'barcodeWidth',
  48. data : 1
  49. },{//打印Code128码
  50. lineType : 'code128',
  51. data : '123456789'
  52. },{
  53. },{// 打印文字
  54. lineType : 'text',
  55. data : 'Print QRcode\n'
  56. },{//设置纠错等级
  57. lineType : 'levelForQRCode',
  58. data : 31
  59. },{//设置qrcode模块大小
  60. lineType : 'levelForQRCode',
  61. data : 3
  62. },{//设置qrcode内容
  63. lineType : 'qrcode',
  64. data : "www.printer.com.cn"
  65. },{
  66. lineType : 'lineFeed'
  67. },{//设置打印居中
  68. lineType : 'align',
  69. data : 'center'
  70. },{// 打印文字
  71. lineType : 'text',
  72. data : 'Completed!\r\n'
  73. },{//开钱箱
  74. lineType : 'openBox'
  75. }]
  76. },function(ret, err){
  77. alert(JSON.stringify(ret)+" "+ JSON.stringify(err));
  78. });

可用性

Android系统

可提供的1.0.5及更高版本

printLabel

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

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

params

xh:

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

rows:

  • 类型:数组
  • 描述:(必输项)打印数据。
  1. [{
  2. lineType: 'size',//设置标签尺寸,按照实际尺寸设置
  3. width : 80,
  4. height : 72
  5. },{
  6. lineType: 'gap', //设置标签间隙,按照实际尺寸设置
  7. data : 0 //如果为无间隙纸则设置为0
  8. },{
  9. lineType: 'direction', //设置打印方向
  10. direction : 'backward', //backward|forward
  11. mirror : 'normal' //normal|mirror
  12. },{
  13. lineType: 'reference', /设置原点坐标
  14. x : 0 ,
  15. y : 0
  16. },{
  17. lineType: 'tear', //撕纸模式开启
  18. data : true
  19. },{
  20. lineType: 'cls' // 清除打印缓冲区
  21. },{
  22. lineType: 'text', //绘制简体中文
  23. x : 20 ,
  24. y : 20,
  25. rotation : 0, // 0|90|180|270
  26. xscal : 1, //1~10
  27. yscal : 1, //1~10
  28. data : '要打印的文字'
  29. },{
  30. lineType: 'image', //绘制图片
  31. x : 20,
  32. y : 50,
  33. data : 'widget://image/huibo_logo.png' //支持fs:// widget://
  34. },{
  35. lineType: 'qrcode', 绘制qrcode
  36. x : 250 ,
  37. y : 80 ,
  38. level : 'L', //L|M|H|Q
  39. cellwith : 5,
  40. rotation : 0, // 0|90|180|270
  41. data : '要打印的二维码内容'
  42. },{
  43. lineType: 'code128', //绘制一维条码
  44. x : 250 ,
  45. y : 80 ,
  46. height : 100,
  47. rotation : 0, // 0|90|180|270
  48. data : '123456789'
  49. },{
  50. lineType: 'bar', //画线
  51. x : 0 ,
  52. y : 0 ,
  53. width : 0
  54. height : 0
  55. }]

openBox:

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

callback(ret, err)

ret:

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

err:

  • 类型:JSON对象
  • 内部字段:
  1. {
  2. msg:""
  3. }

示例代码

  1. var obj = api.require('xGprinterPlus');
  2. demo.printLabel({
  3. rows : [{
  4. lineType : 'size',
  5. width : 80,
  6. height : 72
  7. },{
  8. lineType : 'gap',
  9. data : 0
  10. },{
  11. lineType : 'cls'
  12. },{
  13. lineType : 'gap',
  14. data : 0
  15. },{// 左边框
  16. lineType : 'bar',
  17. x : 16,
  18. y : 0,
  19. width : 4,
  20. height : 556
  21. },{// 上边框
  22. lineType : 'bar',
  23. x : 16,
  24. y : 0,
  25. width : 608,
  26. height : 8
  27. },{// 右边框
  28. lineType : 'bar',
  29. x : 624,
  30. y : 0,
  31. width : 4,
  32. height : 556
  33. },{// 下边框
  34. lineType : 'bar',
  35. x : 16,
  36. y : 556,
  37. width : 608,
  38. height : 4
  39. },{// 图片单元格竖线
  40. lineType : 'bar',
  41. x : 16 + 130,
  42. y : 0,
  43. width : 2,
  44. height : 106
  45. },{//绘制图片
  46. lineType : 'image',
  47. x : 16 + 8,
  48. data : 'widget://image/huibo_logo.png'
  49. },{//绘制文本
  50. lineType : 'text',
  51. x : 16 + 8,
  52. y : 8,
  53. rotation : 0,
  54. xscal : 1,
  55. yscal : 1,
  56. data : '青岛汇博通商贸有限公司'
  57. },{// 图片单元格横线
  58. lineType : 'bar',
  59. x : 16 + 130,
  60. y : 56,
  61. width : 640 - 130 - 16 - 16,
  62. height : 2
  63. },{// 二维码旁边竖线
  64. lineType : 'bar',
  65. x : 16 + 320,
  66. y : 106,
  67. width : 2,
  68. height : 556 - 106
  69. },{// 横线
  70. lineType : 'bar',
  71. x : 16 ,
  72. y : 106,
  73. width : 608,
  74. height : 2
  75. },{//绘制文本
  76. lineType : 'text',
  77. x : 16 + 4,
  78. y : 106 + 2,
  79. rotation : 0,
  80. xscal : 1,
  81. yscal : 2,
  82. },{//绘制文本
  83. lineType : 'text',
  84. x : 16 + 320 + 2,
  85. y : 106 + 2,
  86. rotation : 0,
  87. xscal : 1,
  88. yscal : 2,
  89. data : '钢厂资源号:ML123456789'
  90. },{// 横线
  91. lineType : 'bar',
  92. x : 16 + 320 + 2 ,
  93. y : 156,
  94. width : 608,
  95. height : 2
  96. },{//绘制code128
  97. lineType : 'code128',
  98. x : 16 + 16,
  99. y : 156 + 2,
  100. height : 48,
  101. rotation : 0,
  102. data : '1234567890'
  103. },{//绘制文本
  104. lineType : 'text',
  105. x : 16 + 320 + 2,
  106. y : 156 + 2,
  107. rotation : 0,
  108. xscal : 1,
  109. yscal : 2,
  110. data : '母卷号:P123456789'
  111. },{// 横线
  112. lineType : 'bar',
  113. x : 16 ,
  114. y : 206,
  115. width : 608,
  116. height : 2
  117. },{//绘制文本
  118. lineType : 'text',
  119. x : 16 + 4,
  120. y : 206 + 2,
  121. rotation : 0,
  122. xscal : 1,
  123. yscal : 2,
  124. data : '品名:冷轧卷'
  125. },{//绘制文本
  126. lineType : 'text',
  127. x : 16 + 320 + 2,
  128. y : 206 + 2,
  129. rotation : 0,
  130. xscal : 1,
  131. yscal : 2,
  132. data : '牌号:DC06'
  133. },{// 横线
  134. lineType : 'bar',
  135. x : 16 ,
  136. y : 256,
  137. width : 608,
  138. height : 2
  139. },{//绘制文本
  140. lineType : 'text',
  141. x : 16 + 4,
  142. y : 256 + 2,
  143. rotation : 0,
  144. xscal : 1,
  145. yscal : 2,
  146. data : '规格:1.2*1250*C'
  147. },{//绘制文本
  148. lineType : 'text',
  149. x : 16 + 320 + 2,
  150. y : 256 + 2,
  151. rotation : 0,
  152. xscal : 1,
  153. yscal : 2,
  154. data : '净重:10.5t'
  155. },{// 横线
  156. lineType : 'bar',
  157. x : 16 ,
  158. y : 306,
  159. width : 608,
  160. height : 2
  161. },{//绘制文本
  162. lineType : 'text',
  163. x : 16 + 4,
  164. y : 306 + 2,
  165. rotation : 0,
  166. xscal : 1,
  167. yscal : 2,
  168. data : '毛重:10.8t'
  169. },{//绘制文本
  170. lineType : 'text',
  171. x : 16 + 320 + 2,
  172. y : 306 + 2,
  173. rotation : 0,
  174. xscal : 1,
  175. yscal : 2,
  176. data : '件数:1'
  177. },{// 横线
  178. lineType : 'bar',
  179. x : 16 ,
  180. y : 356,
  181. width : 608,
  182. height : 2
  183. },{//绘制文本
  184. lineType : 'text',
  185. x : 16 + 4,
  186. y : 356 + 2,
  187. rotation : 0,
  188. xscal : 1,
  189. yscal : 2,
  190. data : '库区:原料区'
  191. },{//绘制qrcode
  192. lineType : 'qrcode',
  193. x : 16 + 320 + 2 + 100,
  194. y : 356 + 2 + 25,
  195. level : 'L',
  196. cellwith : 5,
  197. rotation : 0,
  198. data : '捆包号|钢厂资源号|母卷号|品名|牌号|规格|净重|毛重'
  199. },{// 横线
  200. lineType : 'bar',
  201. x : 16 ,
  202. y : 406,
  203. width : 320,
  204. height : 2
  205. },{//绘制文本
  206. lineType : 'text',
  207. x : 16 + 4,
  208. y : 406 + 2,
  209. rotation : 0,
  210. xscal : 1,
  211. yscal : 2,
  212. data : '库位:YC-02'
  213. },{// 横线
  214. lineType : 'bar',
  215. x : 16 ,
  216. y : 456,
  217. width : 320,
  218. height : 2
  219. },{//绘制文本
  220. lineType : 'text',
  221. x : 16 + 4,
  222. y : 456 + 2,
  223. rotation : 0,
  224. xscal : 1,
  225. yscal : 2,
  226. data : '收货单位:英大'
  227. },{// 横线
  228. lineType : 'bar',
  229. x : 16 ,
  230. y : 506,
  231. width : 320,
  232. height : 2
  233. },{//绘制文本
  234. lineType : 'text',
  235. x : 16 + 4,
  236. y : 506 + 2,
  237. rotation : 0,
  238. xscal : 1,
  239. yscal : 2,
  240. data : '日期:2016.06.12'
  241. }]
  242. },function(ret, err){
  243. alert(JSON.stringify(ret)+" "+ JSON.stringify(err));
  244. });

可用性

Android系统

可提供的1.0.5及更高版本

connectBleByMacAddress

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

connectBleByMacAddress(callback(ret, err))

params

macAddress:

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

callback(ret, err)

ret:

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

err:

  • 类型:JSON对象
  • 内部字段:
  1. {
  2. msg: ''
  3. }

示例代码

Android系统