3 Replies Latest reply on Jan 17, 2018 9:56 PM by John Ferlazzo

    Code no longer works using Xamarin LinkOS to print to ZQ510 via Bluetooth on iPad

    Kevin Snow

      Hello,

       

      We have a body of code that is running in the following environment:

       

      • C# / Xamarin / LinkOS_Xaramin_SDK version 1.1.75
      • Xcode 9.2
      • iPad running iOS 11.2.1
      • Printing via bluetooth to ZEBRA ZQ510

       

      The method below deployed to a half-dozen developer iPads and was working from all of them up until a couple of weeks ago.  In the code below we try to access "printer.CurrentStatus" we are now getting the error "Error getting status: Malformed status response - unable to determine printer status"

       

      That line previously worked but if we skip over that section and just go to connection.Write the code executes with-out error but it never prints (it used to print)

       

             // Before call this method we use DiscoveryHandlerFactory to discover a printer and use the following method to print to the connection. 

            // The attached screen shots show the connection object when we get to this method

              public void SendPrintCommandToBluetoothPrinterIfConnectedAndReadyToPrint(IConnection connection, string zplCommand)

              {

                  try

                  {

                      if (!connection.IsConnected)

                          connection.Open();

       

                      var printer = LinkOS.Plugin.ZebraPrinterFactory.Current.GetInstance(LinkOS.Plugin.Abstractions.PrinterLanguage.ZPL, connection);

       

                       // The line below now yields the error "Error getting status: Malformed status response - unable to determine printer status"

                      if (!printer.CurrentStatus.IsReadyToPrint)

                      {

                          var message = $"Printer is not ready for print.  Printer status is currently set to: {printer.CurrentStatus.Status}";

                          _logger.Warn(message);

                      }

                      else

                      {

                           // Bluetooth communications must be handled on a separate thread and it's

                           // best practice to handle network coms on it as well

                           // If I skip the line that reads "printer.CurrentStatus.IsReadyToPrint" then I get to the line below and it executes with-out error but the printer does not print

                          // (I have tried with and with-out Task.Factory)

                           Task.Factory.StartNew(() => connection.Write(Encoding.ASCII.GetBytes(zplCommand)));

       

                           connection.Close();

                      }

                  }

                  catch (Exception e)

                  {

                      _logger.Error($"An error occurred while trying to send the following command to the printer: {zplCommand}", e);

                  }

                  finally

                  {

                      connection?.Close();

                  }

              }

       

      I attached screen shots showing the connection object and also the exception itself.

       

      Also on the iPad I downloaded Zebra Utilities app and was able to connect to and print to the printer so I believe the printer is working.

       

      I also tried resetting the settings and re-configuring bluetooth with the following commands (which I can send via usb on pc)

      ! U1 do "device.restore_defaults" "bluetooth"

      ! U1 do "device.restore_defaults" "ip"

      ! U1 do "device.restore_defaults" "wlan"

      ! U1 do "device.restore_defaults" "power"

      ! U1 do "device.restore_defaults" "display"

       

      ! U1 setvar "bluetooth.enable" "on"

      ! U1 setvar "bluetooth.friendly_name" "BluePrinter"

      ! U1 setvar "bluetooth.discoverable" "on"

      ! U1 setvar "bluetooth.minimum_security_mode" "1"

      ! U1 setvar "bluetooth.bonding" "on"

      ! U1 setvar "bluetooth.enable_reconnect" "iOS_only"

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

      ! U1 setvar "bluetooth.le.minimum_security" "none"

      ! U1 do "device.reset" ""

       

       

      I wanted to see if anyone knew of any recents changes that would have broken things or if I'm printing incorrectly and this shouldn't have worked to begin with?

       

      Thanks in advance for any and all help you can give.

        • Re: Code no longer works using Xamarin LinkOS to print to ZQ510 via Bluetooth on iPad
          Kevin Snow

          Below is just another perspective.  I tried to get all of the printer related code in one view.  Again, everything used to work and I can successfully connect to the printer.  Getting the printer status or trying to print now results in the error "Error getting status: Malformed status response - unable to determine printer status"  Unfortunately delivery of the product is blocked until we can get this working again.

           

           

                     TestPrinnerButton.TouchUpInside += (object sender, EventArgs e) =>

                      {

                          var bthandler = DiscoveryHandlerFactory.Current.GetInstance();

                          //bthandler.OnDiscoveryError += (IDiscoveryHandler handler, string message) => { error(message); };

                          //bthandler.OnDiscoveryFinished += (IDiscoveryHandler handler) => { error("Could not find a connected Zebra bluetooth printer."); };

                          bthandler.OnFoundPrinter += (IDiscoveryHandler handler, IDiscoveredPrinter discoveredPrinter) =>

                          {

                              var connection = discoveredPrinter.Connection;

           

                              try

                              {

                                  //I have tried increasing all the time-outs

                                  //connection.TimeToWaitAfterWrite = 30000;

                                  //connection.TimeToWaitAfterRead = 30000;

                                  //connection.MaxTimeoutForRead = 30000;

           

                                  if (!connection.IsConnected)

                                      connection.Open();

           

                                  var printer = LinkOS.Plugin.ZebraPrinterFactory.Current.GetInstance(LinkOS.Plugin.Abstractions.PrinterLanguage.ZPL, connection);

           

                                  // this works and instantly returns ZPL

                                  var printerLanguage = printer.PrinterControlLanguage;

           

                                  // throws exception: "Error getting status: Malformed status response - unable to determine printer status"

                                  var printerStatus = printer.CurrentStatus;

           

                                  // If I comment out the line above then we can reach below

                                  Task.Factory.StartNew(() =>

                                  {

                                      // throws exception: "Error getting status: Malformed status response - unable to determine printer status"

                                      connection.Write(Encoding.ASCII.GetBytes(ServiceConstants.ZplCommandCalibratePrinterForLabels));

                                  }

                                );

                             }

                              catch(Exception exc)

                              {

                                  throw exc;

                              }

                              finally

                              {

                                  connection?.Close();

                              }

                          };

           

                          BluetoothDiscoverer.Current.FindPrinters(null, bthandler);

                      };

            • Re: Code no longer works using Xamarin LinkOS to print to ZQ510 via Bluetooth on iPad
              John Ferlazzo

              Hi Kevin,

               

              I have not updated my iPad but I have been printing on ZQ510 from my iPhone running 11.2.2. Have you tried on an iPhone?

              What SDK are you building your app against?

               

              The difference between my code and yours is that I am getting the Zebra printer instance via the following code (not using the method that takes in the Printer Language enum).

               

              var printer = ZebraPrinterFactory.Current.GetInstance(CurrentConnection);

               

              I am also ensuring that I set the printer language to ZPL before anything else on the first connection. It could be that your printer language is not set to ZPL? I believe I read somewhere once that the ZQ500 series is not set to ZPL by default.

               

              I run the following code to set and check the printer language:

               

              CurrentConnection.Open();

               

              // Set the Printer Language to ZPL

              var languageBytesResponse = CurrentConnection.SendAndWaitForResponse(Encoding.UTF8.GetBytes(

                                          "! U1 setvar \"device.languages\" \"zpl\"\r\n\r\n! U1 getvar \"device.languages\"\r\n\r\n"),

                                      1000, 1000);

               

              // This should contain something like "hybrid_zpl"

              var stringLanguageResponse = Encoding.UTF8.GetString(languageBytesResponse);

               

              Also maybe just double check that you have this in your Info.plist file:

               

              <key>NSBluetoothPeripheralUsageDescription</key>

              <string>This app needs access to bluetooth for printing.</string>

              <key>UIBackgroundModes</key>

              <array>

                 <string>external-accessory</string>

              </array>

              <key>UISupportedExternalAccessoryProtocols</key>

              <array>

                 <string>com.zebra.rawport</string>

              </array>

               

              Hope this helps.

                • Re: Code no longer works using Xamarin LinkOS to print to ZQ510 via Bluetooth on iPad
                  John Ferlazzo

                  Hi Kevin,

                   

                  I should also add that you could double check and ensure everything is running on a background thread.

                  I open my connections, check printer status and write to printer on the same task.

                   

                              await Task.Run(() =>

                              {

                                  try

                                  {

                                      CurrentConnection.Open();

                   

                                      if (CurrentConnection.IsConnected)

                                      {

                                           // GET PRINTER INSTANCE AND STATUS

                                           var printer = ZebraPrinterFactory.Current.GetInstance(CurrentConnection);

                                           var status = printer.CurrentStatus

                   

                                           // PRE-PRINT CHECK AND HANDLE ANY PRINTER STATUS ISSUES HERE

                   

                                           // WRITE ZPL DATA TO PRINTER

                                           CurrentConnection.Write(Encoding.UTF8.GetBytes(zplData));

                   

                                           // POST-PRINT CHECK AND HANDLE ANY PRINTER STATUS ISSUES HERE

                                      }

                                      else

                                      {

                                          // HANDLE ANY ISSUES WITH OPENING CONNECTION

                                      }

                                  }

                                  catch (Exception)

                                  {

                                      // CLEAR PRINTER BUFFER IF NEEDED

                                      // CHECK PRINTER STATUS AND HANDLE ERRORS

                                  }

                                  finally

                                  {

                                      CurrentConnection.Close();                 

                                  }

                              });