Blame view

src/com/ectrip/cyt/center/PosApiHandle.java 4.44 KB
5acca6a8   杜方   畅游通核销app: 1.补漏上次未上传
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  package com.ectrip.cyt.center;
  
  import android.content.Context;
  import android.os.Build;
  import android.os.Handler;
  import android.posapi.PosApi;
  import android.posapi.PrintQueue;
  import android.widget.Toast;
  
  import com.ectrip.cyt.config.MyApp;
  
  import zyapi.CommonApi;
  
  
  public class PosApiHandle {
  
      private PosApi mPosSDK = null;
      private PrintQueue mPrintQueue = null;
      CommonApi mCommonApi;
      private static PosApiHandle posApiHandle;
      public static PosApiHandle getInstance(){
          if (posApiHandle==null){
              posApiHandle = new PosApiHandle();
          }
          return posApiHandle;
      }
      private int mComFd = -1;
      public PosApiHandle initIDread(){
          mCommonApi = new CommonApi();
          new Handler().postDelayed(new Runnable() {
              @Override
              public void run() {
                  // TODO Auto-generated method stub
                  mCommonApi.setGpioOut(53, 1);
  
                  mCommonApi.setGpioOut(83, 1);
  
                  int ret1 = mCommonApi.setGpioOut(68, 1);
  
                  if (ret1 == 0) {
                      Toast.makeText(MyApp.getInstance(), "设置成功", Toast.LENGTH_SHORT).show();
  
                  } else {
                      Toast.makeText(MyApp.getInstance(), "设置失败", Toast.LENGTH_SHORT).show();
                  }
              }
          }, 1000);
          return posApiHandle;
      }
  
      public PosApiHandle initSdk(Context context){
          // 获取PosApi实例
          mPosSDK = PosApi.getInstance(context);
          mPrintQueue = new PrintQueue(context, mPosSDK);
          // 打印队列初始化
          mPrintQueue.init();
          mPrintQueue.close();
  
          // 根据型号进行初始化mPosApi类
          if (Build.MODEL.contains("LTE")
                  || Build.DISPLAY.contains("3508")
                  || Build.DISPLAY.contains("403")
                  || Build.DISPLAY.contains("35S09")) {
              mPosSDK.initPosDev("ima35s09");
          } else if (Build.MODEL.contains("5501")) {
              mPosSDK.initPosDev("ima35s12");
          } else {
              mPosSDK.initPosDev(PosApi.PRODUCT_MODEL_IMA80M01);
          }
  
          //监听初始化回调结果
          mPosSDK.setOnComEventListener(mCommEventListener);
          return posApiHandle;
      }
  
      public PrintQueue getmPrintQueue() {
          return mPrintQueue;
      }
  
      public PosApi getPosApi(){
          return mPosSDK;
      }
  
      public void closeApi(){
          // 关闭下层串口以及打印机
          if (mPosSDK!=null)
  		mPosSDK.closeDev();
          if (mCommonApi != null) {
              mCommonApi.setGpioDir(83, 1);
              mCommonApi.setGpioOut(83, 0);
              //设置启用GPIO口为53,也就是使GPIO 53口生效
              mCommonApi.setGpioDir(53, 1);
              //拉低53口电压,断开对身份证模块的供电
              mCommonApi.setGpioOut(53, 0);
  //			Toast.makeText(getApplicationContext(), "退出", 0).show();
  //		mCommonApi.setGpioDir(68,1);
  //		mCommonApi.setGpioOut(68,0);
              //关闭mCommonApi类
              mCommonApi.closeCom(mComFd);
          }
  
      }
  
      public void openScan(){
          // 必须延迟一秒,否则将会出现第一次扫描和打印延迟的现象
  		new Handler().postDelayed(new Runnable() {
  			@Override
  			public void run() {
  				// TODO Auto-generated method stub
  				// 打开GPIO,给扫描头上电
  				openDevice();
  
  			}
  		}, 1000);
      }
  
      // 打开串口以及GPIO口
      private byte mGpioPower = 0x1E;// PB14
      private int mCurSerialNo = 3; // usart3
      private int mBaudrate = 4; // 9600
      private void openDevice() {
          // open power
          mPosSDK.gpioControl(mGpioPower, 0, 1);
  
          mPosSDK.extendSerialInit(mCurSerialNo, mBaudrate, 1, 1, 1, 1);
  
      }
  
      /**
       * 初始化
       */
      PosApi.OnCommEventListener mCommEventListener = new PosApi.OnCommEventListener() {
          @Override
          public void onCommState(int cmdFlag, int state, byte[] resp, int respLen) {
              // TODO Auto-generated method stub
              switch (cmdFlag) {
                  case PosApi.POS_INIT:
                      if (state == PosApi.COMM_STATUS_SUCCESS) {
                          Toast.makeText(MyApp.getInstance(), "设备初始化成功",
                                  Toast.LENGTH_SHORT).show();
                      } else {
                          Toast.makeText(MyApp.getInstance(), "设备初始化失败",
                                  Toast.LENGTH_SHORT).show();
                      }
                      break;
              }
          }
      };
  
  
  }