Blame view

src/android_serialport_api/SerialPortNewHandleFinder.java 3.37 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
  /*
   * 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;
  
  import java.io.File;
  import java.io.FileReader;
  import java.io.IOException;
  import java.io.LineNumberReader;
  import java.util.Iterator;
  import java.util.Vector;
  
  import android.util.Log;
  
  public class SerialPortNewHandleFinder {
  
  	public class Driver {
  		public Driver(String name, String root) {
  			mDriverName = name;
  			mDeviceRoot = root;
  		}
  		private String mDriverName;
  		private String mDeviceRoot;
  		Vector<File> mDevices = null;
  		public Vector<File> getDevices() {
  			if (mDevices == null) {
  				mDevices = new Vector<File>();
  				File dev = new File("/dev");
  				File[] files = dev.listFiles();
  				int i;
  				for (i=0; i<files.length; i++) {
  					if (files[i].getAbsolutePath().startsWith(mDeviceRoot)) {
  						Log.d(TAG, "Found new device: " + files[i]);
  						mDevices.add(files[i]);
  					}
  				}
  			}
  			return mDevices;
  		}
  		public String getName() {
  			return mDriverName;
  		}
  	}
  
  	private static final String TAG = "SerialPort";
  
  	private Vector<Driver> mDrivers = null;
  
  	Vector<Driver> getDrivers() throws IOException {
  		if (mDrivers == null) {
  			mDrivers = new Vector<Driver>();
  			LineNumberReader r = new LineNumberReader(new FileReader("/proc/tty/drivers"));
  			String l;
  			while((l = r.readLine()) != null) {
  				// Issue 3:
  				// Since driver name may contain spaces, we do not extract driver name with split()
  				String drivername = l.substring(0, 0x15).trim();
  				String[] w = l.split(" +");
  				if ((w.length >= 5) && (w[w.length-1].equals("serial"))) {
  					Log.d(TAG, "Found new driver " + drivername + " on " + w[w.length-4]);
  					mDrivers.add(new Driver(drivername, w[w.length-4]));
  				}
  			}
  			r.close();
  		}
  		return mDrivers;
  	}
  
  	public String[] getAllDevices() {
  		Vector<String> devices = new Vector<String>();
  		// Parse each driver
  		Iterator<Driver> itdriv;
  		try {
  			itdriv = getDrivers().iterator();
  			while(itdriv.hasNext()) {
  				Driver driver = itdriv.next();
  				Iterator<File> itdev = driver.getDevices().iterator();
  				while(itdev.hasNext()) {
  					String device = itdev.next().getName();
  					String value = String.format("%s (%s)", device, driver.getName());
  					devices.add(value);
  				}
  			}
  		} catch (IOException e) {
  			e.printStackTrace();
  		}
  		return devices.toArray(new String[devices.size()]);
  	}
  
  	public String[] getAllDevicesPath() {
  		Vector<String> devices = new Vector<String>();
  		// Parse each driver
  		Iterator<Driver> itdriv;
  		try {
  			itdriv = getDrivers().iterator();
  			while(itdriv.hasNext()) {
  				Driver driver = itdriv.next();
  				Iterator<File> itdev = driver.getDevices().iterator();
  				while(itdev.hasNext()) {
  					String device = itdev.next().getAbsolutePath();
  					devices.add(device);
  				}
  			}
  		} catch (IOException e) {
  			e.printStackTrace();
  		}
  		return devices.toArray(new String[devices.size()]);
  	}
  }