Blame view

src/android_serialport_api/print_tool/MobilePrintTool.java 17.7 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
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
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
  package android_serialport_api.print_tool;
  
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import java.util.List;
  
  import android.annotation.SuppressLint;
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.bluetooth.BluetoothAdapter;
  import android.bluetooth.BluetoothDevice;
  import android.content.Intent;
  import android.os.Bundle;
  import android.os.Handler;
  import android.os.Looper;
  import android.os.Message;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.WindowManager;
  import android.widget.Toast;
  
  import com.ectrip.cyt.config.MyApp;
  import com.ectrip.cyt.constant.DeviceType;
  import com.ectrip.cyt.constant.LogoType;
  import com.ectrip.cyt.constant.SelectAction;
  import com.ectrip.cyt.constant.constant;
  import com.ectrip.cyt.shield_home.LockLayer.MToast;
  import com.ectrip.cyt.ui.DeviceListActivity;
  import com.ectrip.cyt.utils.LogUtil;
  import com.ectrip.cyt.utils.SharedPreferences2Obj;
  import com.ectrip.trips.check.R;
  import com.eztlib.BluetoothService;
  import com.eztlib.PrinterEscCmd;
  import com.eztlib.blu.PairUtil;
  
  /**
   * @author jigo 手机、手持机打印
   */
  public class MobilePrintTool extends Activity {
  	private String TAG = "MobilePrintTool";
  	private String info;
  	private PrinterEscCmd printer;// 蓝牙打印的类
  	private Integer type = null;// 设备类型
  	public BluetoothService mService = null;
  	// 请求的代码
  	public final int REQUEST_CONNECT_DEVICE = 15;
  	private final int REQUEST_ENABLE_BT = 16;
  	public final int PRINT_RESPONSE = 10;
  	private BluetoothAdapter mBluetoothAdapter = null;// 当地蓝牙适配器
  	private boolean isOpen = false;
  	private boolean isturn = false;
  	private boolean isprint = false;
  	private boolean isprint2 = false;
  	private String isStatistic = "0";
  	private MyHandler myHandler = null;
  
  	@Override
  	protected void onCreate(Bundle savedInstanceState) {
  		super.onCreate(savedInstanceState);
  		setContentView(R.layout.mobel_tool);
  		initView();
  		try {
  			isStatistic = SharedPreferences2Obj
  					.getInstance(MobilePrintTool.this).setName("SelectAction")
  					.getObject("isStatistic", String.class); // 非统计判断
  			init(getIntent().getStringExtra("info"));
  		} catch (Exception e) {
  			e.printStackTrace();
  			finish();
  		}
  		myHandler = new MyHandler();
  	}
  
  	@Override
  	protected void onResume() {
  		super.onResume();
  		if (isprint) {
  			finish();
  		}
  	}
  
  	public void initView() {
  		findViewById(R.id.retryButton).setOnClickListener(
  				new OnClickListener() {
  					@Override
  					public void onClick(View v) {
  						adapterCallback();
  					}
  				});
  		findViewById(R.id.topBack).setOnClickListener(new OnClickListener() {
  			@Override
  			public void onClick(View v) {
  				finish();
  			}
  		});
  	}
  
  	public void init(String info) {
  		this.info = info;
  		if (printer == null) {
  			printer = new PrinterEscCmd();
  		}
  		type = SharedPreferences2Obj.getInstance(MobilePrintTool.this)
  				.setName("MachineType").getObject("type", Integer.class);
  		if (initBlu()) {
  			initBS();
  		}
  	}
  
  	private boolean initBlu() { // 判断是否有蓝牙功能
  		// 得到本地的适配器
  		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  		if (mBluetoothAdapter == null) {
  			if (type != null && type == DeviceType.HANDSET.getValue()) {
  				MToast.makeText(MobilePrintTool.this,
  						getString(R.string.bluetooth_not_available),
  						MToast.LENGTH_LONG).show();
  				finish();
  			} else {
  				Toast.makeText(MobilePrintTool.this,
  						R.string.bluetooth_not_available, Toast.LENGTH_LONG)
  						.show();
  				finish();
  			}
  			return false;
  		}
  		return true;
  	}
  
  	private void initBS() { // 初始化mService
  		if (mBluetoothAdapter != null && !mBluetoothAdapter.isEnabled()) {
  			isOpen = false;
  			if (turnOnBluetooth()) {
  				openBluDevices();
  			} else {
  				Intent enableIntent = new Intent(
  						BluetoothAdapter.ACTION_REQUEST_ENABLE);
  				startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
  			}
  			if (type != null && type == DeviceType.HANDSET.getValue()) { // 手持机强制打开
  				adapterCallback();
  			}
  		} else {
  			isOpen = true;
  			if (mService == null) {
  				mService = new BluetoothService(MobilePrintTool.this, mHandler);
  			}
  			adapterCallback();
  		}
  	}
  
  	public void adapterCallback() { // 跳转到驱动页面
  		if (initBlu()) {
  			if (mBluetoothAdapter.isEnabled()) {
  				if (mService != null) {
  					if (mService.getState() == BluetoothService.STATE_NONE) {
  						mService.start();
  					}
  				} else {
  					mService = new BluetoothService(MobilePrintTool.this,
  							mHandler);
  				}
  				openBluDevices();
  			} else {
  				if (type != null && type == DeviceType.HANDSET.getValue()) {
  					// 先试着强制打开时,不行者弹框打开
  					if (turnOnBluetooth()) {
  						openBluDevices();
  					} else {
  						Intent enableIntent = new Intent(
  								BluetoothAdapter.ACTION_REQUEST_ENABLE);
  						startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
  					}
  				} else {
  					Intent enableIntent = new Intent(
  							BluetoothAdapter.ACTION_REQUEST_ENABLE);
  					startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
  				}
  			}
  		}
  	}
  
  	/**
  	 * 强制开启当前 Android 设备的 Bluetooth
  	 *
  	 * @return true:强制打开 Bluetooth 成功 false:强制打开 Bluetooth 失败
  	 */
  	public boolean turnOnBluetooth() {
  		if (mBluetoothAdapter != null) {
  			return mBluetoothAdapter.enable();
  		}
  		return false;
  	}
  
  	private void openBluDevices() {
  		// 打开连接蓝牙设备页面
  		if (!isturn && mBluetoothAdapter.isEnabled()) {
  			Intent intent = new Intent(MobilePrintTool.this,
  					DeviceListActivity.class);
  			startActivityForResult(intent, REQUEST_CONNECT_DEVICE);
  			isturn = true;
  		}
  	}
  
  	@Override
  	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  		super.onActivityResult(requestCode, resultCode, data);
  		switch (requestCode) {
  			case REQUEST_CONNECT_DEVICE:
  				// 当DeviceListActivity返回一个设备连接
  				if (resultCode == Activity.RESULT_OK) {
  					// 获取设备的MAC地址
  					String address = data.getExtras().getString(
  							DeviceListActivity.EXTRA_DEVICE_ADDRESS);
  					// 得到BLuetoothDevice对象
  					if (address == null) {
  						break;
  					}
72876d86   杜方   畅游通核销app: 1.增加550...
219
  					LogUtil.i(TAG,"address" + address);
3c2353cd   杜方   1、畅游通核销app源码提交;
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
  					// 获取设备进行连接
  					BluetoothDevice device = mBluetoothAdapter
  							.getRemoteDevice(address);
  					try {
  						if (type != null
  								&& type == DeviceType.HANDSET.getValue()
  								&& device.getBondState() != BluetoothDevice.BOND_BONDED) {
  							PairUtil.pair(address, constant.pair, device);
  						}
  					} catch (Exception e1) {
  						e1.printStackTrace();
  					}
  					// 尝试连接到设备
  					if (device != null) {
  						try {
  							mService.connect(device);
  						} catch (Exception e) {
  							e.printStackTrace();
  						}
  					} else {
  						finish();
  					}
  				} else {
  					finish();
  				}
  				break;
  			case REQUEST_ENABLE_BT:
  				// 当请求激活蓝牙的回报
  				if (!isOpen) {
  					adapterCallback();
  				}
  				if (resultCode == Activity.RESULT_OK) {
  					// 蓝牙现在是启用的,因此,建立一个聊天会话
  					// 蓝牙开启成功,则继续初始化UI
  					if (mService == null) {
  						mService = new BluetoothService(MobilePrintTool.this,
  								mHandler);
  					}
  
  					// openBluDevices();
  				} else {
  					// 用户没有启用蓝牙或出错
  					LogUtil.d(TAG, "BT not enabled");
  				}
  			default:
  				break;
  		}
  
  	}
  
  	private void print(final String str) {
  		if (isprint && isprint2) {
  			isprint2 = false;
  			if (printer == null) {
  				printer = new PrinterEscCmd();
  			}
  			final int printNum = MyApp.getInstance().getPrintNum();
72876d86   杜方   畅游通核销app: 1.增加550...
277
  			LogUtil.d(TAG,printNum + getString(R.string.times));
3c2353cd   杜方   1、畅游通核销app源码提交;
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
  			new Thread(new Runnable() {
  				@Override
  				public void run() {
  					printThread(printNum, str);
  				}
  			}).start();
  		}
  	}
  
  	@SuppressLint("SimpleDateFormat")
  	private void printThread(int printNum, List<String> str) {
  		for (int x = 0; x < printNum; x++) {
  			if (x == 1 && isStatistic.equals("1")) {
  				if (dialog != null) {
  					dialog.dismiss();
  				}
  				return;
  			}
  			try {
  				if (isStatistic != null && isStatistic.equals("1")) {
  					printer.escPrintText("           统计信息" + "\n");
  				} else {
  					printer.escPrintText("           订单信息");
  					// 当时重打小票的时候,多打一段字符
  					int Select = SharedPreferences2Obj
  							.getInstance(MobilePrintTool.this)
  							.setName("SelectAction")
  							.getObject("Select", Integer.class);
  					if (Select == SelectAction.Reprint.getValue()) {
  						printer.escEnter();
  						printer.escPrintText("           (重打小票)");
  					}
  				}
  				printer.escEnter();
  			} catch (Exception e) {
  				e.printStackTrace();
  			}
  
  			if (str == null) {
  				return;
  			}
  			// 打印信息
  			try {
  				if (isStatistic != null && isStatistic.equals("1")) {
  					System.out.println(str);
  					try {
  						for (int i = 0; i < str.size(); i++) {
  							printer.escPrintText(str.get(i));// 订单信息
  						}
  					} catch (Exception e) {
  						e.printStackTrace();
  					}
  				} else {
  					int Select = SharedPreferences2Obj
  							.getInstance(MobilePrintTool.this)
  							.setName("SelectAction")
  							.getObject("Select", Integer.class);
  					if (Select == SelectAction.Query.getValue()) { // 打印时间
  						SimpleDateFormat df = new SimpleDateFormat(
  								"yyyy-MM-dd HH:mm:ss");// 设置日期格式
  						printer.escPrintText("检票时间:" + df.format(new Date()));// new
  						printer.escEnter();
  						// Date()为获取当前系统时间
  					}else if (Select == SelectAction.Reprint.getValue()) {// 重打时间
  						SimpleDateFormat df = new SimpleDateFormat(
  								"yyyy-MM-dd HH:mm:ss");// 设置日期格式
  						//printer.escPrintText("重打时间:" + df.format(new Date()));// new
  						printer.escEnter();
  						// Date()为获取当前系统时间
  					}
  
  					for (int i = 0; i < str.size(); i++) {
  						printer.escPrintText(str.get(i));// 订单信息
  					}
  					printer.escEnter();
  					printer.escPrintText("取票人签名:" + "\n");
  				}
  
  				if (constant.logoMark == LogoType.CYT.getValue()) {
  					printer.escEnter();
  					printer.escPrintText("---畅游通—智慧旅游O2O平台---");
  					printer.escEnter();
  					printer.escPrintText("--------www.jingqu.cn---------");
  				} else if (constant.logoMark == LogoType.QNE.getValue()) {
  					printer.escEnter();
  					printer.escPrintText("---欢迎使用去哪儿网O2O系统---");
  					printer.escEnter();
  					printer.escPrintText("--------www.qunar.com--------");
  				}
  				printer.escEnter();
  				sleep(2000);
  			} catch (Exception e) {
  				e.printStackTrace();
  			}
  
  			// 信息完打印的空格行数
  			try {
  				printer.escEnter();
  				printer.escEnter();
  				printer.escEnter();
  				if ((x + 1) == printNum) {
  					myHandler.sendMessage(myHandler
  							.obtainMessage(FINISH_ACTIVITY));
  				}
  			} catch (Exception e) {
  				e.printStackTrace();
  			}
  		}
  	}
  
  	@SuppressLint("SimpleDateFormat") private void printThread(int printNum, String str) {
  		for (int x = 0; x < printNum; x++) {
  			if (x == 1 && isStatistic.equals("1")) {
  				if (dialog != null) {
  					dialog.dismiss();
  				}
  				return;
  			}
  			try {
  				if (isStatistic != null && isStatistic.equals("1")) {
  					printer.escPrintText("           统计信息" + "\n");
  				} else {
  					printer.escPrintText("           订单信息");
  					// 当时重打小票的时候,多打一段字符
  					int Select = SharedPreferences2Obj
  							.getInstance(MobilePrintTool.this)
  							.setName("SelectAction")
  							.getObject("Select", Integer.class);
  					if (Select == 2) {
  						printer.escEnter();
  						printer.escPrintText("           (重打小票)");
  					}
  				}
  				printer.escEnter();
  			} catch (Exception e) {
  				e.printStackTrace();
  			}
  
  			if (str == null) {
  				return;
  			}
  			// 打印信息
  			try {
  				if (isStatistic != null && isStatistic.equals("1")) {
  					System.out.println(str);
  					try {
  						printer.escPrintText(str);// 订单信息
  					} catch (Exception e) {
  						e.printStackTrace();
  					}
  				} else {
  					// 重打时间
  					int Select = SharedPreferences2Obj
  							.getInstance(MobilePrintTool.this)
  							.setName("SelectAction")
  							.getObject("Select", Integer.class);
  					if (Select == 2) {
  						SimpleDateFormat df = new SimpleDateFormat(
  								"yyyy-MM-dd HH:mm:ss");// 设置日期格式
  						printer.escPrintText("重打时间:" + df.format(new Date()));// new
  						printer.escEnter();
  						// Date()为获取当前系统时间
  					}
  
  					printer.escPrintText(str);// 订单信息
  					printer.escEnter();
  					printer.escPrintText("取票人签名:" + "\n");
  				}
  
  				if (constant.logoMark == LogoType.CYT.getValue()) {
  					printer.escEnter();
  					printer.escPrintText("---畅游通—智慧旅游O2O平台---");
  					printer.escEnter();
  					printer.escPrintText("--------www.jingqu.cn---------");
  				} else if (constant.logoMark == LogoType.QNE.getValue()) {
  					printer.escEnter();
  					printer.escPrintText("---欢迎使用去哪儿网O2O系统---");
  					printer.escEnter();
  					printer.escPrintText("--------www.qunar.com--------");
  				}
  				try {
  					printer.escEnter();
  					printer.escEnter();
  					printer.escEnter();
  					printer.escEnter();
  				} catch (Exception e) {
  					e.printStackTrace();
  				}
  				sleep(2000);
  			} catch (Exception e) {
  				e.printStackTrace();
  			} finally {
  				if ((x + 1) == printNum) {
  					myHandler.sendMessage(myHandler
  							.obtainMessage(FINISH_ACTIVITY));
  				}
  			}
  		}
  	}
  
  	private final int FINISH_ACTIVITY = 10;
  
  	@SuppressLint("HandlerLeak") class MyHandler extends Handler {
  		@Override
  		public void handleMessage(Message msg) {
  			super.handleMessage(msg);
  			switch (msg.what) {
  				case FINISH_ACTIVITY:
  					sleep(2000);
  					if (mService != null) {
  						mService.stop();
  					}
  					if (mBluetoothAdapter != null) {
  						mBluetoothAdapter.cancelDiscovery();
  						if (mBluetoothAdapter.isEnabled()) {
  							mBluetoothAdapter.disable();
  						}
  					}
  					Intent intent = new Intent();
  					setResult(2, intent);
  					finish();
  					break;
  				default:
  					break;
  			}
  		}
  	}
  
  	public void sleep(int ms) {
  
  		try {
  			java.lang.Thread.sleep(ms);
  		} catch (Exception e) {
  			e.printStackTrace();
  		}
  	}
  
  	// 蓝牙连接等待弹框
  	private ProgressDialog dialog = null;
  	// 连接的设备的名称
  	private String mConnectedDeviceName = null;
  	// 更新标题栏右边状态和读写状态的Handler
  	@SuppressLint("HandlerLeak")
  	private final Handler mHandler = new Handler() {
  		@Override
  		public void handleMessage(Message msg) {
  			switch (msg.what) {
  				case BluetoothService.MESSAGE_STATE_CHANGE:
  					switch (msg.arg1) {
  						case BluetoothService.STATE_CONNECTED:
  							MyApp.getInstance().setBluConnection(true);
  							if (!isprint) {
  								isprint = true;
  								isprint2 = true;
  							}
  							print(info);
  							if (dialog != null) {
  								dialog.dismiss();
  								dialog = null;
  							}
  							break;
  						case BluetoothService.STATE_CONNECTING:
  							if (dialog == null) {
  								// 显示ProgressDialog
  								dialog = new ProgressDialog(MobilePrintTool.this);
  								dialog.setMessage("连接打印机中...");
  								dialog.setCanceledOnTouchOutside(true);
  								dialog.setCancelable(true);
  								if (type == DeviceType.HANDSET.getValue()) {
  									dialog.getWindow()
  											.setType(
  													WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
  								}
  								try {
  									dialog.show();
  								} catch (Exception e) {
  									e.printStackTrace();
  								}
  							} else {
  								dialog.setMessage("连接打印机中...");
  							}
  							break;
  						case BluetoothService.STATE_NONE:
  							break;
  						case BluetoothService.STATE_LISTEN:
  							// MyApp.getInstance().setBluConnection(false);
  							if (dialog != null) {
  								dialog.dismiss();
  								dialog = null;
  							}
  							break;
  						case BluetoothService.STATE_FAILL:
  							if (dialog != null) {
  								dialog.dismiss();
  								dialog = null;
  							}
  							finish();
  							break;
  					}
  					break;
  				case BluetoothService.STATE_FAILL:
  					if (dialog != null) {
  						dialog.dismiss();
  						dialog = null;
  					}
  					finish();
  					break;
  				case BluetoothService.MESSAGE_WRITE:
  					break;
  				case BluetoothService.MESSAGE_READ:
  					break;
  				case BluetoothService.MESSAGE_DEVICE_NAME:
  					// 保存连接设备的名字
  					mConnectedDeviceName = msg.getData().getString(
  							BluetoothService.DEVICE_NAME);
  					if (type != null && type == DeviceType.HANDSET.getValue()) {
  						MToast.makeText(MobilePrintTool.this,
  								"连接到" + mConnectedDeviceName, MToast.LENGTH_SHORT)
  								.show();
  					} else {
  						Toast.makeText(MobilePrintTool.this,
  								"连接到" + mConnectedDeviceName, Toast.LENGTH_SHORT)
  								.show();
  					}
  					if (!isprint) {
  						isprint = true;
  						isprint2 = true;
  					}
  					print(info);
  					break;
  				case BluetoothService.MESSAGE_TOAST:
  					if (!msg.getData().getBoolean("sucess", false)) {
  						finish();
  						return;
  					}
  					if (Thread.currentThread() != Looper.getMainLooper()
  							.getThread()) {
  						Looper.prepare();
  					}
  					String info = msg.getData().getString(BluetoothService.TOAST);
  					if (info != null && !info.equals("")) {
  						if (type != null && type == DeviceType.HANDSET.getValue()) {
  							MToast.makeText(
  									MobilePrintTool.this,
  									msg.getData().getString(BluetoothService.TOAST),
  									MToast.LENGTH_LONG).show();
  						} else {
  							Toast.makeText(
  									MobilePrintTool.this,
  									msg.getData().getString(BluetoothService.TOAST),
  									Toast.LENGTH_LONG).show();
  						}
  						if (Thread.currentThread() != Looper.getMainLooper()
  								.getThread()) {
  							Looper.loop();
  						}
  					}
  					break;
  			}
  		}
  	};
  
  	@Override
  	protected void onDestroy() {
  		super.onDestroy();
  		if (mBluetoothAdapter != null) {
  			mBluetoothAdapter.cancelDiscovery();
  			if (mBluetoothAdapter.isEnabled()) {
  				mBluetoothAdapter.disable();
  			}
  		}
  	}
  }