Read this in other launguages: [简体中文](https://github.com/Zebra/Zebra-Printer-Samples/blob/master/WeChat-MiniProgram-Samples/WeChatPrintDemo/READ.zh-cn.md)
## Introduction
Zebra printers of ZQ300 & ZQ500 series mobile printers, ZD400 & ZD600 series desktop printers, and ZT600 series industrial printers have both Bluetooth Classic and Low Energy (LE) capabilities. This WeChatPrintDemo demonstrates how to scan, connect, send ZPL to a BLE enabled Zebra printer from WeChat Mini-Program to print labels and images. For details on how to create WeChat Mini-Program, please refer to [WeChat Mini-Program for Developers](https://mp.weixin.qq.com/) site, where you can register a developer account, check out the API documentation, download SDK and tutorials.
To query if Bluetooth LE is enabled or not on a printer, use Zebra SGD command below with [Zebra Setup Utilities](https://www.zebra.com/us/en/products/software/barcode-printers/zebralink/zebra-setup-utility.html):
* `! U1 getvar "bluetooth.le.controller_mode"`
To enable Bluetooth LE on a printer, use one of the following Zebra SGD commands to enable the BLE on the printer with [Zebra Setup Utilities](https://www.zebra.com/us/en/products/software/barcode-printers/zebralink/zebra-setup-utility.html):
* `! U1 setvar "bluetooth.le.controller_mode" "le"`
* `! U1 setvar "bluetooth.le.controller_mode" "both"`
## Services on Zebra printers
The Zebra Bluetooth LE enabled printers offer two services, i.e. Device Information Service (DIS, UUID is `0x180A`) and Parser Service (UUID is `38eb4a80-c570-11e3-9507-0002a5d5c51b`). These services cannot be discovered unless the central device has connected to the printer.
The DIS is a standard service that includes the characteristics of Device Name, Serial Number, Firmware Revision, etc. that can be read back. The Parser Service offers two characteristics for getting data from printer (named as `"From Printer Data"`) and for sending data to printer (named as `"To Printer Data"`).
As defined in [Link-OS Environment Bluetooth Low Energy AppNote](https://www.zebra.com/content/dam/zebra/software/en/application-notes/AppNote-BlueToothLE-v4.pdf) document by Zebra, the UUIDs of the services and characteristics of Zebra BLE enabled printers are defined in pages/index/index.js file as below.
```javascript
// Zebra Bluetooth LE services and characteristics UUIDs
const ZPRINTER_DIS_SERVICE_UUID = "0000180A-0000-1000-8000-00805F9B34FB" // Or "180A". Device Information Services UUID
const ZPRINTER_SERVICE_UUID="38EB4A80-C570-11E3-9507-0002A5D5C51B" // Zebra Bluetooth LE Parser Service
const READ_FROM_ZPRINTER_CHARACTERISTIC_UUID = "38EB4A81-C570-11E3-9507-0002A5D5C51B" // Read from printer characteristic
const WRITE_TO_ZPRINTER_CHARACTERISTIC_UUID = "38EB4A82-C570-11E3-9507-0002A5D5C51B" // Write to printer characteristic
```
## Write to characteristic
Each write to the characteristic operation is limited to a number of bytes in BLE. We need to break the ZPL or image data into small chunks and write a chunk of bytes a time to the characteristic. On iOS, the wx.writeBLECharacteristicValue() has no issues when writing each chunk one after another. On Android, however, we must provide a delay between the writes of chunks. Following code illustrates how to break the ZPL or image data into chunks and how the delay is implemented for Android. Both the maxChunk and the delay in setTimeout() should be tuned to fit a particular Android device and performance. Currently, the delay is 250ms for each write.
```javascript
// Write printer language string to the printer
writeStringToPrinter: function (str) {
var that = this
var maxChunk = 20 // Default is 20 bytes per write to characteristic
if (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 = 0
for (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 needed
j++
}
}
}
function writeStrToCharacteristic (str) {
// Convert str to ArrayBuff and write to printer
let 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 printer
wx.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,
})
}
})
}
},
```
## Screenshot and printout
###### A screenshot of the mini-program
<img src="https://developer.zebra.com/resources/statics/56275/WeChatPrintDemo.jpg" width="400">
###### Printouts of a barcode and a logo image on a 2" wide label from Zebra ZD410 desktop printer
<img src="https://developer.zebra.com/resources/statics/56275/PrintoutWeChatDem.jpg" width="400">
## Notes
1. On Android, the WeChat app should be granted with access permission to location service first. in order for this mini-program to scan for and connect to a Bluetooth LE device. Otherwise, this mini-program won't be able to find any BLE devices during the scan operation. To grant location permission to WeChat app, go to Settings -> Apps -> WeChat -> Permissions.
2. On Android, the sequential execution of wx.writeBLECharacteristicValue() without delay in between will cause the write operation to fail. In this example, we give 250ms delay in between. This delay can be adjusted if needed.
## References
This WeChatPrintDemo uses or refers to the following materials:
* [Link-OS Environment Bluetooth Low Energy AppNote](https://www.zebra.com/content/dam/zebra/software/en/application-notes/AppNote-BlueToothLE-v4.pdf), by Zebra
* [WeChat Mini-Program Software Development Kit](https://mp.weixin.qq.com/), by Tencent
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
斑马打印机样本主 重要提示:在使用之前,请仔细阅读最终用户许可_ 包含与Zebra打印机相关的样本,演示等。 ZQ300和ZQ500系列移动打印机的Zebra打印机,ZD400和ZD600系列台式打印机以及ZT600系列工业打印机具有Bluetooth Classic和低能耗(LE)功能。 此WeChatPrintDemo演示了如何从微信小程序扫描,连接ZPL并将其发送到启用BLE的Zebra打印机,以打印标签和图像。 有关如何创建微信小程序的详细信息,请访问网站,您可以在其中注册开发者帐户,查看API文档,下载SDK和教程。
资源详情
资源评论
资源推荐
收起资源包目录
Zebra-Printer-Samples-master-master.zip (17个子文件)
Zebra-Printer-Samples-master-master
LICENSE_AGREEMENT.pdf 69KB
README.md 817B
WeChat-MiniProgram-Samples
README.md 129B
WeChatPrintDemo
app.js 1KB
project.config.json 537B
utils
util.js 472B
README.md 7KB
READ.zh-cn.md 7KB
pages
index
index.wxml 3KB
index.js 21KB
index.wxss 1KB
logs
logs.wxml 173B
logs.js 261B
logs.wxss 106B
logs.json 52B
app.json 251B
app.wxss 200B
共 17 条
- 1
雪地女王
- 粉丝: 95
- 资源: 4601
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0