Android Question Zebra Printer

Discussion in 'Android Questions' started by RonkyOne, Mar 15, 2019.

  1. RonkyOne

    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

    Code:
    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
     
  2. DonManfred

    DonManfred Expert Licensed User

    Is this a B4A or a B4i question?
     
  3. RonkyOne

    RonkyOne Member Licensed User

    B4A
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    This is a community forum. You should not limit your questions or requests to a single member.
     
    Last edited: Mar 17, 2019
  5. DonManfred

    DonManfred Expert Licensed User

    There is missing a NOT ;-)
     
    Erel likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice