Advertisement
morty346

Untitled

Apr 21st, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.15 KB | None | 0 0
  1. package com.sm.trackit;
  2.  
  3. import android.graphics.Bitmap;
  4.  
  5. import com.zebra.sdk.comm.BluetoothConnection;
  6. import com.zebra.sdk.comm.Connection;
  7. import com.zebra.sdk.comm.ConnectionException;
  8.  
  9. import com.zebra.sdk.graphics.internal.ZebraImageAndroid;
  10. import com.zebra.sdk.printer.PrinterStatus;
  11. import com.zebra.sdk.printer.ZebraPrinter;
  12. import com.zebra.sdk.printer.ZebraPrinterFactory;
  13. import com.zebra.sdk.printer.ZebraPrinterLanguageUnknownException;
  14.  
  15. import java.io.IOException;
  16. import java.text.SimpleDateFormat;
  17. import java.util.Date;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20.  
  21.  
  22. /**
  23.  * Created by gregm on 4/14/2016.
  24.  */
  25. public class BTPrinter {
  26.  
  27.     String custName,streetnum,custadd,custcity,custzip,custemail,custphone,eBill,fdos,colspin,cartsize,recyItem,yarspin;
  28.     void SetVariables(String _custName, String _streetnum , String _custadd, String _custcity ,String _custzip, String _custemail,String  _custphone,String _eBill, String _fdos , String _colspin , String _cartsize ,String _recyItem , String _yarspin)
  29.     {
  30.         custName = _custName;
  31.         streetnum= _streetnum;
  32.         custadd= _custadd;
  33.         custcity= _custcity;
  34.         custzip= _custzip;
  35.         custemail= _custemail;
  36.         custphone= _custphone;
  37.         eBill= _eBill;
  38.         fdos= _fdos;
  39.         colspin= _colspin;
  40.         cartsize= _cartsize;
  41.         recyItem= _recyItem;
  42.         yarspin= _yarspin;
  43.     }
  44.  
  45.     ZebraPrinter printer;
  46.     Connection connection;
  47.     void Setup()
  48.     {
  49.         try {
  50.  
  51.         String hardcodedmac = "ac3fa446767d";
  52.         connection = new BluetoothConnection(hardcodedmac);
  53.         connection.open();
  54.          printer = ZebraPrinterFactory.getInstance(connection);
  55.  
  56.         } catch (ConnectionException e) {
  57.             e.printStackTrace();
  58.         }
  59.         catch (ZebraPrinterLanguageUnknownException e) {
  60.             e.printStackTrace();
  61.  
  62.         }
  63.     }
  64.     void CloseDown() {
  65.         try {
  66.  
  67.             connection.close();
  68.  
  69.         } catch (ConnectionException e) {
  70.             e.printStackTrace();
  71.         }
  72.     }
  73.     void ConnectAndPrint(final MainActivity ma, final Bitmap sigImage,final Bitmap canImage)
  74.     {
  75.         new Thread(new Runnable() {
  76.             public void run() {
  77.                 boolean bPrint = true;
  78.                 int counter = 0;
  79.                 int maxtime = 10;
  80.                 int step = 0;
  81.  
  82.                 while (bPrint) {
  83.                     step++;
  84.                     System.out.println("step:" + step);
  85.                     switch (step) {
  86.                         case 1:
  87.                             new Thread(new Runnable() {
  88.                                 public void run() {
  89.                                     try {
  90.                                         printer.printImage(new ZebraImageAndroid(sigImage), 0, 0, sigImage.getWidth() / 4, sigImage.getHeight() / 4, false);
  91.                                     } catch (ConnectionException e) {
  92.                                         e.printStackTrace();
  93.                                     }
  94.                                 }
  95.                             }).run();
  96.                             break;
  97.                         case 2:
  98.  
  99.                             System.out.println("counter:" + counter);
  100.                             //waiting 10 seconds
  101.                             try {
  102.                                 Thread.sleep(1000);
  103.                             } catch (InterruptedException e) {
  104.                                 e.printStackTrace();
  105.                             }
  106.                             counter++;
  107.                             if (counter > maxtime) {
  108.                                 counter = 0;
  109.                                 System.out.println("counterMAX" + counter);
  110.                                 //  step++;
  111.                             } else
  112.                                 step--; //reset it back to us
  113.                             break;
  114.  
  115.                         case 3:
  116.                             new Thread(new Runnable() {
  117.                                 public void run() {
  118.                                     sendTestLabelWithManyJobs(connection, ma);
  119.  
  120.                                 }
  121.                             }).run();
  122.                             break;
  123.                         case 4:
  124.  
  125.                             System.out.println("counter:" + counter);
  126.                             //waiting 10 seconds
  127.                             try {
  128.                                 Thread.sleep(1000);
  129.                             } catch (InterruptedException e) {
  130.                                 e.printStackTrace();
  131.                             }
  132.                             counter++;
  133.                             if (counter > maxtime) {
  134.                                 counter = 0;
  135.                                 System.out.println("counterMAX" + counter);
  136.                                 //  step++;
  137.                             } else
  138.                                 step--; //reset it back to us
  139.                             break;
  140.                         case 5:
  141.                             bPrint = false;
  142.                             break;
  143.  
  144.                     }
  145.                 }
  146.             }
  147.         }).run();
  148.  
  149.     }
  150.     private void sendTestLabelWithManyJobs(Connection printerConnection,MainActivity ma) {
  151.         try {
  152.             sendZplReceipt(printerConnection);
  153.         } catch (ConnectionException e) {
  154.             e.printStackTrace();
  155.             ma.ShowToastMsg(e.getMessage());
  156.         }
  157.     }
  158.     private Map<String, String> createListOfItems() {
  159.  
  160.         String[] names = { "Name", "Address", "City", "Zip Code", "Phone Number","E-Mail Address"," Size"," Size"," Var", " Day", "Start Date",  };
  161.  
  162.         String add = streetnum + custadd;
  163.         String[] prices = { custName, add, custcity, custzip, custphone,custemail, cartsize, recyItem, yarspin, "Tuesday", fdos};
  164.         Map<String, String> retVal = new HashMap<String, String>();
  165.  
  166.         for (int ix = 0; ix < names.length; ix++) {
  167.             retVal.put(names[ix], prices[ix]);
  168.         }
  169.         return retVal;
  170.     }
  171.     private void sendZplReceipt(Connection printerConnection) throws ConnectionException {
  172.         String tmpHeader =
  173.                 "^XA" +
  174.                         "^POI^PW400^MNN^LL325^LH0,0" + "\r\n" +
  175.  
  176.                         "^FO50,50" + "\r\n" + "^A0,N,70,70" + "\r\n" + "^FD Shipping^FS" + "\r\n" +
  177.  
  178.                         "^FO50,130" + "\r\n" + "^A0,N,35,35" + "\r\n" + "^FDPurchase Confirmation^FS" + "\r\n" +
  179.  
  180.                         "^FO50,180" + "\r\n" + "^A0,N,25,25" + "\r\n" + "^FDCustomer:^FS" + "\r\n" +
  181.  
  182.                         "^FO225,180" + "\r\n" + "^A0,N,25,25" + "\r\n" + "^FDAcme Industries^FS" + "\r\n" +
  183.  
  184.                         "^FO50,220" + "\r\n" + "^A0,N,25,25" + "\r\n" + "^FDDelivery Date:^FS" + "\r\n" +
  185.  
  186.                         "^FO225,220" + "\r\n" + "^A0,N,25,25" + "\r\n" + "^FD%s^FS" + "\r\n" +
  187.  
  188.                         "^FO50,273" + "\r\n" + "^A0,N,30,30" + "\r\n" + "^FDItem^FS" + "\r\n" +
  189.  
  190.                         "^FO280,273" + "\r\n" + "^A0,N,25,25" + "\r\n" + "^FDPrice^FS" + "\r\n" +
  191.  
  192.                         "^FO50,300" + "\r\n" + "^GB350,5,5,B,0^FS" + "^XZ";
  193.  
  194.         int headerHeight = 150;
  195.  
  196.         Date date = new Date();
  197.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
  198.         String dateString = sdf.format(date);
  199.  
  200.         String header = String.format(tmpHeader, dateString);
  201.  
  202.         printerConnection.write(header.getBytes());
  203.  
  204.         int heightOfOneLine = 40;
  205.  
  206.         float totalPrice = 0;
  207.  
  208.         Map<String, String> itemsToPrint = createListOfItems();
  209.  
  210.         int i = 0;
  211.         for (String productName : itemsToPrint.keySet()) {
  212.             String price = itemsToPrint.get(productName);
  213.  
  214.             String lineItem = "^XA^POI^LL40" + "^FO50,10" + "\r\n" + "^A0,N,28,28" + "\r\n" + "^FD%s^FS" + "\r\n" + "^FO280,10" + "\r\n" + "^A0,N,28,28" + "\r\n" + "^FD%s^FS" + "^XZ";
  215.             //totalPrice += Float.parseFloat(price);
  216.             String oneLineLabel = String.format(lineItem, productName, price);
  217.  
  218.             printerConnection.write(oneLineLabel.getBytes());
  219.         }
  220.     }
  221.  
  222.  
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement