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; } }
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 class SerialPort { private static final String TAG = "SerialPort";
/* * Do not remove or rename the field mFd: it is used by native method * close(); */ private FileDescriptor mFd; private FileInputStream mFileInputStream; private FileOutputStream mFileOutputStream;
public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException {
/* Check access permission */ if (!device.canRead() || !device.canWrite()) { try { /* Missing read/write permission, trying to chmod the file */ Process su; su = Runtime.getRuntime().exec("/system/bin/su"); String cmd = "chmod 666 " + device.getAbsolutePath() + "
" + "exit
"; su.getOutputStream().write(cmd.getBytes()); if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()) { throw new SecurityException(); } } catch (Exception e) { e.printStackTrace(); throw new SecurityException(); } }
mFd = open(device.getAbsolutePath(), baudrate, flags); if (mFd == null) { Log.e(TAG, "native open returns null"); throw new IOException(); } mFileInputStream = new FileInputStream(mFd); mFileOutputStream = new FileOutputStream(mFd); }
// Getters and setters public InputStream getInputStream() { return mFileInputStream; }
public OutputStream getOutputStream() { return mFileOutputStream; }
// JNI private native static FileDescriptor open(String path, int baudrate, int flags);
public class MyApplication extends android.app.Application { public SerialPortFinder mSerialPortFinder = new SerialPortFinder(); private SerialPort mSerialPort = null;
public SerialPort getSerialPort() throws SecurityException, IOException, InvalidParameterException { if (mSerialPort == null) { /* Read serial port parameters */ SharedPreferences sp = getSharedPreferences("android_serialport_api.sample_preferences", MODE_PRIVATE); String path = sp.getString("DEVICE", ""); int baudrate = Integer.decode(sp.getString("BAUDRATE", "-1"));
/* Check parameters */ if ( (path.length() == 0) || (baudrate == -1)) { throw new InvalidParameterException(); }
/* Open the serial port */ mSerialPort = new SerialPort(new File(path), baudrate, 0); } return mSerialPort; }
public void closeSerialPort() { if (mSerialPort != null) { mSerialPort.close(); mSerialPort = null; } } } </span>
6)编写string.xml 以及baudrates.xml文件 在string.xml文件中添加: 复制代码 代码如下: <span style="font-size:18px;"> <string name="error_configuration">Please configure your serial port first.</string> <string name="error_security">You do not have read/write permission to the serial port.</string> <string name="error_unknown">The serial port can not be opened for an unknown reason.</string> </span>