From d0b947139a9a9fde4dc70fd71b75bb0dce253653 Mon Sep 17 00:00:00 2001 From: "Y.X.Sun" Date: Mon, 24 Jul 2017 18:14:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E5=B4=A9=E6=BA=83=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- gradle.properties | 2 +- sample/build.gradle | 2 +- .../serialport/sample/Application.java | 3 +- .../serialport/sample/SerialPortActivity.java | 7 +- .../main/res/xml/serial_port_preferences.xml | 16 +- serialport/build.gradle | 1 - .../java/android/serialport/SerialPort.java | 95 +++++----- .../android/serialport/SerialPortFinder.java | 178 +++++++++--------- 9 files changed, 162 insertions(+), 144 deletions(-) diff --git a/build.gradle b/build.gradle index 1ea4bd0..c2eea8e 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.0' + classpath 'com.android.tools.build:gradle:2.3.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle.properties b/gradle.properties index aac7c9b..93d98df 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. -org.gradle.jvmargs=-Xmx1536m +org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit diff --git a/sample/build.gradle b/sample/build.gradle index 985a0d4..e669992 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -26,7 +26,7 @@ dependencies { androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - compile 'com.android.support:appcompat-v7:25.3.0' + compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile project(':serialport') diff --git a/sample/src/main/java/android/serialport/sample/Application.java b/sample/src/main/java/android/serialport/sample/Application.java index e58cf46..4ec8298 100644 --- a/sample/src/main/java/android/serialport/sample/Application.java +++ b/sample/src/main/java/android/serialport/sample/Application.java @@ -19,6 +19,7 @@ import android.content.SharedPreferences; import android.serialport.SerialPort; import android.serialport.SerialPortFinder; + import java.io.File; import java.io.IOException; import java.security.InvalidParameterException; @@ -29,7 +30,7 @@ public class Application extends android.app.Application { private SerialPort mSerialPort = null; public SerialPort getSerialPort() - throws SecurityException, IOException, InvalidParameterException { + throws SecurityException, IOException, InvalidParameterException { if (mSerialPort == null) { /* Read serial port parameters */ diff --git a/sample/src/main/java/android/serialport/sample/SerialPortActivity.java b/sample/src/main/java/android/serialport/sample/SerialPortActivity.java index b8991e3..cfab4f6 100644 --- a/sample/src/main/java/android/serialport/sample/SerialPortActivity.java +++ b/sample/src/main/java/android/serialport/sample/SerialPortActivity.java @@ -22,6 +22,7 @@ import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.serialport.SerialPort; + import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -44,7 +45,8 @@ public void run() { int size; try { byte[] buffer = new byte[64]; - if (mInputStream == null) return; + if (mInputStream == null) + return; size = mInputStream.read(buffer); if (size > 0) { onDataReceived(buffer, size); @@ -94,7 +96,8 @@ protected void onCreate(Bundle savedInstanceState) { @Override protected void onDestroy() { - if (mReadThread != null) mReadThread.interrupt(); + if (mReadThread != null) + mReadThread.interrupt(); mApplication.closeSerialPort(); mSerialPort = null; super.onDestroy(); diff --git a/sample/src/main/res/xml/serial_port_preferences.xml b/sample/src/main/res/xml/serial_port_preferences.xml index b79e2c5..ebed79a 100644 --- a/sample/src/main/res/xml/serial_port_preferences.xml +++ b/sample/src/main/res/xml/serial_port_preferences.xml @@ -1,6 +1,14 @@ - - - + + + diff --git a/serialport/build.gradle b/serialport/build.gradle index dd14bae..261f85b 100644 --- a/serialport/build.gradle +++ b/serialport/build.gradle @@ -25,6 +25,5 @@ dependencies { androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - compile 'com.android.support:appcompat-v7:25.3.0' testCompile 'junit:junit:4.12' } diff --git a/serialport/src/main/java/android/serialport/SerialPort.java b/serialport/src/main/java/android/serialport/SerialPort.java index 32211c6..13cbe50 100644 --- a/serialport/src/main/java/android/serialport/SerialPort.java +++ b/serialport/src/main/java/android/serialport/SerialPort.java @@ -17,6 +17,7 @@ package android.serialport; import android.util.Log; + import java.io.File; import java.io.FileDescriptor; import java.io.FileInputStream; @@ -27,58 +28,60 @@ public class SerialPort { - private static final String TAG = "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; + /* + * 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 { + 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() + "\n" - + "exit\n"; - su.getOutputStream().write(cmd.getBytes()); - if ((su.waitFor() != 0) || !device.canRead() - || !device.canWrite()) { - throw new SecurityException(); - } - } catch (Exception e) { - e.printStackTrace(); - throw new SecurityException(); - } - } + 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() + "\n" + + "exit\n"; + 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; + } - 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); - } + public OutputStream getOutputStream() { + return mFileOutputStream; + } - // Getters and setters - public InputStream getInputStream() { - return mFileInputStream; - } + // JNI + private native static FileDescriptor open(String path, int baudrate, int flags); - public OutputStream getOutputStream() { - return mFileOutputStream; - } + public native void close(); - // JNI - private native static FileDescriptor open(String path, int baudrate, int flags); - public native void close(); - static { - System.loadLibrary("serial_port"); - } + static { + System.loadLibrary("serial_port"); + } } diff --git a/serialport/src/main/java/android/serialport/SerialPortFinder.java b/serialport/src/main/java/android/serialport/SerialPortFinder.java index 55ce0be..900cfb9 100644 --- a/serialport/src/main/java/android/serialport/SerialPortFinder.java +++ b/serialport/src/main/java/android/serialport/SerialPortFinder.java @@ -17,6 +17,7 @@ package android.serialport; import android.util.Log; + import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -26,96 +27,99 @@ public class SerialPortFinder { - public class Driver { - public Driver(String name, String root) { - mDriverName = name; - mDeviceRoot = root; - } - private String mDriverName; - private String mDeviceRoot; - Vector mDevices = null; - public Vector getDevices() { - if (mDevices == null) { - mDevices = new Vector(); - File dev = new File("/dev"); - File[] files = dev.listFiles(); - int i; - for (i=0; i mDevices = null; + + public Vector getDevices() { + if (mDevices == null) { + mDevices = new Vector<>(); + File dev = new File("/dev"); + File[] files = dev.listFiles(); + if (files != null) + for (File file : files) { + if (file.getAbsolutePath().startsWith(mDeviceRoot)) { + Log.d(TAG, "Found new device: " + file); + mDevices.add(file); + } + } + } + return mDevices; + } + + public String getName() { + return mDriverName; + } + } - private static final String TAG = "SerialPort"; + private static final String TAG = "SerialPort"; - private Vector mDrivers = null; + private Vector mDrivers = null; - Vector getDrivers() throws IOException { - if (mDrivers == null) { - mDrivers = new Vector(); - 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; - } + Vector getDrivers() throws IOException { + if (mDrivers == null) { + mDrivers = new Vector(); + 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 devices = new Vector(); - // Parse each driver - Iterator itdriv; - try { - itdriv = getDrivers().iterator(); - while(itdriv.hasNext()) { - Driver driver = itdriv.next(); - Iterator 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[] getAllDevices() { + Vector devices = new Vector(); + // Parse each driver + Iterator itdriv; + try { + itdriv = getDrivers().iterator(); + while (itdriv.hasNext()) { + Driver driver = itdriv.next(); + Iterator 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 devices = new Vector(); - // Parse each driver - Iterator itdriv; - try { - itdriv = getDrivers().iterator(); - while(itdriv.hasNext()) { - Driver driver = itdriv.next(); - Iterator 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 String[] getAllDevicesPath() { + Vector devices = new Vector(); + // Parse each driver + Iterator itdriv; + try { + itdriv = getDrivers().iterator(); + while (itdriv.hasNext()) { + Driver driver = itdriv.next(); + Iterator 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()]); + } }