package android_serialport_api.print_tool; import hdx.HdxUtil; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.security.InvalidParameterException; import java.text.SimpleDateFormat; import java.util.Date; import android.annotation.SuppressLint; import android.app.ProgressDialog; import android.content.Context; import android.os.Handler; import android.os.Message; import android.os.PowerManager; import android.os.PowerManager.WakeLock; import android.view.WindowManager; import android.widget.Toast; import android_serialport_api.SerialPort; import com.ectrip.cyt.config.MyApp; import com.ectrip.cyt.constant.DeviceType; import com.ectrip.cyt.constant.LogoType; import com.ectrip.cyt.constant.constant; import com.ectrip.cyt.utils.LogUtil; import com.ectrip.cyt.utils.SharedPreferences2Obj; /** * @author jigo * 新版手持pos机 */ public class SerialNewHandlePortTool { private WakeLock lock; private static final String TAG = "SerialNewPortTool"; private Context context; private final int ENABLE_BUTTON = 2; private SerialPort mSerialPort = null; protected OutputStream mOutputStream; private InputStream mInputStream; protected ReadThread mReadThread; private int n = 0; private int printer_status = 0; private MyHandler handler; private int printNum=1; @SuppressWarnings("deprecation") public void init(Context context,String contentStr) { this.context=context; if(handler==null){ handler = new MyHandler(); } popUpDialog(); try { HdxUtil.SwitchSerialFunction(HdxUtil.SERIAL_FUNCTION_PRINTER); } catch (Throwable e) { e.printStackTrace(); Toast.makeText(context, "不支持", Toast.LENGTH_LONG).show(); return; } PowerManager pm = (PowerManager) context .getSystemService(Context.POWER_SERVICE); lock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); new WriteThread(contentStr).start(); } @SuppressLint("SimpleDateFormat") private class WriteThread extends Thread { private String arr; public WriteThread(String str) { arr = str; } public void run() { super.run(); try { mSerialPort =getSerialPort(); mOutputStream = mSerialPort.getOutputStream(); mInputStream = mSerialPort.getInputStream(); /* Create a receiving thread */ mReadThread = new ReadThread(); mReadThread.start(); } catch (SecurityException e) { // DisplayError(R.string.error_security); } catch (IOException e) { // DisplayError(R.string.error_unknown); } catch (InvalidParameterException e) { // DisplayError(R.string.error_configuration); } HdxUtil.SetPrinterPower(1); lock.acquire(); try { try { sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } sendCommand(0x1B, 0x23, 0x23, 0x53, 0x4C, 0x41, 0x4E, 0x0f); // china sendCommand(0x1B,0x32);//设置行间距 // for(int i=0;i<2;i++){ // sendCharacterDemo(arr); // sendCommand(0x0a); // sendCommand(0x1d, 0x56, 0x30); // try { // sleep(4000); // } catch (InterruptedException e) { // e.printStackTrace(); // } // } for (int x = 0; x < printNum; x++) { if(x==1&&isStatistic.equals("1")){ break; } try { if(isStatistic!=null&&isStatistic.equals("1")){ sendCharacterDemo(" 统计信息"+"\n"); sendCommand(0x1b, 0x33, 0x14, 0x0a); }else{ sendCharacterDemo(" 订单信息"+"\n"); sendCommand(0x1b, 0x33, 0x14, 0x0a); // 当时重打小票的时候,多打一段字符 if (Select== 2) { sendCharacterDemo(" (重打小票)"); sendCommand(0x1b, 0x33, 0x14, 0x0a); } } } catch (Exception e) { e.printStackTrace(); } try { sendCommand(0x1b, 0x33, 0x14, 0x0a); // 间隔 sendCommand(0x1b, 0x33, 0x14, 0x0a); } catch (Exception e) { e.printStackTrace(); } sendCharacterDemo(arr); sendCommand(0x0a); sendCommand(0x1b, 0x33, 0x14, 0x0a); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } if(isStatistic!=null&&isStatistic.equals("1")){ }else{ // 重打时间 // if (Select== 2) { // SimpleDateFormat df = new SimpleDateFormat( // "yyyy-MM-dd HH:mm:ss");// 设置日期格式 // sendCharacterDemo("重打时间:" + df.format(new Date())); // sendCommand(0x1b, 0x33, 0x14, 0x0a); // } sendCommand(0x1b, 0x33, 0x14, 0x0a); sendCharacterDemo("取票人签名:"); } sendCommand(0x1b, 0x33, 0x14, 0x0a); sendCommand(0x1b, 0x33, 0x14, 0x0a); if (constant.logoMark == LogoType.CYT.getValue()) { sendCharacterDemo("---畅游通—智慧旅游O2O平台---"); sendCommand(0x1b, 0x33, 0x14, 0x0a); sendCommand(0x1b, 0x33, 0x14, 0x0a); sendCharacterDemo("--------www.jingqu.cn---------"); } else if (constant.logoMark ==LogoType.QNE.getValue()) { sendCommand(0x1b, 0x33, 0x14, 0x0a); sendCommand(0x1b, 0x33, 0x14, 0x0a); sendCharacterDemo("---欢迎使用去哪儿网O2O系统---"); sendCommand(0x1b, 0x33, 0x14, 0x0a); sendCommand(0x1b, 0x33, 0x14, 0x0a); sendCharacterDemo("--------www.qunar.com--------"); } sendCharacterDemo("\n"); sendCharacterDemo("\n"); sendCharacterDemo("\n"); sendCharacterDemo("\n"); sendCommand(0x1b, 0x33, 0x14, 0x0a); sendCommand(0x1b, 0x33, 0x14, 0x0a); sendCommand(0x1b, 0x33, 0x14, 0x0a); sendCommand(0x1b, 0x33, 0x14, 0x0a); sendCommand(0x1b, 0x33, 0x14, 0x0a); try { sleep(2500); if (isStatistic.equals("1")){ sleep(1500); } } catch (InterruptedException e) { e.printStackTrace(); } } } finally { lock.release(); //HdxUtil.SetPrinterPower(0); } handler.sendMessage(handler .obtainMessage(ENABLE_BUTTON, 1, 0, null)); } } private void sendCharacterDemo(String arr) { synchronized (MyApp.getInstance()) { try { mOutputStream.write(arr.getBytes("GBK")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } @SuppressLint("HandlerLeak") private class MyHandler extends Handler { public void handleMessage(Message msg) { switch (msg.what) { case ENABLE_BUTTON: close(); break; default: break; } } } class ReadThread extends Thread { @Override public void run() { super.run(); while(!isInterrupted()) { int size; try { byte[] buffer = new byte[64]; if (mInputStream == null) return; size = mInputStream.read(buffer); if (size > 0) { onDataReceived(buffer, size,n); } } catch (IOException e) { e.printStackTrace(); return; } } } } protected void onDataReceived(final byte[] buffer, final int size, final int n) { printer_status = buffer[0]; LogUtil.e(TAG, "onDataReceived= " + printer_status); } private void sendCommand(int... command) { try { for (int i = 0; i < command.length; i++) { mOutputStream.write(command[i]); // Log.e(TAG,"command["+i+"] = "+Integer.toHexString(command[i])); } } catch (IOException e) { e.printStackTrace(); } // / sleep(1); } public void sleep(int ms) { try { java.lang.Thread.sleep(ms); } catch (Exception e) { e.printStackTrace(); } } public SerialPort getSerialPort() throws SecurityException, IOException, InvalidParameterException { if (mSerialPort == null) { /* Read serial port parameters */ String path = "/dev/ttyS1"; ;// sp.getString("DEVICE", ""); int baudrate = 115200;// Integer.decode(sp.getString("BAUDRATE", // "-1")); /* Check parameters */ if ((path.length() == 0) || (baudrate == -1)) { // throw new InvalidParameterException(); /* use default value. Nirvana 0710 */ path = "/dev/ttyS1"; baudrate = 115200; } /* Open the serial port */ mSerialPort = new SerialPort(new File(path), baudrate, 0); } return mSerialPort; } public void closeSerialPort() { if (mSerialPort != null) { mSerialPort.close(); mSerialPort = null; } } public void close(){ try { if (mReadThread != null) mReadThread.interrupt(); } catch (Exception e1) { e1.printStackTrace(); } try { mSerialPort = null; } catch (Exception e1) { e1.printStackTrace(); } try { mOutputStream.close(); mInputStream.close(); } catch (IOException e) { }catch (Exception e) { } if(dialog!=null){ dialog.dismiss(); } } private ProgressDialog dialog; private String isStatistic; private Integer type; private int Select = 0; private void popUpDialog(){ try { isStatistic=SharedPreferences2Obj.getInstance(context). setName("SelectAction").getObject("isStatistic",String.class); //非统计判断 type=SharedPreferences2Obj.getInstance(context). setName("MachineType").getObject("type", Integer.class); Select=SharedPreferences2Obj.getInstance(context). setName("SelectAction").getObject("Select",Integer.class); printNum = MyApp.getInstance().getPrintNum(); } catch (Exception e1) { e1.printStackTrace(); } if (dialog == null) { // 显示ProgressDialog dialog = new ProgressDialog(context); dialog.setMessage("打印中..."); dialog.setCanceledOnTouchOutside(false); dialog.setCancelable(false); if (type==DeviceType.HANDSET.getValue()) { dialog.getWindow() .setType( WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); } try { dialog.show(); } catch (Exception e) { e.printStackTrace(); } }else{ dialog.setMessage("打印中..."); dialog.show(); } } }