影视久久国产高清_成人毛片网站av_国产天堂AV在线色_亚洲国产精品人人爽夜夜爽_91免费观看国产_亚洲av网站在线播放_中文字幕日韩在线观看_欧美精品乱码在线看_亚洲AⅤ永久无码毛片蜜桃_日韩激情无码专区精品

新聞資訊

斑馬標(biāo)簽打印機(jī)在微信中調(diào)用的方法

2025-03-13 08:52:42


現(xiàn)在微信使用越來(lái)越便利,最近我在官網(wǎng)論壇找到了一個(gè)微信小程序的例子。

image.png

       搭建了一個(gè)微信小程序(WeChatPrintDemo,可從GitHub下載獲?。﹣?lái)演示如何用微信,在斑馬打印機(jī)上打印標(biāo)簽及圖像。WeChatPrintDemo的軟件實(shí)現(xiàn)進(jìn)行了詳盡描述、并指出了在Android上向藍(lán)牙設(shè)備特征值寫二進(jìn)制數(shù)據(jù)的注意事項(xiàng)。

       WeChatPrintDemo演示了如何從微信小程序掃描,連接ZPL并將其發(fā)送到啟用BLE的Zebra打印機(jī),以打印標(biāo)簽和圖像。有關(guān)如何創(chuàng)建微信小程序的詳細(xì)信息,請(qǐng)?jiān)L問微信小程序開發(fā)者網(wǎng)站,您可以在其中注冊(cè)開發(fā)者帳戶,查看API文檔,下載SDK和教程。


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


要查詢打印機(jī)上是否啟用了藍(lán)牙LE,請(qǐng)?jiān)赯ebra設(shè)置實(shí)用程序ZSU中, 使用以下Zebra SGD命令:


U1 getvarbluetooth.le.controller_mode

要在打印機(jī)上啟用Bluetooth LE,請(qǐng)使用以下Zebra SGD命令之一來(lái)通過Zebra設(shè)置實(shí)用程序在打印機(jī)上啟用BLE:

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

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


Services on Zebra printers

斑馬打印機(jī)上的藍(lán)牙LE服務(wù)

斑馬打印機(jī)在藍(lán)牙LE上提供了兩種服務(wù):一種是DIS服務(wù)(Device Information Service, UUID: `0x180A`),另一種是解析服務(wù)(Parser Service, UUID: `38eb4a80-c570-11e3-9507-0002a5d5c51b`)。這兩種服務(wù)只能在藍(lán)牙LE連接上以后,才能被發(fā)現(xiàn)。


DIS是一種藍(lán)牙的標(biāo)準(zhǔn)服務(wù)。中央設(shè)備(Central Devices)可以從DIS服務(wù)中讀回打印機(jī)的設(shè)備名稱、序列號(hào)、固件版本等特征值。解析服務(wù)是斑馬打印機(jī)特有的服務(wù),該服務(wù)包含兩種特征值:從打印機(jī)讀數(shù)據(jù)(`"From Printer Data"`)和向打印機(jī)寫數(shù)據(jù)(`"To Printer Data"`)。


斑馬打印機(jī)藍(lán)牙LE服務(wù)和特征值的UUID,都已經(jīng)由[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 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

每次對(duì)特征操作的寫入都限制為BLE中的字節(jié)數(shù)。我們需要將ZPL或圖像數(shù)據(jù)分成小塊,并一次向特征寫入一個(gè)字節(jié)塊。在iOS上,一個(gè)接一個(gè)地寫每個(gè)塊時(shí),wx.writeBLECharacteristicValue()沒有問題。但是,在Android上,我們必須在寫入塊之間提供延遲。以下代碼說(shuō)明了如何將ZPL或圖像數(shù)據(jù)分成多個(gè)塊,以及如何為Android實(shí)現(xiàn)延遲。應(yīng)該調(diào)整maxChunk和setTimeout()中的延遲以適合特定的Android設(shè)備和性能。當(dāng)前,每次寫入的延遲為250ms。


 // 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,
})
}
})
}
},


此外還可以進(jìn)行截圖,圖片打印等功能


Demo 軟件運(yùn)行參考圖



image.png

參照了如下文獻(xiàn):

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

* 騰訊:[微信小程序軟件開發(fā)環(huán)境和工具](https://mp.weixin.qq.com/)

客服