Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sm.trackit;
- import android.graphics.Bitmap;
- import com.zebra.sdk.comm.BluetoothConnection;
- import com.zebra.sdk.comm.Connection;
- import com.zebra.sdk.comm.ConnectionException;
- import com.zebra.sdk.graphics.internal.ZebraImageAndroid;
- import com.zebra.sdk.printer.PrinterStatus;
- import com.zebra.sdk.printer.ZebraPrinter;
- import com.zebra.sdk.printer.ZebraPrinterFactory;
- import com.zebra.sdk.printer.ZebraPrinterLanguageUnknownException;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * Created by gregm on 4/14/2016.
- */
- public class BTPrinter {
- String custName,streetnum,custadd,custcity,custzip,custemail,custphone,eBill,fdos,colspin,cartsize,recyItem,yarspin;
- 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)
- {
- custName = _custName;
- streetnum= _streetnum;
- custadd= _custadd;
- custcity= _custcity;
- custzip= _custzip;
- custemail= _custemail;
- custphone= _custphone;
- eBill= _eBill;
- fdos= _fdos;
- colspin= _colspin;
- cartsize= _cartsize;
- recyItem= _recyItem;
- yarspin= _yarspin;
- }
- ZebraPrinter printer;
- Connection connection;
- void Setup()
- {
- try {
- String hardcodedmac = "ac3fa446767d";
- connection = new BluetoothConnection(hardcodedmac);
- connection.open();
- printer = ZebraPrinterFactory.getInstance(connection);
- } catch (ConnectionException e) {
- e.printStackTrace();
- }
- catch (ZebraPrinterLanguageUnknownException e) {
- e.printStackTrace();
- }
- }
- void CloseDown() {
- try {
- connection.close();
- } catch (ConnectionException e) {
- e.printStackTrace();
- }
- }
- void ConnectAndPrint(final MainActivity ma, final Bitmap sigImage,final Bitmap canImage)
- {
- new Thread(new Runnable() {
- public void run() {
- boolean bPrint = true;
- int counter = 0;
- int maxtime = 10;
- int step = 0;
- while (bPrint) {
- step++;
- System.out.println("step:" + step);
- switch (step) {
- case 1:
- new Thread(new Runnable() {
- public void run() {
- try {
- printer.printImage(new ZebraImageAndroid(sigImage), 0, 0, sigImage.getWidth() / 4, sigImage.getHeight() / 4, false);
- } catch (ConnectionException e) {
- e.printStackTrace();
- }
- }
- }).run();
- break;
- case 2:
- System.out.println("counter:" + counter);
- //waiting 10 seconds
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- counter++;
- if (counter > maxtime) {
- counter = 0;
- System.out.println("counterMAX" + counter);
- // step++;
- } else
- step--; //reset it back to us
- break;
- case 3:
- new Thread(new Runnable() {
- public void run() {
- sendTestLabelWithManyJobs(connection, ma);
- }
- }).run();
- break;
- case 4:
- System.out.println("counter:" + counter);
- //waiting 10 seconds
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- counter++;
- if (counter > maxtime) {
- counter = 0;
- System.out.println("counterMAX" + counter);
- // step++;
- } else
- step--; //reset it back to us
- break;
- case 5:
- bPrint = false;
- break;
- }
- }
- }
- }).run();
- }
- private void sendTestLabelWithManyJobs(Connection printerConnection,MainActivity ma) {
- try {
- sendZplReceipt(printerConnection);
- } catch (ConnectionException e) {
- e.printStackTrace();
- ma.ShowToastMsg(e.getMessage());
- }
- }
- private Map<String, String> createListOfItems() {
- String[] names = { "Name", "Address", "City", "Zip Code", "Phone Number","E-Mail Address"," Size"," Size"," Var", " Day", "Start Date", };
- String add = streetnum + custadd;
- String[] prices = { custName, add, custcity, custzip, custphone,custemail, cartsize, recyItem, yarspin, "Tuesday", fdos};
- Map<String, String> retVal = new HashMap<String, String>();
- for (int ix = 0; ix < names.length; ix++) {
- retVal.put(names[ix], prices[ix]);
- }
- return retVal;
- }
- private void sendZplReceipt(Connection printerConnection) throws ConnectionException {
- String tmpHeader =
- "^XA" +
- "^POI^PW400^MNN^LL325^LH0,0" + "\r\n" +
- "^FO50,50" + "\r\n" + "^A0,N,70,70" + "\r\n" + "^FD Shipping^FS" + "\r\n" +
- "^FO50,130" + "\r\n" + "^A0,N,35,35" + "\r\n" + "^FDPurchase Confirmation^FS" + "\r\n" +
- "^FO50,180" + "\r\n" + "^A0,N,25,25" + "\r\n" + "^FDCustomer:^FS" + "\r\n" +
- "^FO225,180" + "\r\n" + "^A0,N,25,25" + "\r\n" + "^FDAcme Industries^FS" + "\r\n" +
- "^FO50,220" + "\r\n" + "^A0,N,25,25" + "\r\n" + "^FDDelivery Date:^FS" + "\r\n" +
- "^FO225,220" + "\r\n" + "^A0,N,25,25" + "\r\n" + "^FD%s^FS" + "\r\n" +
- "^FO50,273" + "\r\n" + "^A0,N,30,30" + "\r\n" + "^FDItem^FS" + "\r\n" +
- "^FO280,273" + "\r\n" + "^A0,N,25,25" + "\r\n" + "^FDPrice^FS" + "\r\n" +
- "^FO50,300" + "\r\n" + "^GB350,5,5,B,0^FS" + "^XZ";
- int headerHeight = 150;
- Date date = new Date();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
- String dateString = sdf.format(date);
- String header = String.format(tmpHeader, dateString);
- printerConnection.write(header.getBytes());
- int heightOfOneLine = 40;
- float totalPrice = 0;
- Map<String, String> itemsToPrint = createListOfItems();
- int i = 0;
- for (String productName : itemsToPrint.keySet()) {
- String price = itemsToPrint.get(productName);
- 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";
- //totalPrice += Float.parseFloat(price);
- String oneLineLabel = String.format(lineItem, productName, price);
- printerConnection.write(oneLineLabel.getBytes());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement