Blame view

src/com/ectrip/cyt/config/DevicTool.java 16.5 KB
3c2353cd   杜方   1、畅游通核销app源码提交;
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
  package com.ectrip.cyt.config;
  
  import java.io.File;
  import java.io.IOException;
  import java.lang.reflect.Method;
  import java.net.InetAddress;
  import java.net.NetworkInterface;
  import java.security.InvalidParameterException;
  import java.util.ArrayList;
  import java.util.Enumeration;
  
  import com.ectrip.cyt.bean.ConfigBean;
  import com.ectrip.cyt.db.DbManager;
  import com.ectrip.cyt.utils.LogUtil;
  import com.hdx.lib.printer.SerialPrinter;
  import com.hdx.lib.serial.SerialParam;
  import com.hdx.lib.serial.SerialPortOperaion;
  import com.hdx.lib.serial.SerialPortOperaion.SerialReadData;
  
  import android.content.Context;
  import android.content.SharedPreferences;
  import android.content.res.Configuration;
  import android.content.res.Resources.NotFoundException;
  import android.net.wifi.WifiInfo;
  import android.net.wifi.WifiManager;
  import android.os.Build;
  import android.os.Handler;
  import android.os.Message;
  import android.telephony.TelephonyManager;
  import android.text.TextUtils;
  import android.util.DisplayMetrics;
72876d86   杜方   畅游通核销app: 1.增加550...
32
  import android.util.Log;
3c2353cd   杜方   1、畅游通核销app源码提交;
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
  import android.view.Display;
  import android.view.WindowManager;
  
  import javax.crypto.Mac;
  
  import android_serialport_api.SerialPort;
  import android_serialport_api.SerialPortFinderHandset;
  import android_serialport_api.SerialPortHandset;
  import android_serialport_api.SerialPortNewHandset;
  import android_serialport_pos_id_api.SerialIdCardPort;
  
  /**
   * 设备信息工具类,获取设备编号,型号等
   *
   * @author Administrator
   */
  public class DevicTool {
  
      public SerialPortFinderHandset mSerialPortFinder = new SerialPortFinderHandset();
      private SerialPortHandset mSerialPortHandset = null;
      private static DevicTool tool;
      private String devId = "";// 设备号
      private Context context = MyApp.getInstance();
  
  
      private DevicTool() {
          super();
      }
  
      public static DevicTool getInstance() {
          if (tool == null) {
              synchronized (DevicTool.class) {
                  tool = new DevicTool();
              }
          }
          return tool;
      }
  
  
      public String getDevId() {
  
          if (devId.equals("")) {
              devId = getDeviceId();
          } else if (devId.equals("000000000000000")
                  || devId.equals("00:E0:4C:87:00:00") || devId.equals("00:E0:4C:87:00:00")) {
              devId = getDeviceId();
          }
          return devId;
      }
  
      /**
       * @return 获取设备id
       */
      public String getDeviceId() {
          String devId = "";
  
          ArrayList<ConfigBean> beans = DbManager.GetConfigs();
          if (beans != null && beans.size() > 0) {
              devId = beans.get(0).getEc_mac();
              if (!devId.equals("000000000000000")
                      && !devId.equals("02:00:00:00:00:00")) {
                  return devId;
              }
          }
  
          if ((getSerialNumber() != null && getSerialNumber().contains("CYT") && getSerialNumber()
                  .substring(0, 3).equals("CYT"))
                  || (getSerialNumber() != null
                  && getSerialNumber().contains("CTY") && getSerialNumber()
                  .substring(0, 3).equals("CTY"))) {
              devId = getSerialNumber();
          } else if ((getSerialNumber() != null
                  && getSerialNumber().contains("LY") && getSerialNumber()
                  .substring(0, 2).equals("LY"))) {
              devId = getSerialNumber();
          } else if ((getSerialNumber() != null
                  && getSerialNumber().contains("DY") && getSerialNumber()
                  .substring(0, 2).equals("DY"))) {
              devId = getSerialNumber();
          } else {
              if(Build.VERSION.SDK_INT>=28){
                  devId = getLocalMacIdFromIp();
              }else{
                  devId = getLocalMacAddress();
              }
              if (devId.equals("打开wifi才能获取mac")) { // 在获取一遍mac地址
                  devId = getLocalMacAddress();
              }
              if (devId.equals("00:E0:4C:87:00:00")
                      || devId.equals("打开wifi才能获取mac") || devId.equals("02:00:00:00:00:00")) {
                  try {
                      devId = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE))
                              .getDeviceId();
                      if (devId != null && devId.length() > 0) {
                          if (devId.contains("+ESN:")) {
                              devId = devId.replace("+ESN:", "");
                          }
                       }
                      if (devId.length() < 15) {
                          int size = 15 - devId.length();
                          for (int i = 0; i < size; i++) {
                              devId += "0";
                          }
                      }
                      if (devId.equals("000000000000000") || devId.equals("02:00:00:00:00:00")) { // 如果等于0
                          devId = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE))
                                  .getDeviceId();//返回pos机的IMEI/SV码
                          if (devId != null && devId.length() > 0) {
                              if (devId.contains("+ESN:")) {
                                  devId = devId.replace("+ESN:", "");
                              }
                          }
                          if (devId.length() < 15) {
                              int size = 15 - devId.length();
                              for (int i = 0; i < size; i++) {
                                  devId += "0";
                              }
                          }
                          if (devId.equals("000000000000000") || devId.equals("02:00:00:00:00:00")) {
                              devId = android.os.Build.SERIAL; // 2.3之后才有这儿接口
                          }
                      }
                  } catch (Exception e) {
                      e.printStackTrace();
                      devId = android.os.Build.SERIAL; // 2.3之后才有这儿接口
                  }
              }
          }
          return devId;
      }
  
  
      public static String getSerialNumber() {
          String serial = null;
          try {
              Class<?> c = Class.forName("android.os.SystemProperties");
              Method get = c.getMethod("get", String.class);
              serial = (String) get.invoke(c, "ro.serialno");
          } catch (Exception e) {
              e.printStackTrace();
          }
          return serial;
      }
  
      public static String getLocalMacIdFromIp(){
          String strMacAddr = "";
          try {
              InetAddress ip = getLocalInetAddress();
  
              byte[] b = NetworkInterface.getByInetAddress(ip)
                      .getHardwareAddress();
              StringBuffer buffer = new StringBuffer();
              for (int i = 0; i < b.length; i++) {
                  if (i != 0) {
                      buffer.append(':');
                  }
  
                  String str = Integer.toHexString(b[i]&0xFF);
                  buffer.append(str.length() == 1 ? 0 + str : str);
              }
              strMacAddr = buffer.toString().toUpperCase();
          } catch (Exception e) {
              // TODO Auto-generated catch block
              strMacAddr = "打开wifi才能获取mac";
              e.printStackTrace();
          }
          return strMacAddr;
      }
      /**
       * 获取设备本地IP
       */
      protected static InetAddress getLocalInetAddress() {
          InetAddress ip = null;
          try {
              //列举
              Enumeration en_netInterface = NetworkInterface.getNetworkInterfaces();
              while (en_netInterface.hasMoreElements()) {//是否还有元素
                  NetworkInterface ni = (NetworkInterface) en_netInterface.nextElement();//得到下一个元素
                  Enumeration en_ip = ni.getInetAddresses();//得到一个ip地址的列举
                  while (en_ip.hasMoreElements()) {
                      ip = (InetAddress) en_ip.nextElement();
                      if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1)
                          break;
                      else
                          ip = null;
                  }
  
                  if (ip != null) {
                      break;
                  }
              }
          } catch (Exception e) {
  
              e.printStackTrace();
          }
          return ip;
      }
  
      /**
       * @return 打開wifi才能获取mac地址
       */
      public String getLocalMacAddress() {
          WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
          WifiInfo info = wifi.getConnectionInfo();
          String mac = info.getMacAddress();
          if (TextUtils.isEmpty(mac)) {
              if (wifi.isWifiEnabled()) {
                  wifi.setWifiEnabled(false);
              } else {
                  wifi.setWifiEnabled(true);
              }
              return "打开wifi才能获取mac";
          } else {
              return mac.toUpperCase();
          }
      }
  
      /**************************************************************************/
  
      /**
       * 判断当前设备是手机还是平板,代码来自 Google I/O App for Android
       *
       * @param context
       * @return 平板返回 True,手机返回 False
       */
      public boolean isTablet(Context context) {
          return isTabletDevice(context);
      }
  
      private boolean isTabletDevice(Context context) {
          try {
              return ((context.getResources().getConfiguration().screenLayout
                      & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE)
                      && isPad();
          } catch (NotFoundException e) {
              e.printStackTrace();
          }
          return false;
      }
  
      /**
       * 判断是否为平板
       *
       * @return
       */
      private boolean isPad() {
          WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
          Display display = wm.getDefaultDisplay();
          // 屏幕宽度
          // float screenWidth = display.getWidth();
          // 屏幕高度
          // float screenHeight = display.getHeight();
          DisplayMetrics dm = new DisplayMetrics();
          display.getMetrics(dm);
          double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
          double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
          // 屏幕尺寸
          double screenInches = Math.sqrt(x + y);
          // 大于6尺寸则为Pad
          if (screenInches >= 6.5) {
              return true;
          }
          return false;
      }
  
      /* .............................手持机串口............................... */
  
      public SerialPortHandset getSerialPortHandset() throws SecurityException,
              IOException, InvalidParameterException {
          if (mSerialPortHandset == null) {
              String path = "/dev/ttySAC1";
              int baudrate = 9600;
              mSerialPortHandset = new SerialPortHandset(new File(path),
                      baudrate, 0);
          }
          return mSerialPortHandset;
      }
  
      /**
       * 判断二维码读头,根据是否能生成串口对象
       */
      public boolean isSerialPortHandset() {
          try {
              if (getSerialPortHandset() != null) {
                  return true;
              } else {
                  return false;
              }
          } catch (InvalidParameterException e) {
              e.printStackTrace();
              return false;
          } catch (SecurityException e) {
              e.printStackTrace();
              return false;
          } catch (IOException e) {
              e.printStackTrace();
              return false;
          } catch (Exception e) {
              e.printStackTrace();
              return false;
          }
      }
  
      public void closeSerialPortHandset() {
          if (mSerialPortHandset != null) {
              mSerialPortHandset.close();
              mSerialPortHandset = null;
          }
      }
  
      /****************************** 新版手持机 ***************************************/
      private SerialPortNewHandset mSerialPortNewHandset = null;
  
      public SerialPortNewHandset getSerialPortNewHandset()
              throws SecurityException, IOException, InvalidParameterException {
          if (mSerialPortNewHandset == null) {
              String path = "/dev/ttySAC1";
              int baudrate = 9600;
              mSerialPortNewHandset = new SerialPortNewHandset(new File(path),
                      baudrate, 0);
          }
          return mSerialPortNewHandset;
      }
  
      public void closeSerialPortNewHandset() {
          if (mSerialPortNewHandset != null) {
              mSerialPortNewHandset.close();
              mSerialPortNewHandset = null;
          }
      }
  
      /*
       * id pos 打印判断
       */
      public boolean isIdPosPrint() {
          try {
              SerialPrinter mSerialPrinter2 = SerialPrinter.GetSerialPrinter();
              mSerialPrinter2.OpenPrinter(
                      new SerialParam(115200, "/dev/ttyS1", 0),
                      new SerialDataHandler(), null);
              mSerialPrinter2.ClosePrinter();
              return true;
          } catch (Exception e1) {
              e1.printStackTrace();
              return false;
          } catch (Throwable e) {
              e.printStackTrace();
              return false;
          }
      }
  
      private class SerialDataHandler extends Handler {
          public void handleMessage(Message msg) {
              switch (msg.what) {
                  case SerialPortOperaion.SERIAL_RECEIVED_DATA_MSG:
                      SerialReadData data = (SerialReadData) msg.obj;
                      StringBuilder sb = new StringBuilder();
                      for (int x = 0; x < data.size; x++)
                          sb.append(String.format("%02x", data.data[x]));
              }
          }
      }
  
      public void closeSerialPort() {
          if (mSerialPort != null) {
              mSerialPort.close();
              mSerialPort = null;
          }
      }
  
      /* .............................039BPOS机串口............................... */
      SerialIdCardPort mSerialIdCardPort = null;
  
      public SerialIdCardPort getSerialIdCardPort() throws SecurityException,
              IOException, InvalidParameterException {
  
          if (mSerialIdCardPort == null) {
              mSerialIdCardPort = new SerialIdCardPort(new File("/dev/ttyS1"),
                      115200, 0);
              mSerialIdCardPort.setMaxRFByte((byte) 0x50);
          }
  
          return mSerialIdCardPort;
      }
  
      /* .............................039BPOS机串口............................... */
  
      /* ...............................判断是否是039BPOS机.......................... */
      public boolean isPos() {
          try {
              if (getSerialIdCardPort() != null) {
                  return true;
              } else {
                  return false;
              }
          } catch (Exception e) {
              e.printStackTrace();
              return false;
          }
      }
  
      /* ...............................判断是否是039BPOS机.......................... */
  
      /***************************** Pos机 串口,打印 *********************************/
  
      private SerialPort mSerialPort = null;
  
      public SerialPort getSerialPort() throws SecurityException, IOException,
              InvalidParameterException {
          if (mSerialPort == null) {
              /* Read serial port parameters */
              SharedPreferences sp = context.getSharedPreferences(
                      "android_serialport_api.sample_preferences", Context.MODE_PRIVATE);
              String path = "/dev/ttyS2";
              ;// 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/ttyS2";
                  baudrate = 115200;
              }
  
              /* Open the serial port */
              mSerialPort = new SerialPort(new File(path), baudrate, 0);
              // (new File(path), baudrate, 0);
          }
          return mSerialPort;
      }
  
      public boolean isSerialPort() {
          try {
              if (getSerialPort() != null) {
                  return true;
              } else {
                  return false;
              }
          } catch (InvalidParameterException e) {
              e.printStackTrace();
              return false;
          } catch (SecurityException e) {
              e.printStackTrace();
              return false;
          } catch (IOException e) {
              e.printStackTrace();
              return false;
          }
      }
72876d86   杜方   畅游通核销app: 1.增加550...
484
485
486
487
488
489
490
491
492
493
494
495
496
497
      public static boolean isHiboryPos() {
          try {
              String sn = android.os.Build.MODEL;
              LogUtil.d("SN", sn);
              if (TextUtils.equals(sn, "5501H")) {
                  return true;
              } else {
                  return false;
              }
          } catch (Exception e) {
              e.printStackTrace();
              return false;
          }
      }
3c2353cd   杜方   1、畅游通核销app源码提交;
498
  }