6 Replies Latest reply on Aug 20, 2015 12:10 AM by Nguyen Doan

    Print on iMZ320 with iOS SDK (Objective C)

    Nguyen Doan

      Hello,

       

      I'm developing an iPhone application, it has print feature. I use iZM320, I did test with Zebra SDK and it worked fine.

      But with I try with my app, after send print cmd, the bluetooth light on the printer flashed about 3-4 times, but no paper was printed.

      I attached my source code, I hope anybody can help me.

      1. Printing string

      ^XA^PON^PW384^MNN^LL1260^LH0,0^FX ---1^FO40,40^GB304,100,3^FS^CF8,40^FO80,60^FDVIOLATION^FS^CF0,10^FO60,110^FB264,1,1,C,0^FDYOU ARE IN VIOLATION OF PARKING RULES/LAWS^FS^FX ---2 ^FO80,160^FDCITATION ID#^FS ^FO240,160^FDOTHER ID#^FS^FO40,180^FB140,1,1,C,0^FDID 1606^FS^FO192,180^FB140,1,1,C,0^FDOT 1786^FS^FO192,150^GB1,50,1^FS ^FO20,210^GB344,1,1^FS^FX ---3 ^FO80,230^FB224,1,1,C,0^FDSPECIFIC VIOLATION(S)^FS^FO30,260^FDMORE THAN 12" FROM CURB^FS^FO30,290^FDASSIGNED PARKING SPACE^FS^FO30,320^FDBLOCKING GARAGE^FS^FO30,350^FDEXPIRED TAGS^FS^FO30,380^FDEXCESSIVE USE OF GUEST PARKING^FS^FO30,410^FDEXPIRED REGISTRATION^FS^FO20,440^GB344,1,1^FS^FX ---4 ^FO80,460^FB224,1,1,C,0^FDVEHICLE INFORMATION^FS^FO110,490^FDMAKE:   Make^FS^FO109,520^FDMODE:   Model^FS^FO107,550^FDPLATE:   Plate^FS^FO103,580^FDCOLOR:   Color^FS^FO106,610^FDSTATE:   State^FS^FO78,640^FDEXPIRATION:   Expire^FS^FO20,670^GB344,1,1^FS^FX ---5 ^FO80,690^FB224,1,1,C,0^FDPROPERTY^FS^FO70,720^FD4 Doan tests^FS^FO20,750^GB344,1,1^FS^FX ---6 ^FO80,770^FB224,1,1,C,0^FDLOCATION / COMMENTS^FS^FO30,800^FB324,2,1,B,0^FDThis is note content, we need to notice this issues I commented here.

      Thanks.^FS^FO20,830^GB344,1,1^FS^FX ---7 ^FO80,850^FB224,1,1,C,0^FDVEHICLE STATUS^FS^FO70,880^FDPAY FINE SHOWN IN COMMENT BOX^FS^FO20,910^GB344,1,1^FS^FX ---8 ^FO80,930^FB224,1,1,C,0^FDTOW INFORMATION^FS^FO63,960^FDTOW COMPANY:  Tow Company^FS^FO68,990^FDTOW PHONE #:   Tow Phone^FS^FO90,1020^FDCLEARED:   Operator^FS^FO94,1050^FDAGENCY:   Agency^FS^FX ---9 ^FO20,1070^GB344,80,1^FS^FO80,1090^FB224,1,1,C,0^FDDATE / TIME OF VIOLATION^FS^FO80,1120^FB224,1,1,C,0^FD2015-08-08 11:31:54^FS^FX ---10 ^FO88,1170^FDOFFICER:   DOAN^FS^FO143,1200^FDDPS TESTING ACCOUNT^FS^FO143,1230^FD(949) 342-4334^FS^XZ

       

      2. Print Button click

      - (IBAction)buttonClick:(id)sender {

                if (_printString) {

                      [NSThread detachNewThreadSelector:@selector(performReportPrinting) toTarget:self withObject:nil];

                  }

      }

       

      3. performReportPrinting function

      - (void)performReportPrinting

      {

          BOOL completed = NO;

          @autoreleasepool {

              _lblStatus.text = [NSString stringWithFormat:@"Status: Connect printer:%@",_printerSerialNumber];

              id<ZebraPrinterConnection, NSObject> connection = [[MfiBtPrinterConnection alloc] initWithSerialNumber:_printerSerialNumber];

              BOOL didOpen = [connection open];

              if(didOpen == YES) {

                  NSError *error = nil;

                  id<ZebraPrinter,NSObject> printer = [ZebraPrinterFactory getInstance:connection error:&error];

                 

                  if(printer != nil) {

                      PrinterLanguage language = [printer getPrinterControlLanguage];

                      if(language == PRINTER_LANGUAGE_CPCL) {

                          _lblStatus.text = @"Status: No support CPCL printers";

                      } else {

                          _lblStatus.text = @"Status: Printing...";

                          [self printReportAsOneJobUsingNSString:printer withString:[_printString mutableCopy]];

                          _lblStatus.text = @"Status: Print completed.";

                         

                          completed = YES;

                      }

                     

                  } else {

                      _lblStatus.text = [NSString stringWithFormat:@"No Printer Language: %@", _printerSerialNumber];

                  }

                 

                 

              } else {

                  _lblStatus.text = [NSString stringWithFormat:@"Status: Not connect printer:%@",_printerSerialNumber];

              }

             

              [connection close];

              _btnPrint.enabled = YES;

          }

         

          if (completed) {

              [self hide];

          }

      }

       

      4. printReportAsOneJobUsingNSString function

      I copy this function from Zebra SDK Demo app.

      -(BOOL)printReportAsOneJobUsingNSString:(id<NSObject,ZebraPrinter>)printer withString:(NSMutableString*)fullLabel {

          /*

           Sending large amounts of data in a single write command can overflow the NSStream buffers which are the underlying mechanism used by the SDK to communicate with the printers.

           This method shows one way to break up large strings into smaller chunks to send to the printer

           */

          NSError *error = nil;

         

          long blockSize = 256;

          long totalSize = fullLabel.length;

          long bytesRemaining = totalSize;

         

          while (bytesRemaining > 0) {

              long bytesToSend = MIN(blockSize, bytesRemaining);

              NSRange range = NSMakeRange(0, bytesToSend);

             

              NSString *partialLabel = [fullLabel substringWithRange:range];

             

              [[printer getToolsUtil] sendCommand:partialLabel error:&error];

             

              bytesRemaining -= bytesToSend;

             

              [fullLabel deleteCharactersInRange:range];

             

          }

         

          return (error == nil);

      }

       

      Please help me, I did face this issue for 2 weeks.

       

      Thanks a lot!

        • Re: Print on iMZ320 with iOS SDK (Objective C)

          Hello Nguyen,

           

          What version of the SDK are you using?

           

          If it is the latest, then the first thing I would like to have you do is to make a change to a timeout value. When using the newest version of the Link-OS™ Multiplatform SDK for iOS, a few developers have reported being unable to print images and/or text. Please change the timeout on the MFi Bluetooth connection method setTimeToWaitAfterWriteInMilliseconds, from 10ms to a larger value like 30ms or higher to resolve the issue. This article has an example on how to make that change. Please make the change and re-test and let me know if it prints or if the problem persists.

           

          If you are not using the latest SDK, please download it here, make the timeout change, re-test it and let me know the results.

           

          Thank you,

          Beverly

          • Re: Print on iMZ320 with iOS SDK (Objective C)
            Nguyen Doan

            Beverly Vinson,

             

            Can you help me one more thing, please!

             

            Now, I want to print a image within above printing paper.

            I know I must use bellow function:

            if (_printImage) {

                    id<GraphicsUtil, NSObject> graphicsUtil = [printer getGraphicsUtil];

                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                        NSError *error = nil;

                        [graphicsUtil printImage:[_printImage CGImage] atX:_posX atY:_posY withWidth:_printImage.size.width withHeight:_printImage.size.height andIsInsideFormat:NO error:&error];

                       

                    });

                }

            but I don't know how I connect this image with above printing string and where I can put Image print code in function

            -(BOOL)printReportAsOneJobUsingNSString:(id<NSObject,ZebraPrinter>)printer withString:(NSMutableString*)fullLabel{...}

             

            and do I need change setTimeToWaitAfterWriteInMilliseconds to 60 or 10 etc...

             

            Can you give some advises, please?

             

            Thank you so much!!!!