From 4c6f5510f545f27ab7a5b76fbc40278f118ffe6b Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期三, 25 三月 2026 16:52:04 +0800
Subject: [PATCH] 2026年03月25日16:51:49
---
app/src/main/java/com/hdl/photovoltaic/utils/BleWifiConfiguratorUtils.java | 89 ++++++++++++++++++++++++++++++++------------
1 files changed, 65 insertions(+), 24 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 da08be8..f8b302d 100644
--- a/app/src/main/java/com/hdl/photovoltaic/utils/BleWifiConfiguratorUtils.java
+++ b/app/src/main/java/com/hdl/photovoltaic/utils/BleWifiConfiguratorUtils.java
@@ -442,9 +442,11 @@
@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();
+// Log.d(TAG, "鎵撳嵃鍗佸叚杩涘埗: " + bytesToHexString(value));
String response = new String(value, StandardCharsets.UTF_8);
callbackHandler.post(() -> writeListener.onDeviceResponse(response));
}
@@ -504,32 +506,38 @@
// 绗� 1 姝ワ細鍦ㄥ崗璁爤灞傞潰寮�鍚�氱煡
boolean enabled = bluetoothGatt.setCharacteristicNotification(characteristic, true);
if (enabled) {
- // 绗� 2 姝ワ細鑾峰彇瀹㈡埛绔壒寰侀厤缃弿杩扮 (CCCD)
- // 鈫� 00002902 鏄爣鍑嗙殑 CCCD 鎻忚堪绗� UUID
- BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
- UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
- if (descriptor != null) {
+ // 鏍规嵁 Android 鐗堟湰鍜岃澶囧巶鍟嗗姩鎬佽皟鏁村欢杩�
+ int delayMs = calculateNotificationDelay();
+ callbackHandler.postDelayed(() -> {
+
+ // 绗� 2 姝ワ細鑾峰彇瀹㈡埛绔壒寰侀厤缃弿杩扮 (CCCD)
+ // 鈫� 00002902 鏄爣鍑嗙殑 CCCD 鎻忚堪绗� UUID
+ 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.d(TAG, "writeDescriptor success");
- }
+ 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.d(TAG, "writeDescriptor success");
+ }
+ }, delayMs);
}
}
@@ -564,6 +572,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)); // 娌℃湁鏉冮檺
@@ -575,6 +584,38 @@
Log.d(TAG, "writeCharacteristic :寮�濮嬪畬鎴�");
}
+
+
+ /**
+ * 鏍规嵁 Android 鐗堟湰鍜岃澶囧巶鍟嗚绠楀欢杩熸椂闂�
+ * Android 10/11 涓嶉渶瑕佸欢杩� 鍘熷洜锛氱郴缁熷鐞嗗揩锛屾棤涓ユ牸妫�鏌�
+ * Android 12 闇�瑕�100-200ms寤惰繜 鍘熷洜锛氱郴缁熼渶瑕佹椂闂村噯澶� BLE 鍗忚鏍�
+ * Android 13 闇�瑕�50-100ms寤惰繜 鍘熷洜锛氫紭鍖栧悗閫熷害绋嶅揩
+ * Android 14 闇�瑕�50ms寤惰繜 鍘熷洜锛氳繘涓�姝ヤ紭鍖�
+ */
+ private int calculateNotificationDelay() {
+ int sdkVersion = Build.VERSION.SDK_INT;
+
+ // Android 12 (SDK 31) 闇�瑕佹渶闀垮欢杩�
+ if (sdkVersion == Build.VERSION_CODES.S) {
+ // 灏忕背/绾㈢背璁惧鍙兘闇�瑕佹洿闀垮欢杩�
+ if ("Xiaomi".equals(Build.MANUFACTURER)) {
+ Log.d(TAG, "妫�娴嬪埌灏忕背璁惧锛屽鍔犲欢杩熷埌 200ms");
+ return 200;
+ }
+ // 鍏朵粬 Android 12 璁惧
+ return 100;
+ }
+
+ // Android 13+ 浼樺寲鍚庡欢杩熷彲浠ョ煭涓�浜�
+ if (sdkVersion >= Build.VERSION_CODES.TIRAMISU) {
+ return 50;
+ }
+
+ // Android 11 鍙婁互涓嬩笉闇�瑕佸欢杩�
+ return 0;
+ }
+
// ==================== 宸ュ叿鏂规硶 ====================
/**
--
Gitblit v1.8.0