content: package com.aill.androidserialport;
  
  import java.io.File;
  import java.io.FileReader;
  import java.io.IOException;
  import java.io.LineNumberReader;
  import java.util.Iterator;
  import java.util.Vector;
  
  /**
   * Created on 2018/10/7
   *
   * @author AIll.
   * @description 用于查找设备下所有串口路径
   */
  public class SerialPortFinder {
  
      private static final String TAG = "SerialPort";
  
      private Vector<Driver> mDrivers;
  
      private Vector<Driver> getDrivers() throws IOException {
          if (mDrivers == null) {
              mDrivers = new Vector<>();
              LineNumberReader r = new LineNumberReader(new FileReader("/proc/tty/drivers"));
              String line;
              while ((line = r.readLine()) != null) {
                  // 设备名称可能存在空格
                  String drivername = line.substring(0, 0x15).trim();
                  String[] w = line.split(" +");
                  if ((w.length >= 5) && ("serial".equals(w[w.length - 1]))) {
                      mDrivers.add(new Driver(drivername, w[w.length - 4]));
                  }
              }
              r.close();
          }
          return mDrivers;
      }
  
      public String[] getAllDevices() {
          Vector<String> devices = new Vector<>();
          // 解析每个设备
          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<>();
          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()]);
      }
  
      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 dev = new File("/dev");
                  File[] files = dev.listFiles();
                  int i;
                  for (i = 0; i < files.length; i++) {
                      if (files[i].getAbsolutePath().startsWith(mDeviceRoot)) {
                          mDevices.add(files[i]);
                      }
                  }
              }
              return mDevices;
          }
  
          public String getName() {
              return mDriverName;
          }
  
      }
  }
  