diff --git a/app/src/main/java/com/ouxuan/oxface/DebugActivity.java b/app/src/main/java/com/ouxuan/oxface/DebugActivity.java index 3162966..c3313f2 100644 --- a/app/src/main/java/com/ouxuan/oxface/DebugActivity.java +++ b/app/src/main/java/com/ouxuan/oxface/DebugActivity.java @@ -45,6 +45,7 @@ public class DebugActivity extends Activity implements com.kongqw.serialportlibr private Ox485 ox485; private GateABController gateABController; private NetworkEnvironmentManager environmentManager; // 添加环境管理器 + private com.kongqw.serialportlibrary.SerialPortManager qrSerialPortManager; // 扫码器串口管理器 @Override protected void onCreate(Bundle savedInstanceState) { @@ -56,6 +57,17 @@ public class DebugActivity extends Activity implements com.kongqw.serialportlibr setupClickListeners(); } + @Override + protected void onDestroy() { + super.onDestroy(); + // 清理扫码器串口连接 + if (qrSerialPortManager != null) { + qrSerialPortManager.closeSerialPort(); + qrSerialPortManager = null; + Log.d(TAG, "扫码器串口连接已清理"); + } + } + private void initViews() { tvLogOutput = findViewById(R.id.tvLogOutput); logScrollView = findViewById(R.id.logScrollView); @@ -1043,9 +1055,15 @@ public class DebugActivity extends Activity implements com.kongqw.serialportlibr private void testQrScanner() { logMessage("启动扫码器测试..."); try { - com.kongqw.serialportlibrary.SerialPortManager serialPortManager = new com.kongqw.serialportlibrary.SerialPortManager(); - serialPortManager.setOnQrDataListener(this); - serialPortManager.x9_serialport_qr(); + // 如果已经有实例,先清理 + if (qrSerialPortManager != null) { + qrSerialPortManager.closeSerialPort(); + qrSerialPortManager = null; + } + + qrSerialPortManager = new com.kongqw.serialportlibrary.SerialPortManager(); + qrSerialPortManager.setOnQrDataListener(this); + qrSerialPortManager.x9_serialport_qr(); logMessage("扫码器测试已启动,请扫描二维码,数据将输出到日志"); showToast("扫码器测试已启动"); } catch (Exception e) {