Skip to content

Commit

Permalink
1. 新增 设备查询API
Browse files Browse the repository at this point in the history
  • Loading branch information
F1ReKing committed Nov 25, 2019
1 parent e7fd368 commit 029fb4b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;

/**
* @author F1ReKing
Expand Down Expand Up @@ -48,17 +49,27 @@ private ArrayList<Driver> getDrivers() throws IOException {
}

/**
* get serialPort devices
* Use {@link #getAllDevices()} instead.
*
* @return 串口
* @return serialPort
*/
@Deprecated
public ArrayList<Device> getDevices() {
ArrayList<Device> devices = new ArrayList<>();
return (ArrayList<Device>) getAllDevices();
}

/**
* get serialPort devices
*
* @return serialPort
*/
public List<Device> getAllDevices() {
List<Device> devices = new ArrayList<>();
try {
ArrayList<Driver> drivers = getDrivers();
List<Driver> drivers = getDrivers();
for (Driver driver : drivers) {
String driverName = driver.getName();
ArrayList<File> driverDevices = driver.getDevices();
List<File> driverDevices = driver.getDevices();
for (File file : driverDevices) {
String devicesName = file.getName();
devices.add(new Device(devicesName, driverName, file));
Expand All @@ -69,4 +80,26 @@ public ArrayList<Device> getDevices() {
}
return devices;
}

/**
* get serialPort devices path
*
* @return serialPort path
*/
public List<String> getAllDeicesPath() {
List<String> paths = new ArrayList<>();
try {
List<Driver> drivers = getDrivers();
for (Driver driver : drivers) {
List<File> driverDevices = driver.getDevices();
for (File file : driverDevices) {
String devicesPaths = file.getAbsolutePath();
paths.add(devicesPaths);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return paths;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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; //波特率默认值
Expand All @@ -46,6 +50,30 @@ public class SerialPortHelper {
private static int mFlowCon = 0; //流控默认值
private static int mFlags = 0;

/**
* 获得所有串口设备的地址
*
* @return 所有串口设备的地址
*/
public List<String> getAllDeicesPath() {
if (mSerialPortFinder == null) {
mSerialPortFinder = new SerialPortFinder();
}
return mSerialPortFinder.getAllDeicesPath();
}

/**
* 获取所有串口设备
*
* @return 所有串口设备
*/
public List<Device> getAllDevices() {
if (mSerialPortFinder == null) {
mSerialPortFinder = new SerialPortFinder();
}
return mSerialPortFinder.getAllDevices();
}

/**
* 打开串口
*
Expand Down Expand Up @@ -142,6 +170,7 @@ public void sendTxt(String txt) {

/**
* 设置串口打开的监听
*
* @param IOpenSerialPortListener
*/
public void setIOpenSerialPortListener(IOpenSerialPortListener IOpenSerialPortListener) {
Expand All @@ -150,6 +179,7 @@ public void setIOpenSerialPortListener(IOpenSerialPortListener IOpenSerialPortLi

/**
* 设置串口数据收发的监听
*
* @param ISerialPortDataListener
*/
public void setISerialPortDataListener(ISerialPortDataListener ISerialPortDataListener) {
Expand Down

0 comments on commit 029fb4b

Please sign in to comment.