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
没有合适的资源?快使用搜索试试~ 我知道了~
资料收集:微信小程序-BLE.rar

共17个文件
js:4个
md:4个
wxss:3个

需积分: 5 4 下载量 150 浏览量
2024-11-17
15:04:59
上传
评论
收藏 1.2MB RAR 举报
温馨提示
资料收集:微信小程序_BLE.rar 网络收集到的微信小程序BLE低功耗蓝牙示例项目源代码。
资源推荐
资源详情
资源评论



















收起资源包目录

























共 17 条
- 1
资源评论


Naiva
- 粉丝: 4w+
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 项目管理-采购管理.ppt
- 综合布线课程设计.pptx
- 国内医药电子商务营销的现状分析教材.pptx
- 物业新项目管理方案.doc
- 清华-谭浩强-C语言程序设计视频教程-第8章.ppt
- 通信工程工程量清单计价与投标快速报价实务全书.doc
- 云计算导论第6章.ppt
- 煤炭安全质量标准化精细化管理与信息化.ppt
- 电子商务培训班主持词.docx
- 基因工程习题答案.doc
- Python 实现数据结构的方法:助力课后作业与课程大作业
- 2023年最全的事业单位考试计算机基础知识试题.doc
- 信息化规划案例.doc
- 最新国家开放大学电大专科《信息化管理与运作》判断填空题题库及答案(试卷号:2499).docx
- 企业信息化教研中心实训方案.doc
- 施工现场安全管理网络及安全技术措施有用.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
