From 09c54e2bfa51aa9800f224fda7ad3754b353bfed Mon Sep 17 00:00:00 2001
From: zhuguifei <zhuguifei@zhuguifeideiMac.local>
Date: 星期三, 14 一月 2026 08:43:21 +0800
Subject: [PATCH] 功能调整

---
 app/src/main/java/com/shlb/comb/activity/ParameterSettingActivity.java |  429 ++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 308 insertions(+), 121 deletions(-)

diff --git a/app/src/main/java/com/shlb/comb/activity/ParameterSettingActivity.java b/app/src/main/java/com/shlb/comb/activity/ParameterSettingActivity.java
index 1d68133..eab9635 100644
--- a/app/src/main/java/com/shlb/comb/activity/ParameterSettingActivity.java
+++ b/app/src/main/java/com/shlb/comb/activity/ParameterSettingActivity.java
@@ -36,35 +36,43 @@
 
 public class ParameterSettingActivity extends BaseActivity {
 
+    // 闃堝�艰瀹氱浉鍏虫帶浠�
     private Spinner sp_threshold_mode;
     private EditText et_threshold_value;
-    private Button btn_threshold_read;
+
     private Button btn_threshold_write;
 
+    // 鐏垫晱搴﹁瀹氱浉鍏虫帶浠�
     private Spinner sp_sensitivity_mode;
     private EditText et_sensitivity_value;
-    private Button btn_sensitivity_read;
+
     private Button btn_sensitivity_write;
 
+    // 鑷鍜屽浣嶆寜閽�
     private Button btn_start_check;
     private Button btn_end_check;
     private Button btn_reset_mainboard;
     private Button btn_reset_sensor;
 
+    // 鏃ュ織鍜岀姸鎬佹樉绀�
     private TextView tv_log;
     private TextView tv_right_text;
+    private androidx.cardview.widget.CardView cvLog;
     
-    // New components
+    // 鑷鐩戞祴缃戞牸鍜屾棩蹇楁帶浠�
     private RecyclerView rvGrid;
     private QMUIRoundButton btnReadData;
     private QMUIRoundButton btnClearLog;
     private ScrollView svLog;
 
+    // 鏁版嵁鐩稿叧
     private GridAdapter mAdapter;
     private List<BoxStatus> boxStatusList = new ArrayList<>();
     private StringBuilder logBuilder = new StringBuilder();
-    private boolean isSelfCheckMode = false;
-    private String pendingSettingCmd = "";
+    private boolean isSelfCheckMode = false;  // 鏍囪褰撳墠鏄惁澶勪簬鑷妯″紡
+    private String pendingSettingCmd = "";  // 璁板綍寰呭鐞嗙殑璁惧畾鎸囦护
+    private Button currentOperatingButton; // 褰撳墠姝e湪鎿嶄綔鐨勬寜閽�
+    private String pendingToastMessage = null; // 寰呮樉绀虹殑Toast娑堟伅
     
     private SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
 
@@ -85,12 +93,12 @@
 
         sp_threshold_mode = findViewById(R.id.sp_threshold_mode);
         et_threshold_value = findViewById(R.id.et_threshold_value);
-        btn_threshold_read = findViewById(R.id.btn_threshold_read);
+
         btn_threshold_write = findViewById(R.id.btn_threshold_write);
 
         sp_sensitivity_mode = findViewById(R.id.sp_sensitivity_mode);
         et_sensitivity_value = findViewById(R.id.et_sensitivity_value);
-        btn_sensitivity_read = findViewById(R.id.btn_sensitivity_read);
+
         btn_sensitivity_write = findViewById(R.id.btn_sensitivity_write);
 
         btn_start_check = findViewById(R.id.btn_start_check);
@@ -99,15 +107,15 @@
         btn_reset_sensor = findViewById(R.id.btn_reset_sensor);
 
         tv_log = findViewById(R.id.tv_log);
+        cvLog = findViewById(R.id.cv_log);
         tv_right_text = findViewById(R.id.tv_right_text);
         
-        // Init new components
+        // 鍒濆鍖栬嚜妫�鐩戞祴鐩稿叧鎺т欢
         rvGrid = findViewById(R.id.rv_grid);
-        // btnReadData was removed from layout
         btnClearLog = findViewById(R.id.btn_clear_log);
         svLog = findViewById(R.id.sv_log);
         
-        // Log setup
+        // 璁剧疆鏃ュ織婊氬姩瑙嗗浘鐨勮Е鎽哥洃鍚紝闃叉婊戝姩鍐茬獊
         if (svLog != null) {
             svLog.setOnTouchListener((v, event) -> {
                 v.getParent().requestDisallowInterceptTouchEvent(true);
@@ -118,6 +126,7 @@
             });
         }
         
+        // 鍒濆鍖栨棩蹇楀唴瀹�
         if (logBuilder.length() > 0) {
             tv_log.setText(Html.fromHtml(logBuilder.toString()));
         } else {
@@ -130,25 +139,36 @@
     protected void initData() {
         updateBluetoothStatus();
         
-        // Initialize box status list
+        // 鍒濆鍖�30涓牸瀛愮殑鐘舵�佸垪琛�
         boxStatusList.clear();
         for (int i = 1; i <= 30; i++) {
             boxStatusList.add(new BoxStatus(i));
         }
         
-        // Grid Setup
+        // 璁剧疆缃戞牸甯冨眬锛�3琛�10鍒�
         rvGrid.setLayoutManager(new GridLayoutManager(this, 10)); 
         mAdapter = new GridAdapter();
         rvGrid.setAdapter(mAdapter);
         
+        // 濡傛灉钃濈墮宸茶繛鎺ワ紝杩涘叆璁惧畾妯″紡
         if (BleGlobalManager.getInstance().isConnected()) {
              sendCmdWithCrc(CMD.ENTER_SETTING);
-             // sendCmdWithCrc(CMD.READ_DATA);
+        }
+    }
+
+    @Override
+    protected void onResume() {
+        super.onResume();
+        // 鏍规嵁绯荤粺璁剧疆鎺у埗鏃ュ織鏄剧ず
+        boolean isCmdLogEnabled = com.blankj.utilcode.util.SPUtils.getInstance().getBoolean("cmd_log_enabled", false);
+        if (cvLog != null) {
+            cvLog.setVisibility(isCmdLogEnabled ? View.VISIBLE : View.GONE);
         }
     }
 
     @Override
     protected void onDestroy() {
+        // 椤甸潰閿�姣佹椂閫�鍑鸿瀹氭ā寮�
         if (BleGlobalManager.getInstance().isConnected()) {
             sendCmdWithCrc(CMD.EXIT_SETTING);
         }
@@ -162,27 +182,37 @@
             public void onClick(View v) {
                 int id = v.getId();
                 String action = "";
-                if (id == R.id.btn_threshold_read) {
-                    action = "璇诲彇闃堝��";
-                    Toast.makeText(ParameterSettingActivity.this, "璇ュ姛鑳芥殏鏈紑鏀�", Toast.LENGTH_SHORT).show();
-                } else if (id == R.id.btn_threshold_write) {
+                if (id == R.id.btn_threshold_write) {
                     handleThresholdWrite();
-                } else if (id == R.id.btn_sensitivity_read) {
-                    action = "璇诲彇鐏垫晱搴�";
-                    Toast.makeText(ParameterSettingActivity.this, "璇ュ姛鑳芥殏鏈紑鏀�", Toast.LENGTH_SHORT).show();
                 } else if (id == R.id.btn_sensitivity_write) {
                     handleSensitivityWrite();
                 } else if (id == R.id.btn_start_check) {
-                    action = "寮�濮嬭嚜妫�";
-                    sendCmdWithCrc(CMD.WRITE_START_CHECK);
-                    isSelfCheckMode = true;
+                    if (BleGlobalManager.getInstance().isConnected()) {
+                        currentOperatingButton = btn_start_check;
+                        updateButtonState(currentOperatingButton, false);
+                        action = "寮�濮嬭嚜妫�";
+                        setAllGridGray();
+                        sendCmdWithCrc(CMD.WRITE_START_CHECK);
+                        isSelfCheckMode = true;
+                    } else {
+                        Toast.makeText(ParameterSettingActivity.this, "璇峰厛杩炴帴钃濈墮", Toast.LENGTH_SHORT).show();
+                    }
                 } else if (id == R.id.btn_end_check) {
-                    action = "缁撴潫鑷";
-                    sendCmdWithCrc(CMD.WRITE_END_CHECK);
-                    isSelfCheckMode = false;
+                    if (BleGlobalManager.getInstance().isConnected()) {
+                        currentOperatingButton = btn_end_check;
+                        updateButtonState(currentOperatingButton, false);
+                        action = "缁撴潫鑷";
+                        setAllGridGray();
+                        sendCmdWithCrc(CMD.WRITE_END_CHECK);
+                        isSelfCheckMode = false;
+                    } else {
+                         Toast.makeText(ParameterSettingActivity.this, "璇峰厛杩炴帴钃濈墮", Toast.LENGTH_SHORT).show();
+                    }
                 } else if (id == R.id.btn_reset_mainboard) {
+                    currentOperatingButton = btn_reset_mainboard;
                     showResetConfirmDialog("纭畾瑕佹墽琛屼富鏉垮浣嶅悧锛�", CMD.WRITE_BOARD_RESET);
                 } else if (id == R.id.btn_reset_sensor) {
+                    currentOperatingButton = btn_reset_sensor;
                     showResetConfirmDialog("纭畾瑕佹墽琛屼紶鎰熷櫒澶嶄綅鍚楋紵", CMD.WRITE_SENSOR_RESET);
                 }
 
@@ -193,14 +223,59 @@
             }
         };
 
-        btn_threshold_read.setOnClickListener(listener);
+
         btn_threshold_write.setOnClickListener(listener);
-        btn_sensitivity_read.setOnClickListener(listener);
+
         btn_sensitivity_write.setOnClickListener(listener);
         btn_start_check.setOnClickListener(listener);
         btn_end_check.setOnClickListener(listener);
         btn_reset_mainboard.setOnClickListener(listener);
         btn_reset_sensor.setOnClickListener(listener);
+        
+        // 闃堝�兼ā寮忛�夋嫨鐩戝惉锛屾帶鍒惰緭鍏ユ鏄惁鍙敤
+        sp_threshold_mode.setOnItemSelectedListener(new android.widget.AdapterView.OnItemSelectedListener() {
+            @Override
+            public void onItemSelected(android.widget.AdapterView<?> parent, View view, int position, long id) {
+                if (position == 0) {
+                    // 涓�閿瀹氾紝闅愯棌杈撳叆妗� (浣跨敤INVISIBLE淇濈暀鍗犱綅锛岄槻姝㈡寜閽Щ浣�)
+                    et_threshold_value.setVisibility(View.INVISIBLE);
+                    et_threshold_value.setText("");
+                } else {
+                    // 鍗曞眰璁惧畾锛屾樉绀鸿緭鍏ユ
+                    et_threshold_value.setVisibility(View.VISIBLE);
+                    int maxLayer = com.blankj.utilcode.util.SPUtils.getInstance().getInt("layer_count", 30);
+                    et_threshold_value.setHint("1-" + maxLayer);
+                }
+            }
+
+            @Override
+            public void onNothingSelected(android.widget.AdapterView<?> parent) {
+            }
+        });
+        
+
+
+        // 鐏垫晱搴︽ā寮忛�夋嫨鐩戝惉锛屾帶鍒惰緭鍏ユ鏄惁鍙敤
+        sp_sensitivity_mode.setOnItemSelectedListener(new android.widget.AdapterView.OnItemSelectedListener() {
+            @Override
+            public void onItemSelected(android.widget.AdapterView<?> parent, View view, int position, long id) {
+                // 0: 鎵归噺澧炲姞, 1: 鎵归噺鍑忓皯, 2: 鍗曞眰澧炲姞, 3: 鍗曞眰鍑忓皯
+                if (position == 0 || position == 1) {
+                    // 鎵归噺鎿嶄綔锛岄殣钘忚緭鍏ユ
+                    et_sensitivity_value.setVisibility(View.INVISIBLE);
+                    et_sensitivity_value.setText("");
+                } else {
+                    // 鍗曞眰鎿嶄綔锛屾樉绀鸿緭鍏ユ
+                    et_sensitivity_value.setVisibility(View.VISIBLE);
+                    int maxLayer = com.blankj.utilcode.util.SPUtils.getInstance().getInt("layer_count", 30);
+                    et_sensitivity_value.setHint("1-" + maxLayer);
+                }
+            }
+
+            @Override
+            public void onNothingSelected(android.widget.AdapterView<?> parent) {
+            }
+        });
         
         btnClearLog.setOnClickListener(v -> {
             logBuilder.setLength(0);
@@ -212,7 +287,7 @@
     private String getCmdDescription(String hex, Boolean isSent) {
         if (hex == null) return "";
         
-        // Special handling for 3B (Enter/Exit Setting)
+        // 杩涘叆/閫�鍑鸿瀹氭ā寮忔寚浠ょ壒娈婂鐞�
         if (hex.startsWith(CMD.ENTER_SETTING.substring(0, 8))) {
             if (hex.startsWith(CMD.ENTER_SETTING)) return "-杩涘叆璁惧畾";
             if (hex.startsWith(CMD.EXIT_SETTING)) return "-閫�鍑鸿瀹�";
@@ -249,7 +324,7 @@
         String displayMsg = msg;
         String cmdDesc = "";
         
-        // 濡傛灉鏄� hex 鎸囦护 (绾� 0-9 A-F a-f)锛屽姞绌烘牸鏍煎紡鍖�
+        // 濡傛灉鏄崄鍏繘鍒舵寚浠わ紝姣忎袱浣嶅姞涓�涓┖鏍兼牸寮忓寲鏄剧ず
         if (msg.matches("^[0-9A-Fa-f]+$")) {
             StringBuilder sb = new StringBuilder();
             for (int i = 0; i < msg.length(); i += 2) {
@@ -267,8 +342,8 @@
         
         String logLine;
         if (isSent != null) {
-            String color = isSent ? "#1890ff" : "#05aa87"; // Blue for sent, Green for received
-            // 鎷兼帴鍒板墠缂�鍚庨潰: "鍙戦��-璇荤珯鍙�: " 鎴� "鏀跺埌-璇荤珯鍙�: "
+            // 鍙戦�佹寚浠ょ敤钃濊壊锛屾帴鏀舵暟鎹敤缁胯壊
+            String color = isSent ? "#1890ff" : "#05aa87";
             String prefix = (isSent ? "鍙戦��" : "鏀跺埌") + cmdDesc + ": ";
             logLine = time + " <font color='" + color + "'>" + prefix + displayMsg + "</font><br>";
         } else {
@@ -278,7 +353,7 @@
         
         logBuilder.append(logLine);
         
-        // Update UI
+        // 鏇存柊鏃ュ織鏄剧ず骞惰嚜鍔ㄦ粴鍔ㄥ埌搴曢儴
         if (tv_log != null) {
             tv_log.setText(Html.fromHtml(logBuilder.toString()));
              if (svLog != null) {
@@ -287,7 +362,7 @@
         }
     }
     
-    // Kept for backward compatibility if needed, but redirected to appendLog
+    // 鍏煎鏃т唬鐮佺殑鏃ュ織鏂规硶
     private void addLog(String message) {
         appendLog(message);
     }
@@ -298,7 +373,7 @@
             updateBluetoothStatus();
         } else if (event.getType() == UpdateEvent.Type.DEVICE_INFO) {
             String hex = event.getMsg();
-            appendLog(hex, false); // false for received (Green)
+            appendLog(hex, false);  // false琛ㄧず鎺ユ敹鐨勬暟鎹紝鏄剧ず涓虹豢鑹�
             parseAndRefresh(hex);
         }
     }
@@ -306,11 +381,13 @@
     private void parseAndRefresh(String hex) {
         if (hex == null) return;
         
-        // 鍐欏叆鎸囦护鐨勮繑鍥� (A55A06寮�澶�)
-        if (hex.startsWith("A55A06") && hex.length() >= 12) {
+        // 澶勭悊鍐欏叆鎸囦护鐨勮繑鍥烇紙A55A06寮�澶达級
+        if (hex.startsWith(CMD.WRITE_SUFFIX) && hex.length() >= 12) {
+             // 鎻愬彇鐘舵�佸瓧鑺傦紝01琛ㄧず鎴愬姛
              String statusHex = hex.substring(10, 12);
              boolean isSuccess = "01".equals(statusHex);
 
+             // 澶勭悊杩涘叆璁惧畾妯″紡鐨勮繑鍥�
              if (hex.startsWith(CMD.ENTER_SETTING.substring(0, 8))) {
                  if (isSuccess) {
                      appendLog("杩涘叆璁惧畾妯″紡鎴愬姛");
@@ -320,57 +397,108 @@
                  return;
              }
              
+             // 澶勭悊閫�鍑鸿瀹氭ā寮忕殑杩斿洖
              if (hex.startsWith(CMD.EXIT_SETTING.substring(0, 8))) {
                  appendLog("閫�鍑鸿瀹氭ā寮忔垚鍔�");
                  return;
              }
 
+             // 澶勭悊闃堝�艰瀹氱殑杩斿洖
              if (hex.startsWith(CMD.WRITE_THRESHOLD_SETTING.substring(0, 8))) {
                  if (isSuccess) {
                      appendLog("闃堝�煎啓鍏ユ垚鍔�");
-                     Toast.makeText(this, "闃堝�煎啓鍏ユ垚鍔�", Toast.LENGTH_SHORT).show();
+                     pendingToastMessage = "闃堝�煎啓鍏ユ垚鍔�";
+                     // 闃堝�煎啓鍏ユ垚鍔熷悗锛岀瓑寰�500姣鍐嶈鍙栨暟鎹�
+                     appendLog("500姣鍚庤鍙栭槇鍊艰瀹氱粨鏋�...");
+                     new android.os.Handler().postDelayed(() -> {
+                         sendCmdWithCrc(CMD.READ_DATA);
+                     }, 500);
                  } else {
+                     restoreCurrentButton();
                      appendLog("闃堝�煎啓鍏ュけ璐�");
                      Toast.makeText(this, "闃堝�煎啓鍏ュけ璐�", Toast.LENGTH_SHORT).show();
                  }
              } else if (hex.startsWith(CMD.WRITE_SENSITIVITY_SETTING.substring(0, 8))) {
+                 // 澶勭悊鐏垫晱搴﹁瀹氱殑杩斿洖
                  if (isSuccess) {
                      appendLog("鐏垫晱搴﹀啓鍏ユ垚鍔�");
-                     Toast.makeText(this, "鐏垫晱搴﹀啓鍏ユ垚鍔�", Toast.LENGTH_SHORT).show();
+                     pendingToastMessage = "鐏垫晱搴﹀啓鍏ユ垚鍔�";
+                     // 鐏垫晱搴﹀啓鍏ユ垚鍔熷悗锛岀瓑寰�500姣鍐嶈鍙栨暟鎹�
+                     appendLog("500姣鍚庤鍙栫伒鏁忓害璁惧畾缁撴灉...");
+                     new android.os.Handler().postDelayed(() -> {
+                         sendCmdWithCrc(CMD.READ_DATA);
+                     }, 500);
                  } else {
+                     restoreCurrentButton();
                      appendLog("鐏垫晱搴﹀啓鍏ュけ璐�");
                      Toast.makeText(this, "鐏垫晱搴﹀啓鍏ュけ璐�", Toast.LENGTH_SHORT).show();
                  }
-             } else if (hex.startsWith(CMD.WRITE_BOARD_RESET.substring(0, 8))) { 
+             } else if (hex.startsWith(CMD.WRITE_BOARD_RESET.substring(0, 8))) {
+                 // 澶勭悊涓绘澘/浼犳劅鍣ㄥ浣嶇殑杩斿洖
+                 // 鍒ゆ柇鏄富鏉垮浣嶈繕鏄紶鎰熷櫒澶嶄綅
                  boolean isBoardReset = pendingSettingCmd.equals(CMD.WRITE_BOARD_RESET);
                  String name = isBoardReset ? "涓绘澘澶嶄綅" : "浼犳劅鍣ㄥ浣�";
                  
                  if (isSuccess) {
                      appendLog(name + "鎴愬姛");
-                     Toast.makeText(this, name + "鎴愬姛", Toast.LENGTH_SHORT).show();
+                     
+                     // 濡傛灉鏄富鏉垮浣嶆垚鍔燂紝鎻愮ず鍗冲皢杩斿洖骞惰烦杞埌瀹炴椂鐩戞帶椤甸潰
+                     if (isBoardReset) {
+                         Toast.makeText(this, "澶嶄綅鎴愬姛鍗冲皢杩斿洖", Toast.LENGTH_SHORT).show();
+                         appendLog("澶嶄綅鎴愬姛锛�2绉掑悗杩斿洖瀹炴椂鐩戞帶椤甸潰...");
+                         
+                         // 2绉掑悗杩斿洖鍒板疄鏃剁洃鎺ч〉闈�
+                         new android.os.Handler().postDelayed(() -> {
+                             finish();  // 鍏抽棴褰撳墠椤甸潰锛岃繑鍥炲埌瀹炴椂鐩戞帶椤甸潰
+                         }, 2000);
+                     } else {
+                         // 浼犳劅鍣ㄥ浣嶆垚鍔燂紝鎻愮ず骞跺湪500姣鍚庤鍙栨暟鎹�
+                         Toast.makeText(this, name + "鎴愬姛", Toast.LENGTH_SHORT).show();
+                         
+                         // 500姣鍚庤鍙栨暟鎹紝閫氳繃鐜版湁鐨刾arseSelfCheckData瑙f瀽
+                         new android.os.Handler().postDelayed(() -> {
+                             sendCmdWithCrc(CMD.READ_DATA);
+                         }, 500);
+                     }
                  } else {
+                     restoreCurrentButton();
                      appendLog(name + "澶辫触");
                      Toast.makeText(this, name + "澶辫触", Toast.LENGTH_SHORT).show();
                  }
              }
         }
 
-        // Check for Self Check command response
+        // 澶勭悊鑷鎸囦护鐨勮繑鍥炵粨鏋�
+        // 寮�濮嬭嚜妫�鍜岀粨鏉熻嚜妫�鐨勬寚浠ゅ墠缂�鐩稿悓锛岄兘鏄� CMD.WRITE_START_CHECK 鐨勫墠8浣�
         if (hex.startsWith(CMD.WRITE_START_CHECK.substring(0, 8)) && hex.length() >= 12) {
+             // 鎻愬彇鐘舵�佸瓧鑺傦紙绗�11-12浣嶏級锛�01琛ㄧず鎴愬姛锛屽叾浠栬〃绀哄け璐�
              String statusHex = hex.substring(10, 12);
              boolean isSuccess = "01".equals(statusHex);
+             // 鏍规嵁褰撳墠妯″紡鍒ゆ柇鎿嶄綔鍚嶇О
              String opName = isSelfCheckMode ? "寮�濮嬭嚜妫�" : "缁撴潫鑷";
              
              if (isSuccess) {
                  appendLog(opName + "鎴愬姛");
-                 Toast.makeText(this, opName + "鎴愬姛", Toast.LENGTH_SHORT).show();
+                 pendingToastMessage = opName + "鎴愬姛";
                  
-                 // If success and isSelfCheckMode, wait 5s then read data
+                 // 鏍规嵁涓嶅悓鐨勮嚜妫�闃舵锛屽欢杩熶笉鍚屾椂闂村悗璇诲彇鏁版嵁
                  if (isSelfCheckMode) {
-                     appendLog("5绉掑悗璇诲彇鑷缁撴灉...");
+                     // 寮�濮嬭嚜妫�鎴愬姛鍚庯紝绛夊緟 灞傛暟 * 1000 姣鍐嶈鍙栨暟鎹�
+                     int layerCount = com.blankj.utilcode.util.SPUtils.getInstance().getInt("layer_count", 3);
+                     long delay = layerCount * 1000L;
+                     Toast.makeText(this, "寮�濮嬭嚜妫�锛岃绛夊緟" + layerCount + "绉�", Toast.LENGTH_SHORT).show();
+                     // 杩欎釜鏃堕棿鏄负浜嗚浼犳劅鍣ㄥ畬鎴愯嚜妫�鍔ㄤ綔
+                     appendLog(delay + "姣鍚庤鍙栬嚜妫�缁撴灉...");
                      new android.os.Handler().postDelayed(() -> {
                          sendCmdWithCrc(CMD.READ_DATA);
-                     }, 5000);
+                     }, delay);
+                 } else {
+                     // 缁撴潫鑷鎴愬姛鍚庯紝绛夊緟500姣鍐嶈鍙栨暟鎹�
+                     // 杩欎釜鏃堕棿鏄负浜嗚璁惧鐘舵�佺ǔ瀹氬悗鍐嶈鍙栨渶缁堢粨鏋�
+                     appendLog("500姣鍚庤鍙栬嚜妫�缁撴灉...");
+                     new android.os.Handler().postDelayed(() -> {
+                         sendCmdWithCrc(CMD.READ_DATA);
+                     }, 500);
                  }
              } else {
                  appendLog(opName + "澶辫触");
@@ -378,69 +506,54 @@
              }
         }
         
-        // Handle READ_DATA response for Self Check results
-        // A55A0301...
+        // 澶勭悊璇诲彇鏁版嵁鎸囦护鐨勮繑鍥炵粨鏋滐紝鐢ㄤ簬鏄剧ず鑷缁撴灉
+        // 鏁版嵁鏍煎紡锛欰55A0301... 鑷冲皯闇�瑕�26浣嶅崄鍏繘鍒跺瓧绗�
         if (hex.startsWith(CMD.READ_DATA.substring(0, 8)) && hex.length() >= 26) {
+             restoreCurrentButton();
              parseSelfCheckData(hex);
+             if (pendingToastMessage != null) {
+                 Toast.makeText(this, pendingToastMessage, Toast.LENGTH_SHORT).show();
+                 pendingToastMessage = null;
+             }
         }
     }
     
+    /**
+     * 瑙f瀽鑷鏁版嵁骞舵洿鏂版牸瀛愮姸鎬�
+     * @param hex 鎺ユ敹鍒扮殑鍗佸叚杩涘埗鏁版嵁
+     */
     private void parseSelfCheckData(String hex) {
         try {
-             // 11th hex -> index 10-18 (8 chars)
+             // 鎻愬彇绗�11-18浣嶏紙8涓瓧绗︼級浣滀负绗竴閮ㄥ垎鏁版嵁
              String part1Hex = hex.substring(10, 18);
              long part1Bits = Long.parseLong(part1Hex, 16);
              
-             // 18th hex -> index 18-26 (8 chars)
+             // 鎻愬彇绗�19-26浣嶏紙8涓瓧绗︼級浣滀负绗簩閮ㄥ垎鏁版嵁
              String part2Hex = hex.substring(18, 26);
              long part2Bits = Long.parseLong(part2Hex, 16);
              
              appendLog("鑷鏁版嵁瑙f瀽: " + part1Hex + ", " + part2Hex);
              
              boolean hasChange = false;
+             // 閬嶅巻鎵�鏈夋牸瀛愶紝鏍规嵁浣嶆暟鎹垽鏂嚜妫�缁撴灉
              for (BoxStatus box : boxStatusList) {
-                 int bitIndex = box.id - 1;
+                 int bitIndex = box.id - 1;  // 鏍煎瓙ID浠�1寮�濮嬶紝浣嶇储寮曚粠0寮�濮�
                  if (bitIndex >= 0 && bitIndex < 32) {
-                     // Determine success based on both bits
-                     // Assuming both need to be 1, or following specific logic.
-                     // Based on user request "11th and 18th hex", we check both.
-                     // Usually part1 is Glass (Detection), part2 is Online.
-                     // We'll assume success means bit is set in both (Online and Detected?)
-                     // OR maybe just Online? 
-                     // Let's stick to combining them to be safe as user mentioned both.
-                     // But if Self Check is just checking if it's working, maybe Online is enough?
-                     // Let's use: Success = (part1 & 1) && (part2 & 1) ? 
-                     // Let's use bitwise AND of the two parts for the result.
-                     
+                     // 鎻愬彇瀵瑰簲浣嶇殑鍊�
                      boolean bit1 = ((part1Bits >> bitIndex) & 1) == 1;
                      boolean bit2 = ((part2Bits >> bitIndex) & 1) == 1;
                      
-                     // If user meant "11th OR 18th", or "11th is this, 18th is that".
-                     // Given "Success/Fail", I'll assume both must be valid.
-                     // However, in normal operation:
-                     // Glass=0, Online=1 -> Empty box, but sensor working.
-                     // Glass=1, Online=1 -> Full box, sensor working.
-                     // Online=0 -> Sensor broken/offline.
-                     
-                     // If "Self Check" puts the sensor in a state where it should report "1" for Glass?
-                     // If so, then we expect Glass=1 AND Online=1.
-                     // If "Self Check" just checks health, then Glass might be 0.
-                     
-                     // "纭畾鍝簺鏍煎瓙鑷鎴愬姛" (Determine which grids passed self-check).
-                     // If I use bit1 && bit2:
-                     // If a sensor is working but empty (Glass=0), it fails self-check?
-                     // This implies Self Check expects a "1" signal.
-                     // This is common in self-checks (force a signal).
-                     
+                     // 涓や釜浣嶉兘涓�1琛ㄧず鑷鎴愬姛锛屽惁鍒欏け璐�
                      if (bit1 && bit2) {
-                         box.checkResult = 1; // Green
+                         box.checkResult = 1;  // 鎴愬姛锛屾樉绀虹豢鑹�
                      } else {
-                         box.checkResult = 0; // Red
+                         box.checkResult = 0;  // 澶辫触锛屾樉绀虹孩鑹�
                      }
                      hasChange = true;
                  }
              }
              
+             // 濡傛灉鏈夋暟鎹彉鍖栵紝鍒锋柊鐣岄潰
              if (hasChange && mAdapter != null) {
                  mAdapter.notifyDataSetChanged();
                  appendLog("鑷缁撴灉宸叉洿鏂�");
@@ -452,7 +565,10 @@
         }
     }
     
-    
+    /**
+     * 鍙戦�佸甫CRC鏍¢獙鐨勬寚浠�
+     * @param cmd 瑕佸彂閫佺殑鎸囦护锛堜笉鍚獵RC锛�
+     */
     private void sendCmdWithCrc(String cmd) {
         if (!BleGlobalManager.getInstance().isConnected()) {
             Toast.makeText(this, "璇峰厛杩炴帴钃濈墮", Toast.LENGTH_SHORT).show();
@@ -462,42 +578,50 @@
         
         byte[] cmdBytes = BleGlobalManager.hexStringToBytes(cmd);
         if (cmdBytes != null) {
+            // 璁$畻CRC鏍¢獙鐮�
             String crc = CRCutil.getCRC(cmdBytes);
-            // Pad CRC to 4 chars if needed
+            // CRC琛ラ綈鍒�4浣�
             while (crc.length() < 4) {
                 crc = "0" + crc;
             }
             String fullCmd = cmd + crc.toUpperCase();
             
-            appendLog(fullCmd, true); // true for sent (Blue)
+            appendLog(fullCmd, true);  // true琛ㄧず鍙戦�佺殑鎸囦护锛屾樉绀轰负钃濊壊
             BleGlobalManager.getInstance().sendCmd(fullCmd);
         } else {
             appendLog("閿欒: 鎸囦护杞崲澶辫触");
         }
     }
 
+    /**
+     * 澶勭悊闃堝�艰瀹氬啓鍏�
+     */
     private void handleThresholdWrite() {
         int position = sp_threshold_mode.getSelectedItemPosition();
         String inputValue = et_threshold_value.getText().toString().trim();
         String modeHex = "";
         String valueHex = "";
         
-        if (position == 0) { // 涓�閿瀹�
+        if (position == 0) {
+            // 涓�閿瀹�
             modeHex = "00";
             valueHex = "00";
-        } else if (position == 1) { // 鍗曞眰璁惧畾
+        } else if (position == 1) {
+            // 鍗曞眰璁惧畾
             modeHex = "01";
+            int maxLayer = com.blankj.utilcode.util.SPUtils.getInstance().getInt("layer_count", 30);
             if (inputValue.isEmpty()) {
-                Toast.makeText(this, "璇疯緭鍏ラ槇鍊�(1-30)", Toast.LENGTH_SHORT).show();
+                Toast.makeText(this, "璇疯緭鍏ラ槇鍊�(1-" + maxLayer + ")", Toast.LENGTH_SHORT).show();
                 return;
             }
             try {
                 int val = Integer.parseInt(inputValue);
-                if (val < 1 || val > 30) {
-                    Toast.makeText(this, "闃堝�艰寖鍥�1-30", Toast.LENGTH_SHORT).show();
+                if (val < 1 || val > maxLayer) {
+                    Toast.makeText(this, "闃堝�艰寖鍥�1-" + maxLayer, Toast.LENGTH_SHORT).show();
                     return;
                 }
-                valueHex = String.format("%02X", val);
+                // 鍗曞眰璁惧畾鏃讹紝杈撳叆鍙傛暟闇�瑕佸噺1
+                valueHex = String.format("%02X", val - 1);
             } catch (NumberFormatException e) {
                 Toast.makeText(this, "杈撳叆鏍煎紡閿欒", Toast.LENGTH_SHORT).show();
                 return;
@@ -506,19 +630,40 @@
             return;
         }
         
-        // Construct pending command: CMD (32) + Len (02) + Mode + Value
-        // CMD.WRITE_THRESHOLD_SETTING is "A55A063202"
+        // 鏋勯�犳寚浠わ細CMD.WRITE_THRESHOLD_SETTING + 妯″紡 + 鍊�
         pendingSettingCmd = CMD.WRITE_THRESHOLD_SETTING + modeHex + valueHex;
-        sendCmdWithCrc(pendingSettingCmd);
+        if (BleGlobalManager.getInstance().isConnected()) {
+            currentOperatingButton = btn_threshold_write;
+            updateButtonState(currentOperatingButton, false);
+            setAllGridGray();
+            sendCmdWithCrc(pendingSettingCmd);
+        } else {
+            Toast.makeText(this, "璇峰厛杩炴帴钃濈墮", Toast.LENGTH_SHORT).show();
+        }
     }
     
+    /**
+     * 灏嗘墍鏈夋牸瀛愯缃负鐏拌壊锛堢瓑寰呯姸鎬侊級
+     */
+    private void setAllGridGray() {
+        for (BoxStatus box : boxStatusList) {
+            box.checkResult = 2; // 2琛ㄧず绛夊緟鐘舵�侊紝鏄剧ず鐏拌壊
+        }
+        if (mAdapter != null) {
+            mAdapter.notifyDataSetChanged();
+        }
+    }
+
+    /**
+     * 澶勭悊鐏垫晱搴﹁瀹氬啓鍏�
+     */
     private void handleSensitivityWrite() {
         int position = sp_sensitivity_mode.getSelectedItemPosition();
         String inputValue = et_sensitivity_value.getText().toString().trim();
         String modeHex = "";
         String valueHex = "";
         
-        // 0: 鎵归噺澧炲姞(00), 1: 鎵归噺鍑忓皯(01), 2: 鍗曞眰澧炲姞(02), 3: 鍗曞眰鍑忓皯(03)
+        // 妯″紡锛�0=鎵归噺澧炲姞(00), 1=鎵归噺鍑忓皯(01), 2=鍗曞眰澧炲姞(02), 3=鍗曞眰鍑忓皯(03)
         if (position == 0) {
             modeHex = "00";
             valueHex = "00";
@@ -533,28 +678,37 @@
             return;
         }
         
+        // 鍗曞眰鎿嶄綔闇�瑕佽緭鍏ュ叿浣撴暟鍊�
         if (position == 2 || position == 3) {
+            int maxLayer = com.blankj.utilcode.util.SPUtils.getInstance().getInt("layer_count", 30);
             if (inputValue.isEmpty()) {
-                Toast.makeText(this, "璇疯緭鍏ユ暟鍊�(1-30)", Toast.LENGTH_SHORT).show();
+                Toast.makeText(this, "璇疯緭鍏ユ暟鍊�(1-" + maxLayer + ")", Toast.LENGTH_SHORT).show();
                 return;
             }
             try {
                 int val = Integer.parseInt(inputValue);
-                if (val < 1 || val > 30) {
-                    Toast.makeText(this, "鏁板�艰寖鍥�1-30", Toast.LENGTH_SHORT).show();
+                if (val < 1 || val > maxLayer) {
+                    Toast.makeText(this, "鏁板�艰寖鍥�1-" + maxLayer, Toast.LENGTH_SHORT).show();
                     return;
                 }
-                valueHex = String.format("%02X", val);
+                // 鍗曞眰璁惧畾鏃讹紝杈撳叆鍙傛暟闇�瑕佸噺1
+                valueHex = String.format("%02X", val - 1);
             } catch (NumberFormatException e) {
                 Toast.makeText(this, "杈撳叆鏍煎紡閿欒", Toast.LENGTH_SHORT).show();
                 return;
             }
         }
         
-        // Construct pending command: CMD (33) + Len (02) + Mode + Value
-        // CMD.WRITE_SENSITIVITY_SETTING is "A55A063302"
+        // 鏋勯�犳寚浠わ細CMD.WRITE_SENSITIVITY_SETTING + 妯″紡 + 鍊�
         pendingSettingCmd = CMD.WRITE_SENSITIVITY_SETTING + modeHex + valueHex;
-        sendCmdWithCrc(pendingSettingCmd);
+        if (BleGlobalManager.getInstance().isConnected()) {
+            currentOperatingButton = btn_sensitivity_write;
+            updateButtonState(currentOperatingButton, false);
+            setAllGridGray();
+            sendCmdWithCrc(pendingSettingCmd);
+        } else {
+            Toast.makeText(this, "璇峰厛杩炴帴钃濈墮", Toast.LENGTH_SHORT).show();
+        }
     }
 
     private void updateBluetoothStatus() {
@@ -580,20 +734,25 @@
                 .setPositiveButton("纭畾", new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
-                         pendingSettingCmd = cmd;
-                         sendCmdWithCrc(cmd);
+                         if (BleGlobalManager.getInstance().isConnected()) {
+                             updateButtonState(currentOperatingButton, false);
+                             pendingSettingCmd = cmd;
+                             sendCmdWithCrc(cmd);
+                         } else {
+                             Toast.makeText(ParameterSettingActivity.this, "璇峰厛杩炴帴钃濈墮", Toast.LENGTH_SHORT).show();
+                         }
                     }
                 })
                 .setNegativeButton("鍙栨秷", null)
                 .show();
     }
     
-    // Inner classes
-    
+    /**
+     * 鏍煎瓙鐘舵�佹暟鎹被
+     */
     private static class BoxStatus {
-        int id;
-        // -1: Unknown/Initial (White), 0: Fail (Red), 1: Success (Green)
-        int checkResult = -1;
+        int id;  // 鏍煎瓙ID锛�1-30锛�
+        int checkResult = -1;  // 鑷缁撴灉锛�-1=鏈煡锛堢櫧鑹诧級锛�0=澶辫触锛堢孩鑹诧級锛�1=鎴愬姛锛堢豢鑹诧級锛�2=绛夊緟锛堢伆鑹诧級
         
         public BoxStatus(int id) {
             this.id = id;
@@ -612,18 +771,17 @@
 
         @Override
         public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
-            // Calculate Box ID based on 3 rows of 10, Right to Left logic as seen in image
-            // Row 1 (pos 0-9): 10 ... 1
-            // Row 2 (pos 10-19): 20 ... 11
-            // Row 3 (pos 20-29): 30 ... 21
-            
+            // 璁$畻鏍煎瓙ID锛�3琛�10鍒楋紝浠庡乏鍒板彸鎺掑垪
+            // 绗�1琛岋紙浣嶇疆0-9锛夛細1-10
+            // 绗�2琛岋紙浣嶇疆10-19锛夛細11-20
+            // 绗�3琛岋紙浣嶇疆20-29锛夛細21-30
             int row = position / 10;
             int col = position % 10;
-            int boxId = (row + 1) * 10 - col;
+            int boxId = row * 10 + col + 1;
             
             holder.tvBoxNumber.setText(String.valueOf(boxId));
             
-            // Find status for this box
+            // 鏌ユ壘瀵瑰簲鏍煎瓙鐨勭姸鎬�
             BoxStatus status = null;
             for (BoxStatus s : boxStatusList) {
                 if (s.id == boxId) {
@@ -632,16 +790,24 @@
                 }
             }
             
+            // 鏍规嵁鑷缁撴灉璁剧疆鑳屾櫙棰滆壊
             if (status != null) {
                 if (status.checkResult == 1) {
-                    // Success
-                    holder.viewBox.setBackgroundResource(R.drawable.bg_box_full); // Green
+                    // 鑷鎴愬姛锛屾樉绀虹豢鑹�
+                    holder.viewBox.setBackgroundResource(R.drawable.bg_box_full);
+                    holder.viewBox.getBackground().clearColorFilter();
                 } else if (status.checkResult == 0) {
-                    // Fail
-                    holder.viewBox.setBackgroundResource(R.drawable.bg_box_error); // Red
+                    // 鑷澶辫触锛屾樉绀虹孩鑹�
+                    holder.viewBox.setBackgroundResource(R.drawable.bg_box_error);
+                    holder.viewBox.getBackground().clearColorFilter();
+                } else if (status.checkResult == 2) {
+                    // 绛夊緟鐘舵�侊紝鏄剧ず鐏拌壊
+                    holder.viewBox.setBackgroundResource(R.drawable.bg_box_empty);
+                    holder.viewBox.getBackground().setColorFilter(android.graphics.Color.GRAY, android.graphics.PorterDuff.Mode.MULTIPLY);
                 } else {
-                    // Unknown / Initial
-                    holder.viewBox.setBackgroundResource(R.drawable.bg_box_empty); // White
+                    // 鏈煡鐘舵�侊紝鏄剧ず鐧借壊
+                    holder.viewBox.setBackgroundResource(R.drawable.bg_box_empty);
+                    holder.viewBox.getBackground().clearColorFilter();
                 }
             }
         }
@@ -662,4 +828,25 @@
             }
         }
     }
+
+    private void restoreCurrentButton() {
+        if (currentOperatingButton != null) {
+            updateButtonState(currentOperatingButton, true);
+            currentOperatingButton = null;
+        }
+    }
+
+    private void updateButtonState(Button btn, boolean enable) {
+        if (btn == null) return;
+        btn.setEnabled(enable);
+        if (enable) {
+            if (btn.getBackground() != null) {
+                btn.getBackground().clearColorFilter();
+            }
+        } else {
+            if (btn.getBackground() != null) {
+                btn.getBackground().setColorFilter(android.graphics.Color.GRAY, android.graphics.PorterDuff.Mode.MULTIPLY);
+            }
+        }
+    }
 }

--
Gitblit v1.9.3