|
@@ -7,6 +7,7 @@ import com.google.gson.Gson;
|
|
import com.naz.sdkdemo.bean.UserDetail;
|
|
import com.naz.sdkdemo.bean.UserDetail;
|
|
import com.naz.sdkdemo.comm.Constants;
|
|
import com.naz.sdkdemo.comm.Constants;
|
|
import com.naz.sdkdemo.utils.SPUtils;
|
|
import com.naz.sdkdemo.utils.SPUtils;
|
|
|
|
+import java.io.File;
|
|
|
|
|
|
import android_serialport_api.SerialPortFinder;
|
|
import android_serialport_api.SerialPortFinder;
|
|
|
|
|
|
@@ -90,37 +91,37 @@ public class UserCache extends SPUtils {
|
|
}
|
|
}
|
|
|
|
|
|
public static String getSerialName() {
|
|
public static String getSerialName() {
|
|
|
|
+
|
|
SerialPortFinder finder = new SerialPortFinder();
|
|
SerialPortFinder finder = new SerialPortFinder();
|
|
- String[] devicePaths = finder.getAllDevicesPath();
|
|
|
|
- if (devicePaths.length > 0) {
|
|
|
|
- for (int i = 0; i < devicePaths.length; i++) {
|
|
|
|
- Log.e("------------>", "---------->finder.getAllDevicesPath(" + i + ")=" + devicePaths[i]);
|
|
|
|
- }
|
|
|
|
- Log.e("------------>", "---------->finder.getAllDevicesPath()=" + Arrays.toString(devicePaths));
|
|
|
|
- // 获取第一个串口路径
|
|
|
|
- String sName = devicePaths[0];
|
|
|
|
- // 在这里检查USB连接状态
|
|
|
|
- boolean isUSBConnected = isUSBConnected();
|
|
|
|
- if (isUSBConnected) {
|
|
|
|
- // 如果USB已连接,你可以在这里进行相应的操作
|
|
|
|
- Log.e("------------>", "---------->USB Connected");
|
|
|
|
- } else {
|
|
|
|
- // 如果USB未连接,你可以在这里进行相应的操作
|
|
|
|
- Log.e("------------>", "---------->USB Not Connected");
|
|
|
|
|
|
+
|
|
|
|
+ searchDevDirectory(new File("/dev"));
|
|
|
|
+ if (finder.getAllDevicesPath().length>0){
|
|
|
|
+ for (int i = 0; i < finder.getAllDevicesPath().length; i++) {
|
|
|
|
+ Log.e("------------>","---------->finder.getAllDevicesPath("+i+")="+finder.getAllDevicesPath()[i]);
|
|
}
|
|
}
|
|
|
|
+ Log.e("------------>","---------->finder.getAllDevicesPath()="+finder.getAllDevicesPath());
|
|
|
|
+ String sName = finder.getAllDevicesPath()[0];
|
|
return sName;
|
|
return sName;
|
|
}
|
|
}
|
|
return "";
|
|
return "";
|
|
}
|
|
}
|
|
|
|
|
|
- private boolean isUSBConnected() {
|
|
|
|
- UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
|
|
|
- HashMap<String, UsbDevice> connectedDevices = usbManager.getDeviceList();
|
|
|
|
- for (UsbDevice device : connectedDevices.values()) {
|
|
|
|
- if (device.getDeviceClass() == UsbConstants.USB_CLASS_COMM) {
|
|
|
|
- return true;
|
|
|
|
|
|
+ private static void searchDevDirectory(File directory) {
|
|
|
|
+ if (directory.isDirectory()) {
|
|
|
|
+ File[] files = directory.listFiles();
|
|
|
|
+ if (files != null) {
|
|
|
|
+ for (File file : files) {
|
|
|
|
+ if (file.isDirectory()) {
|
|
|
|
+ // 递归搜索子目录
|
|
|
|
+ searchDevDirectory(file);
|
|
|
|
+ } else {
|
|
|
|
+ // 检查是否是串口设备文件
|
|
|
|
+ if (file.getAbsolutePath().startsWith("/dev/tty")) {
|
|
|
|
+ System.out.println("Serial port found: " + file.getAbsolutePath());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return false;
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|