/// <summary> /// Set Printer Parameters /// </summary> /// <returns>The printer.</returns> /// <param name="address">Address.</param> public static PrintResult SetDefaultPrinterSettings(string address) { var result = new PrintResult(); IConnection connection = null; try { connection = new BluetoothConnectionInsecure(address); connection.Open(); using (var printer = ZebraPrinterFactory.GetInstance(PrinterLanguage.Zpl, connection)) { // set printer to bluetooth classic only printer.SendCommand("! U1 setvar \"bluetooth.le.controller_mode\" \"classic\"" + "\r\n"); // disable wifi printer.SendCommand("! U1 setvar \"wlan.enable\" \"off\"" + "\r\n"); // disable wifi power save printer.SendCommand("! U1 setvar \"wlan.power_save\" \"off\"" + "\r\n"); // disable bonding printer.SendCommand("! U1 setvar \"bluetooth.bonding\" \"off\"" + "\r\n"); // disable sleep mode printer.SendCommand("! U1 setvar \"power.sleep.enable\" \"off\"" + "\r\n"); // never power down due to inactivity printer.SendCommand("! U1 setvar \"power.inactivity_timeout\" \"0\"" + "\r\n"); // set sleep timeout to 8 hours - not sure if sleep.enable = off overrides this? printer.SendCommand("! U1 setvar \"power.sleep.timeout\" \"28000\"" + "\r\n"); } result.Status = PrintStatus.Success; return(result); } catch (Exception exception) { Microsoft.AppCenter.Crashes.Crashes.TrackError(exception); result.Message = "Print failed. Check the printer is switched on and try again."; result.Status = PrintStatus.PrintException; return(result); } finally { Thread.Sleep(SignaturePauseMilliseconds); connection.Close(); } }
/// <summary> /// Print content /// </summary> /// <returns>The print.</returns> /// <param name="content">Content.</param> public static PrintResult Print(string content, string address, string signature) { var result = new PrintResult { Status = PrintStatus.PrintFailed }; IConnection connection = null; try { connection = new BluetoothConnectionInsecure(address); connection.Open(); var printer = ZebraPrinterFactory.GetInstance(PrinterLanguage.Cpcl, connection); // if there is a signature store it if (signature != null) { using (var image = ZebraImageFactory.GetImage(signature)) { printer.StoreImage(SignatureFilename, image, image.Width / 4, image.Height / 4); } Thread.Sleep(SignaturePauseMilliseconds); } // write the label var printLabel = ConvertExtendedAscii(content); connection.SendAndWaitForResponse(printLabel, InitialiseResponseTimeoutMilliseconds, ResponseCompletionTimeoutMilliseconds, null); var status = printer.CurrentStatus; while (status.NumberOfFormatsInReceiveBuffer > 0 && status.IsReadyToPrint) { Thread.Sleep(500); status = printer.CurrentStatus; } if (!status.IsReadyToPrint) { LogPrinterFailure(result, status); } result.Status = PrintStatus.Success; printer.SendCommand("! U1 setvar \"formats.cancel_all\" \"\"" + "\r\n"); return(result); } catch (Exception exception) { var context = Application.Context; var message = context.Resources.GetString(Resource.String.message_print_failed_continue); Microsoft.AppCenter.Crashes.Crashes.TrackError(exception); result.Message = message; result.Status = PrintStatus.PrintException; return(result); } finally { connection.Close(); } }