From 029fb4b378c3d01d199bb14e7c179f4a243d62f5 Mon Sep 17 00:00:00 2001 From: F1ReKing <284061805@qq.com> Date: Mon, 25 Nov 2019 11:34:39 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E6=96=B0=E5=A2=9E=20=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../android/serialport/SerialPortFinder.java | 43 ++++++++++++++++--- .../serialportlib/SerialPortHelper.java | 30 +++++++++++++ 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/build.gradle b/build.gradle index 042755f..2443b8e 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.5.1' + classpath 'com.android.tools.build:gradle:3.5.2' classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/serialportlib/src/main/java/com/android/serialport/SerialPortFinder.java b/serialportlib/src/main/java/com/android/serialport/SerialPortFinder.java index 69cd699..31be114 100644 --- a/serialportlib/src/main/java/com/android/serialport/SerialPortFinder.java +++ b/serialportlib/src/main/java/com/android/serialport/SerialPortFinder.java @@ -8,6 +8,7 @@ import java.io.IOException; import java.io.LineNumberReader; import java.util.ArrayList; +import java.util.List; /** * @author F1ReKing @@ -48,17 +49,27 @@ private ArrayList getDrivers() throws IOException { } /** - * get serialPort devices + * Use {@link #getAllDevices()} instead. * - * @return 串口 + * @return serialPort */ + @Deprecated public ArrayList getDevices() { - ArrayList devices = new ArrayList<>(); + return (ArrayList) getAllDevices(); + } + + /** + * get serialPort devices + * + * @return serialPort + */ + public List getAllDevices() { + List devices = new ArrayList<>(); try { - ArrayList drivers = getDrivers(); + List drivers = getDrivers(); for (Driver driver : drivers) { String driverName = driver.getName(); - ArrayList driverDevices = driver.getDevices(); + List driverDevices = driver.getDevices(); for (File file : driverDevices) { String devicesName = file.getName(); devices.add(new Device(devicesName, driverName, file)); @@ -69,4 +80,26 @@ public ArrayList getDevices() { } return devices; } + + /** + * get serialPort devices path + * + * @return serialPort path + */ + public List getAllDeicesPath() { + List paths = new ArrayList<>(); + try { + List drivers = getDrivers(); + for (Driver driver : drivers) { + List driverDevices = driver.getDevices(); + for (File file : driverDevices) { + String devicesPaths = file.getAbsolutePath(); + paths.add(devicesPaths); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + return paths; + } } diff --git a/serialportlib/src/main/java/me/f1reking/serialportlib/SerialPortHelper.java b/serialportlib/src/main/java/me/f1reking/serialportlib/SerialPortHelper.java index 882e5e9..9f60a0a 100644 --- a/serialportlib/src/main/java/me/f1reking/serialportlib/SerialPortHelper.java +++ b/serialportlib/src/main/java/me/f1reking/serialportlib/SerialPortHelper.java @@ -6,8 +6,10 @@ import android.util.Log; import androidx.annotation.NonNull; import com.android.serialport.SerialPort; +import com.android.serialport.SerialPortFinder; import com.android.serialport.entity.BAUDRATE; import com.android.serialport.entity.DATAB; +import com.android.serialport.entity.Device; import com.android.serialport.entity.FLOWCON; import com.android.serialport.entity.PARITY; import com.android.serialport.entity.STOPB; @@ -16,6 +18,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.util.List; import me.f1reking.serialportlib.listener.IOpenSerialPortListener; import me.f1reking.serialportlib.listener.ISerialPortDataListener; import me.f1reking.serialportlib.util.ByteUtils; @@ -37,6 +40,7 @@ public class SerialPortHelper { private HandlerThread mSendingHandlerThread; private Handler mSendingHandler; private SerialPortReceivedThread mSerialPortReceivedThread; + private SerialPortFinder mSerialPortFinder; private String mPort = "/dev/ttyUSB0"; //串口设置默认值 private static int mBaudRate = 115200; //波特率默认值 @@ -46,6 +50,30 @@ public class SerialPortHelper { private static int mFlowCon = 0; //流控默认值 private static int mFlags = 0; + /** + * 获得所有串口设备的地址 + * + * @return 所有串口设备的地址 + */ + public List getAllDeicesPath() { + if (mSerialPortFinder == null) { + mSerialPortFinder = new SerialPortFinder(); + } + return mSerialPortFinder.getAllDeicesPath(); + } + + /** + * 获取所有串口设备 + * + * @return 所有串口设备 + */ + public List getAllDevices() { + if (mSerialPortFinder == null) { + mSerialPortFinder = new SerialPortFinder(); + } + return mSerialPortFinder.getAllDevices(); + } + /** * 打开串口 * @@ -142,6 +170,7 @@ public void sendTxt(String txt) { /** * 设置串口打开的监听 + * * @param IOpenSerialPortListener */ public void setIOpenSerialPortListener(IOpenSerialPortListener IOpenSerialPortListener) { @@ -150,6 +179,7 @@ public void setIOpenSerialPortListener(IOpenSerialPortListener IOpenSerialPortLi /** * 设置串口数据收发的监听 + * * @param ISerialPortDataListener */ public void setISerialPortDataListener(ISerialPortDataListener ISerialPortDataListener) {