Blame view

src/android_serialport_api/sample/SerialPortActivity.java 3.62 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
  /*
   * Copyright 2009 Cedric Priscal
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package android_serialport_api.sample;
  
  import android.os.Bundle;
  
  import com.ectrip.cyt.config.DevicTool;
  import com.ectrip.cyt.config.MyApp;
  import com.ectrip.cyt.ui.BaseActivity;
  import com.ectrip.trips.check.R;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.security.InvalidParameterException;
  
  import android_serialport_api.SerialPort;
  
  public abstract class SerialPortActivity extends BaseActivity {
  
      protected MyApp mApplication;
      protected SerialPort mSerialPort;
      protected OutputStream mOutputStream;
      protected InputStream mInputStream;
      protected ReadThread mReadThread;
      protected int tempFlag = -1;
  
      private class ReadThread extends Thread {
  
          @Override
          public void run() {
              super.run();
              while (!isInterrupted()) {
                  int size;
                  try {
                      byte[] buffer = new byte[1500];
                      if (mInputStream == null) {
                          return;
                      }
  //					if(tempFlag==-1){
  //						size = mInputStream.read(buffer);
  //						if (size > 0) {
  //							onDataReceived(buffer, size);
  //						}
  //					}
                  } catch (Exception e) {
                      e.printStackTrace();
                      return;
                  }
              }
          }
      }
  
      private void DisplayError(int resourceId) {
  //		AlertDialog.Builder b = new AlertDialog.Builder(this);
  //		b.setTitle("Error");
  //		b.setMessage(resourceId);
  //		b.setPositiveButton("OK", new OnClickListener() {
  //			public void onClick(DialogInterface dialog, int which) {
  ////				SerialPortActivity.this.finish();
  //			}
  //		});
  //		b.show();
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          mApplication = MyApp.getInstance();
      }
  
      protected abstract void onDataReceived(final byte[] buffer, final int size);
  
      @Override
      protected void onDestroy() {
          if (DevicTool.isHiboryPos()) {
              if (mReadThread != null)
                  mReadThread.interrupt();
              mApplication.closeSerialPort();
              mSerialPort = null;
          }
          super.onDestroy();
      }
  
      @Override
      protected void onResume() {
          // TODO Auto-generated method stub
          super.onResume();
          try {
              if (DevicTool.isHiboryPos()) {
                  mSerialPort = mApplication.getSerialPort();
                  mOutputStream = mSerialPort.getOutputStream();
                  mInputStream = mSerialPort.getInputStream();
                  /* Create a receiving thread */
                  if (mReadThread == null) {
                      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);
          }
      }
  
  }