From c4ae4589c6c001329ebb731589b209e8ddcbf7ca Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期二, 24 三月 2026 16:03:59 +0800
Subject: [PATCH] 2026年03月24日16:03:57

---
 app/src/main/java/com/hdl/photovoltaic/utils/BleWifiConfiguratorUtils.java |   78 ++++++++++++++++++++++++++++++++++-----
 1 files changed, 68 insertions(+), 10 deletions(-)

diff --git a/app/src/main/java/com/hdl/photovoltaic/utils/BleWifiConfiguratorUtils.java b/app/src/main/java/com/hdl/photovoltaic/utils/BleWifiConfiguratorUtils.java
index 3b9e8a4..da08be8 100644
--- a/app/src/main/java/com/hdl/photovoltaic/utils/BleWifiConfiguratorUtils.java
+++ b/app/src/main/java/com/hdl/photovoltaic/utils/BleWifiConfiguratorUtils.java
@@ -327,6 +327,7 @@
             bluetoothGatt.disconnect();  // 鍏堟柇寮�杩炴帴
             bluetoothGatt.close();
             bluetoothGatt = null;
+            Log.d(TAG, "Connecting to 鍏堟柇寮�鏃ц摑鐗欒繛鎺�");
         }
         BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(deviceAddress);
         // 鑷姩杩炴帴 = false锛岄伩鍏嶇郴缁熺紦瀛樿繛鎺�
@@ -369,9 +370,15 @@
                 gatt.discoverServices();
             } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                 isConnected = false;
+//                if (bluetoothGatt != null) {
+//                    bluetoothGatt.close();
+//                    bluetoothGatt = null;
+//                }
                 if (connectListener != null) {
                     callbackHandler.post(() -> connectListener.onDisconnected());
                 }
+
+                Log.d(TAG, "Connected to GATT server, onDisconnected");
             }//
         }
 
@@ -387,6 +394,9 @@
                 }
                 characteristic = targetService.getCharacteristic(charUuid);
                 notifyCharacteristic = targetService.getCharacteristic(notifyCharUuid);
+                boolean mtuRequestResult = gatt.requestMtu(512); // 榛樿璁剧疆璇锋眰鏈�澶� MTU锛屾湁璁惧鍙兘涓嶆敮鎸�
+                Log.d(TAG, "requestMtu(512) 缁撴灉锛�" + mtuRequestResult);
+
                 if (characteristic == null) {
                     Log.e(TAG, "Required characteristics not found");
                     notifyConnectionFailed("Characteristics not found");
@@ -419,7 +429,7 @@
                 }
                 return;
             }
-
+            Log.d(TAG, "onCharacteristicWrite: 鍐欏叆鎴愬姛鍥炲");
             if (characteristic.getUuid().equals(charUuid)) {
                 //鍐欏叆鎴愬姛
                 if (writeListener != null) {
@@ -431,6 +441,8 @@
 
         @Override
         public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
+            Log.d(TAG, "onCharacteristicChanged: " + characteristic.getUuid());
+            //鏀跺埌钃濈墮鍥炲鐨勬暟
             if (characteristic.getUuid().equals(notifyCharUuid) && writeListener != null) {
                 byte[] value = characteristic.getValue();
                 String response = new String(value, StandardCharsets.UTF_8);
@@ -440,16 +452,46 @@
 
         @Override
         public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
+            Log.d(TAG, "onDescriptorWrite: status=" + status +
+                    ", descriptor=" + descriptor.getUuid());
             // 閫氱煡鎻忚堪绗﹀啓鍏ュ畬鎴愶紝鍙互蹇界暐
-//            if (status == BluetoothGatt.GATT_SUCCESS) {
-//                Log.d(TAG, "閫氱煡寮�鍚垚鍔燂紝鍙互鎺ユ敹璁惧鍝嶅簲浜�");
-//            } else {
-//                Log.e(TAG, "閫氱煡寮�鍚け璐ワ細" + status);
-//                if (writeListener != null) {
-//                    callbackHandler.post(() -> writeListener.onWriteFailed(status));
-//                }
-//            }
+            if (status == BluetoothGatt.GATT_SUCCESS) {
+                Log.d(TAG, "閫氱煡寮�鍚垚鍔燂紝鍙互鎺ユ敹璁惧鍝嶅簲浜�");
+            } else {
+                Log.e(TAG, "閫氱煡寮�鍚け璐ワ細" + status);
+                if (writeListener != null) {
+                    callbackHandler.post(() -> writeListener.onWriteFailed(status));
+                }
+            }
         }
+
+        @Override
+        public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
+            //todo 鍚庨潰鍏肩敤mtc鍒嗗寘鐨勶紝鐩墠榛樿璁剧疆512瀛楄妭
+            if (status == BluetoothGatt.GATT_SUCCESS) {
+//                currentMtu = mtu;
+                Log.d(TAG, "鉁� MTU 鍗忓晢鎴愬姛锛氬綋鍓� MTU = " + mtu + " 瀛楄妭");
+                Log.d(TAG, "   鏈夋晥鏁版嵁闀垮害 = " + (mtu - 5) + " 瀛楄妭");
+
+//                // MTU 璁剧疆鎴愬姛鍚庯紝鏍囪杩炴帴鎴愬姛
+//                isConnected = true;
+//                if (connectListener != null) {
+//                    callbackHandler.post(() -> {
+//                        Log.d(TAG, "閫氱煡杩炴帴鎴愬姛鍥炶皟");
+//                        connectListener.onConnected();
+//                    });
+//                }
+            } else {
+//                Log.w(TAG, "鈿狅笍 MTU 鍗忓晢澶辫触锛宻tatus: " + status + "锛屼娇鐢ㄩ粯璁� MTU=23");
+////                currentMtu = 23;
+//                // 鍗充娇 MTU 澶辫触锛屼篃璁や负杩炴帴鎴愬姛锛堜娇鐢ㄩ粯璁� MTU锛�
+//                isConnected = true;
+//                if (connectListener != null) {
+//                    callbackHandler.post(() -> connectListener.onConnected());
+//                }
+            }
+        }
+
 
     };
 
@@ -467,13 +509,26 @@
             BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                     UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
             if (descriptor != null) {
+//                // 绗� 3 姝ワ細璁剧疆鎻忚堪绗︾殑鍊�
+//                // 鍏堟鏌ョ壒寰佸睘鎬э紝鍐冲畾浣跨敤 NOTIFY 杩樻槸 INDICATE
+//                int charProp = characteristic.getProperties();
+//                if ((charProp & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
+//                    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
+//                    Log.d(TAG, "浣跨敤 NOTIFICATION 妯″紡");
+//                } else if ((charProp & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
+//                    descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
+//                    Log.d(TAG, "浣跨敤 INDICATION 妯″紡");
+//                } else {
+//                    Log.e(TAG, "璇ョ壒寰佷笉鏀寔閫氱煡/鎸囩ず");
+//                    return;
+//                }
                 // 绗� 3 姝ワ細璁剧疆鎻忚堪绗︾殑鍊间负 ENABLE_NOTIFICATION_VALUE锛�
                 // 鈫� 杩欎釜鍊兼槸 byte[]{0x01, 0x00}锛屽憡璇夎澶囩"鎴戣璁㈤槄閫氱煡"
                 descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                 // 绗� 4 姝ワ細鍐欏叆鎻忚堪绗﹀埌璁惧
                 //鈫� 鍙湁鍐欏叆鎴愬姛鍚庯紝鍛婅瘔璁惧绔紝璁惧鎵嶄細寮�濮嬪彂閫侀�氱煡锛�
                 bluetoothGatt.writeDescriptor(descriptor);
-                Log.e(TAG, "writeDescriptor success");
+                Log.d(TAG, "writeDescriptor success");
             }
         }
     }
@@ -495,6 +550,7 @@
      */
     @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
     public void writeCredentials(String data, WriteListener listener) {
+        Log.d(TAG, "writeCredentials :鍑嗗鍐欏叆");
         this.writeListener = listener;
         if (!isConnected || characteristic == null) {
             if (listener != null) {
@@ -508,6 +564,7 @@
 
     @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
     private void writeCharacteristic(BluetoothGattCharacteristic characteristic, String value) {
+        Log.d(TAG, "writeCharacteristic :寮�濮嬪啓鍏�");
         if (!this.getMissingPermissions().isEmpty()) {
             callbackHandler.post(() -> this.writeListener.onWriteFailed(-2)); // 娌℃湁鏉冮檺
             return;
@@ -515,6 +572,7 @@
         characteristic.setValue(value.getBytes(StandardCharsets.UTF_8));
         characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
         bluetoothGatt.writeCharacteristic(characteristic);
+        Log.d(TAG, "writeCharacteristic :寮�濮嬪畬鎴�");
     }
 
     // ==================== 宸ュ叿鏂规硶 ====================

--
Gitblit v1.8.0