java调用打印机打印excel linux_在微信中调用打印机

现在微信使用越来越便利,最近我在官网论坛找到了一个微信小程序的例子。

05a89067c9538e1652b5fe04b38557bd.png

       搭建了一个微信小程序(WeChatPrintDemo,可从GitHub下载获取)来演示如何用微信,在斑马打印机上打印标签及图像。WeChatPrintDemo的软件实现进行了详尽描述、并指出了在Android上向蓝牙设备特征值写二进制数据的注意事项。

       WeChatPrintDemo演示了如何从微信小程序扫描,连接ZPL并将其发送到启用BLE的Zebra打印机,以打印标签和图像。有关如何创建微信小程序的详细信息,请访问微信小程序开发者网站,您可以在其中注册开发者帐户,查看API文档,下载SDK和教程。

https://github.com/ZebraDevs/Zebra-Printer-Samples/tree/WeChat-MiniProgram-Samples

要查询打印机上是否启用了蓝牙LE,请在Zebra设置实用程序ZSU中, 使用以下Zebra SGD命令:

!U1 getvar“ bluetooth.le.controller_mode”

要在打印机上启用Bluetooth LE,请使用以下Zebra SGD命令之一来通过Zebra设置实用程序在打印机上启用BLE:

! U1 setvar "bluetooth.le.controller_mode" "le"

! U1 setvar "bluetooth.le.controller_mode" "both"

Services on Zebra printers

斑马打印机上的蓝牙LE服务

斑马打印机在蓝牙LE上提供了两种服务:一种是DIS服务(Device Information Service, UUID: `0x180A`),另一种是解析服务(Parser Service, UUID: `38eb4a80-c570-11e3-9507-0002a5d5c51b`)。这两种服务只能在蓝牙LE连接上以后,才能被发现。

DIS是一种蓝牙的标准服务。中央设备(Central Devices)可以从DIS服务中读回打印机的设备名称、序列号、固件版本等特征值。解析服务是斑马打印机特有的服务,该服务包含两种特征值:从打印机读数据(`"From Printer Data"`)和向打印机写数据(`"To Printer Data"`)。

斑马打印机蓝牙LE服务和特征值的UUID,都已经由[Link-OS Environment Bluetooth Low Energy AppNote](https://www.zebra.com/content/dam/zebra/software/en/application-notes/AppNote-BlueToothLE-v4.pdf)文档定义好了,如下面所示:

// Zebra Bluetooth LE services and characteristics UUIDsconst ZPRINTER_DIS_SERVICE_UUID = "0000180A-0000-1000-8000-00805F9B34FB" // Or "180A". Device Information Services UUIDconst ZPRINTER_SERVICE_UUID="38EB4A80-C570-11E3-9507-0002A5D5C51B"       // Zebra Bluetooth LE Parser Serviceconst READ_FROM_ZPRINTER_CHARACTERISTIC_UUID = "38EB4A81-C570-11E3-9507-0002A5D5C51B" // Read from printer characteristicconst WRITE_TO_ZPRINTER_CHARACTERISTIC_UUID  = "38EB4A82-C570-11E3-9507-0002A5D5C51B" // Write to printer characteristic

Write to characteristic

每次对特征操作的写入都限制为BLE中的字节数。我们需要将ZPL或图像数据分成小块,并一次向特征写入一个字节块。在iOS上,一个接一个地写每个块时,wx.writeBLECharacteristicValue()没有问题。但是,在Android上,我们必须在写入块之间提供延迟。以下代码说明了如何将ZPL或图像数据分成多个块,以及如何为Android实现延迟。应该调整maxChunk和setTimeout()中的延迟以适合特定的Android设备和性能。当前,每次写入的延迟为250ms。

 // Write printer language string to the printerwriteStringToPrinter: function (str) {var that = thisvar maxChunk = 20 // Default is 20 bytes per write to characteristicif (app.getPlatform() == 'ios') {maxChunk = 300 // 300 bytes per write to characteristic works for iOS} else if (app.getPlatform() == 'android') {var maxChunk = 300 // Adjusting for Android      }if (str.length <= maxChunk) {writeStrToCharacteristic(str)} else {// Need to partion the string and write one chunk at a time.var j = 0for (var i = 0; i < str.length; i += maxChunk) {if (i + maxChunk <= str.length) {var subStr = str.substring(i, i + maxChunk)} else {var subStr = str.substring(i, str.length)}if (app.getPlatform() == 'ios') {writeStrToCharacteristic(subStr) // iOS doesn't need the delay during each write} else {// Android needs delay during each write.setTimeout(writeStrToCharacteristic, 250 * j, subStr) // Adjust the delay if neededj++}}}function writeStrToCharacteristic (str) {// Convert str to ArrayBuff and write to printerlet buffer = new ArrayBuffer(str.length)let dataView = new DataView(buffer)for (var i = 0; i < str.length; i++) {dataView.setUint8(i, str.charAt(i).charCodeAt())}// Write buffer to printerwx.writeBLECharacteristicValue({deviceId: that.data.connectedDeviceId,serviceId: ZPRINTER_SERVICE_UUID,characteristicId: WRITE_TO_ZPRINTER_CHARACTERISTIC_UUID,value: buffer,success: function (res) {wx.showToast({title: 'Sent ZPL to printer successfully',icon: 'success',duration: 1000,})},fail: function (res) {console.log("ssi - Failed to send ZPL to printer:", res)wx.showToast({title: 'Failed to send ZPL',icon: 'none',duration: 1000,})}})}},

此外还可以进行截图,图片打印等功能

Demo 软件运行参考图

29b050f680618fa21ce068b148afe889.png

参照了如下文献:

* 斑马:[Link-OS Environment Bluetooth Low Energy AppNote](https://www.zebra.com/content/dam/zebra/software/en/application-notes/AppNote-BlueToothLE-v4.pdf)

* 腾讯:[微信小程序软件开发环境和工具](https://mp.weixin.qq.com/)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值