Android Question Zebra Printer

Similar threads

B4A Library Printing and Pdf creation
B4A Library BLE 2 - Bluetooth Low Energy
B4A Code Snippet [B4X] Hex string to number
B4A Question Classes are soon coming...
B4A Library BLE Peripheral

RonkyOne

Member
Licensed User
Hi EREL ,
can you Help me to translate this program to print on ZEBRA PRINTER bluetooth(BLE)


I use BleSendData.zip example to search and connect zebra printer and it run ok.


ref for ZEBRA
https://github.com/Zebra/Zebra-Prin...MiniProgram-Samples/WeChatPrintDemo/README.md

B4X:
 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,
         })
       }
     })
   }
  }
thanks in advance
 
Top