|
@@ -91,14 +91,36 @@ public class UserCache extends SPUtils {
|
|
|
|
|
|
public static String getSerialName() {
|
|
|
SerialPortFinder finder = new SerialPortFinder();
|
|
|
- if (finder.getAllDevicesPath().length>0){
|
|
|
- for (int i = 0; i < finder.getAllDevicesPath().length; i++) {
|
|
|
- Log.e("------------>","---------->finder.getAllDevicesPath("+i+")="+finder.getAllDevicesPath()[i]);
|
|
|
+ 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");
|
|
|
}
|
|
|
- Log.e("------------>","---------->finder.getAllDevicesPath()="+finder.getAllDevicesPath());
|
|
|
- String sName = finder.getAllDevicesPath()[0];
|
|
|
return sName;
|
|
|
}
|
|
|
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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|