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 | 273 ++++++++++++++++++++++++++++++++++++-----------------
1 files changed, 184 insertions(+), 89 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 f23abd4..f8b302d 100644
--- a/app/src/main/java/com/hdl/photovoltaic/utils/BleWifiConfiguratorUtils.java
+++ b/app/src/main/java/com/hdl/photovoltaic/utils/BleWifiConfiguratorUtils.java
@@ -46,28 +46,21 @@
public class BleWifiConfiguratorUtils {
private static final String TAG = "BleWifiConfigurator";
-
// 榛樿鐨� UUID锛堣澶囩闇�瑕佸畾涔変互涓嬫湇鍔″拰鐗瑰緛锛�
//杩欐槸涓绘湇鍔★紝鍖呭惈鎵�鏈夐厤缃戠浉鍏崇殑鐗瑰緛銆�
- public static final UUID DEFAULT_SERVICE_UUID = UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb");
+ public static final UUID DEFAULT_SERVICE_UUID = UUID.fromString("000000ff-0000-1000-8000-00805f9b34fb");
//鐗瑰緛鍊� 1: 0000fff1-0000-1000-8000-00805f9b34fb (SSID)
//鐢ㄩ�旓細鍐欏叆 Wi-Fi 鐨� SSID 鍚嶇О
//灞炴�э細Write (鍙啓)
//鏁版嵁绫诲瀷锛歎TF-8 瀛楃涓�
//绀轰緥锛�"MyWiFi_5G"
- public static final UUID DEFAULT_SSID_CHAR_UUID = UUID.fromString("0000fff1-0000-1000-8000-00805f9b34fb");
- //鐗瑰緛鍊� 2: 0000fff2-0000-1000-8000-00805f9b34fb (瀵嗙爜)
- // 鐢ㄩ�旓細鍐欏叆 Wi-Fi 瀵嗙爜
- //灞炴�э細Write (鍙啓)
- //鏁版嵁绫诲瀷锛歎TF-8 瀛楃涓�
- //绀轰緥锛�"password123"
- public static final UUID DEFAULT_PWD_CHAR_UUID = UUID.fromString("0000fff2-0000-1000-8000-00805f9b34fb");
+ public static final UUID DEFAULT_CHAR_UUID = UUID.fromString("0000ff01-0000-1000-8000-00805f9b34fb");
//鐗瑰緛鍊� 3: 0000fff3-0000-1000-8000-00805f9b34fb (閫氱煡锛屽彲閫�)
//鐢ㄩ�旓細鎺ユ敹璁惧绔殑鍝嶅簲娑堟伅
//灞炴�э細Notify (閫氱煡)
//鏁版嵁绫诲瀷锛歎TF-8 瀛楃涓�
//绀轰緥鍝嶅簲锛�"SUCCESS", "FAIL: Invalid SSID"
- public static final UUID DEFAULT_NOTIFY_CHAR_UUID = UUID.fromString("0000fff3-0000-1000-8000-00805f9b34fb"); // 鍙�夛紝鐢ㄤ簬鎺ユ敹璁惧鍝嶅簲
+ public static final UUID DEFAULT_NOTIFY_CHAR_UUID = UUID.fromString("0000ff01-0000-1000-8000-00805f9b34fb"); // 鍙�夛紝鐢ㄤ簬鎺ユ敹璁惧鍝嶅簲
// 涓婁笅鏂�
private Context context;
@@ -79,14 +72,12 @@
// GATT 鐩稿叧
private BluetoothGatt bluetoothGatt;
private BluetoothGattService targetService;
- private BluetoothGattCharacteristic ssidCharacteristic;
- private BluetoothGattCharacteristic pwdCharacteristic;
+ private BluetoothGattCharacteristic characteristic;
private BluetoothGattCharacteristic notifyCharacteristic; // 鍙��
// 鑷畾涔� UUID锛堝厑璁歌皟鐢ㄨ�呬慨鏀癸級
private UUID serviceUuid = DEFAULT_SERVICE_UUID;
- private UUID ssidCharUuid = DEFAULT_SSID_CHAR_UUID;
- private UUID pwdCharUuid = DEFAULT_PWD_CHAR_UUID;
+ private UUID charUuid = DEFAULT_CHAR_UUID;
private UUID notifyCharUuid = DEFAULT_NOTIFY_CHAR_UUID;
// 鍥炶皟鎺ュ彛
@@ -97,9 +88,7 @@
// 鍐呴儴鐘舵��
private boolean isScanning = false;
private boolean isConnected = false;
- private int writeState = 0; // 0: idle, 1: writing ssid, 2: writing pwd
- private String ssidToWrite;
- private String pwdToWrite;
+ private String dataToWrite; // 寰呭啓鍏ョ殑鏁版嵁
// 鐢ㄤ簬灏� BLE 鍥炶皟鎶涘埌涓荤嚎绋�
private final Handler callbackHandler = new Handler(Looper.getMainLooper());
@@ -126,17 +115,10 @@
}
/**
- * 璁剧疆鑷畾涔� SSID 鐗瑰緛 UUID锛堝彲閫夛級
+ * 璁剧疆鑷畾涔夌壒寰� UUID锛堝彲閫夛級
*/
- public void setSsidCharUuid(UUID ssidCharUuid) {
- this.ssidCharUuid = ssidCharUuid;
- }
-
- /**
- * 璁剧疆鑷畾涔夊瘑鐮佺壒寰� UUID锛堝彲閫夛級
- */
- public void setPwdCharUuid(UUID pwdCharUuid) {
- this.pwdCharUuid = pwdCharUuid;
+ public void setCharUuid(UUID charUuid) {
+ this.charUuid = charUuid;
}
/**
@@ -144,6 +126,13 @@
*/
public void setNotifyCharUuid(UUID notifyCharUuid) {
this.notifyCharUuid = notifyCharUuid;
+ }
+
+ /**
+ * 妫�鏌ヨ摑鐗欒繛鎺ョ姸鎬�
+ */
+ public boolean getBluetoothStatus() {
+ return this.isConnected;
}
// ==================== 鏉冮檺妫�鏌� ====================
@@ -156,6 +145,7 @@
public List<String> getMissingPermissions() {
List<String> missing = new ArrayList<>();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
+ //鍦ㄥ簲鐢ㄩ噷--鏉冮檺鍚嶇О--闄勮繎鐨勮澶�
if (context.checkSelfPermission(android.Manifest.permission.BLUETOOTH_SCAN) != android.content.pm.PackageManager.PERMISSION_GRANTED) {
missing.add(android.Manifest.permission.BLUETOOTH_SCAN);
Log.d(TAG, "No Permissions: android.Manifest.permission.BLUETOOTH_SCAN");
@@ -225,7 +215,7 @@
isScanning = true;
Log.d(TAG, "BLE scan started");
} catch (Exception e) {
-
+ Log.d(TAG, "BLE scan fail");
}
}
@@ -249,6 +239,29 @@
@Override
public void onScanResult(int callbackType, ScanResult result) {
BluetoothDevice device = result.getDevice();
+// // 鍏抽敭锛氳幏鍙栬澶囩殑钃濈墮绫诲瀷
+// int bluetoothType = device.getType();
+// switch (bluetoothType) {
+// case BluetoothDevice.DEVICE_TYPE_CLASSIC:
+// Log.d(TAG, "馃摓 缁忓吀钃濈墮璁惧锛�" + device.getName());
+// // 渚嬪锛氳摑鐗欒�虫満銆佽溅杞介煶鍝�
+// break;
+//
+// case BluetoothDevice.DEVICE_TYPE_LE:
+// Log.d(TAG, "馃挕 浣庡姛鑰楄摑鐗欒澶囷細" + device.getName());
+// // 渚嬪锛氭墜鐜�佹櫤鑳界伅銆佷綘鐨� HDL 璁惧
+// break;
+//
+// case BluetoothDevice.DEVICE_TYPE_DUAL:
+// Log.d(TAG, "馃攧 鍙屾ā璁惧锛堟敮鎸佷袱绉嶏級锛�" + device.getName());
+// // 渚嬪锛氶珮绔摑鐗欒�虫満
+// break;
+//
+// case BluetoothDevice.DEVICE_TYPE_UNKNOWN:
+// Log.d(TAG, "鉂� 鏈煡绫诲瀷");
+// break;
+// }
+
Log.d("===", "onDeviceFound: " + Objects.requireNonNull(result.getScanRecord()).getDeviceName());
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
@@ -266,7 +279,7 @@
int rssi = result.getRssi();
byte[] scanRecord = result.getScanRecord() != null ? result.getScanRecord().getBytes() : null;
if (scanListener != null) {
- callbackHandler.post(() -> scanListener.onDeviceFound(device, rssi, scanRecord));
+ callbackHandler.post(() -> scanListener.onDeviceFound(device, rssi, scanRecord, result));
}
}
@@ -293,7 +306,7 @@
// ==================== 杩炴帴鐩稿叧 ====================
/**
- * 杩炴帴鎸囧畾鐨� BLE 璁惧(绯荤粺榛樿杩炴帴瓒呮椂30绉� )
+ * 杩炴帴鎸囧畾鐨� BLE 璁惧(绯荤粺榛樿杩炴帴瓒呮椂30绉�)
*
* @param deviceAddress 鐩爣璁惧BluetoothDevice
* @param listener 杩炴帴鐘舵�佸洖璋�
@@ -309,9 +322,12 @@
callbackHandler.post(() -> this.connectListener.onConnectionFailed("No permissions")); // 娌℃湁鏉冮檺
return;
}
+
if (bluetoothGatt != null) {
+ bluetoothGatt.disconnect(); // 鍏堟柇寮�杩炴帴
bluetoothGatt.close();
bluetoothGatt = null;
+ Log.d(TAG, "Connecting to 鍏堟柇寮�鏃ц摑鐗欒繛鎺�");
}
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(deviceAddress);
// 鑷姩杩炴帴 = false锛岄伩鍏嶇郴缁熺紦瀛樿繛鎺�
@@ -340,8 +356,7 @@
}
isConnected = false;
targetService = null;
- ssidCharacteristic = null;
- pwdCharacteristic = null;
+ characteristic = null;
notifyCharacteristic = null;
}
@@ -355,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");
}//
}
@@ -371,15 +392,17 @@
notifyConnectionFailed("Service not found");
return;
}
- ssidCharacteristic = targetService.getCharacteristic(ssidCharUuid);
- pwdCharacteristic = targetService.getCharacteristic(pwdCharUuid);
+ characteristic = targetService.getCharacteristic(charUuid);
notifyCharacteristic = targetService.getCharacteristic(notifyCharUuid);
- if (ssidCharacteristic == null || pwdCharacteristic == null) {
+ 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");
return;
}
-
+ Log.e(TAG, "Required characteristics find");
// 濡傛灉鏀寔閫氱煡锛屽彲浠ュ紑鍚�氱煡锛堝彲閫夛級
if (notifyCharacteristic != null) {
enableNotification(notifyCharacteristic);
@@ -401,25 +424,16 @@
boolean success = status == BluetoothGatt.GATT_SUCCESS;
if (!success) {
Log.e(TAG, "Write failed, status: " + status);
- writeState = 0;
if (writeListener != null) {
callbackHandler.post(() -> writeListener.onWriteFailed(status));
}
return;
}
-
- if (writeState == 1 && characteristic.getUuid().equals(ssidCharUuid)) {
- // SSID 鍐欏叆鎴愬姛锛屽紑濮嬪啓瀵嗙爜
- writeState = 2;
+ Log.d(TAG, "onCharacteristicWrite: 鍐欏叆鎴愬姛鍥炲");
+ if (characteristic.getUuid().equals(charUuid)) {
+ //鍐欏叆鎴愬姛
if (writeListener != null) {
- callbackHandler.post(() -> writeListener.onSsidWriteSuccess());
- }
- writeCharacteristic(pwdCharacteristic, pwdToWrite);
- } else if (writeState == 2 && characteristic.getUuid().equals(pwdCharUuid)) {
- // 瀵嗙爜鍐欏叆鎴愬姛
- writeState = 0;
- if (writeListener != null) {
- callbackHandler.post(() -> writeListener.onPasswordWriteSuccess());
+ callbackHandler.post(() -> writeListener.onWriteSuccess());
callbackHandler.post(() -> writeListener.onWriteComplete(true));
}
}
@@ -427,8 +441,12 @@
@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));
}
@@ -436,16 +454,47 @@
@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());
+// }
+ }
+ }
+
+
};
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
@@ -457,18 +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) {
- // 绗� 3 姝ワ細璁剧疆鎻忚堪绗︾殑鍊间负 ENABLE_NOTIFICATION_VALUE锛�
- // 鈫� 杩欎釜鍊兼槸 byte[]{0x01, 0x00}锛屽憡璇夎澶囩"鎴戣璁㈤槄閫氱煡"
- descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
- // 绗� 4 姝ワ細鍐欏叆鎻忚堪绗﹀埌璁惧
- //鈫� 鍙湁鍐欏叆鎴愬姛鍚庯紝鍛婅瘔璁惧绔紝璁惧鎵嶄細寮�濮嬪彂閫侀�氱煡锛�
- bluetoothGatt.writeDescriptor(descriptor);
- }
+ // 鏍规嵁 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");
+ }
+ }, delayMs);
}
}
@@ -482,31 +551,29 @@
// ==================== 鍐欏叆鍑瘉 ====================
/**
- * 鍐欏叆 Wi-Fi SSID 鍜屽瘑鐮侊紙蹇呴』鍦ㄨ繛鎺ユ垚鍔熷悗璋冪敤锛�
+ * 鍐欏叆鏁版嵁锛堝繀椤诲湪杩炴帴鎴愬姛鍚庤皟鐢級
*
- * @param ssid Wi-Fi SSID
- * @param password Wi-Fi 瀵嗙爜
+ * @param data 鏁版嵁
* @param listener 鍐欏叆缁撴灉鍥炶皟
*/
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
- public void writeCredentials(String ssid, String password, WriteListener listener) {
+ public void writeCredentials(String data, WriteListener listener) {
+ Log.d(TAG, "writeCredentials :鍑嗗鍐欏叆");
this.writeListener = listener;
- if (!isConnected || ssidCharacteristic == null || pwdCharacteristic == null) {
+ if (!isConnected || characteristic == null) {
if (listener != null) {
callbackHandler.post(() -> listener.onWriteFailed(-1)); // 鑷畾涔夐敊璇爜
}
return;
}
- this.ssidToWrite = ssid;
- this.pwdToWrite = password;
-
- // 寮�濮嬪啓 SSID
- writeState = 1;
- writeCharacteristic(ssidCharacteristic, ssid);
+ // 寮�濮嬪啓
+ writeCharacteristic(characteristic, data);
}
@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;
@@ -514,6 +581,39 @@
characteristic.setValue(value.getBytes(StandardCharsets.UTF_8));
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
bluetoothGatt.writeCharacteristic(characteristic);
+ 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;
}
// ==================== 宸ュ叿鏂规硶 ====================
@@ -521,7 +621,7 @@
/**
* 妫�鏌ヨ摑鐗欐槸鍚﹀紑鍚�
*/
- private boolean checkBluetoothEnabled() {
+ public boolean checkBluetoothEnabled() {
return bluetoothAdapter != null && bluetoothAdapter.isEnabled();
}
@@ -547,7 +647,7 @@
/**
* 鍙戠幇璁惧锛堝彲鑳藉娆″洖璋冿級
*/
- void onDeviceFound(BluetoothDevice device, int rssi, byte[] scanRecord);
+ void onDeviceFound(BluetoothDevice device, int rssi, byte[] scanRecord, ScanResult scanResult);
/**
* 鎵弿澶辫触
@@ -574,14 +674,9 @@
public interface WriteListener {
/**
- * SSID 鍐欏叆鎴愬姛
+ * 鍐欏叆鎴愬姛
*/
- void onSsidWriteSuccess();
-
- /**
- * 瀵嗙爜鍐欏叆鎴愬姛
- */
- void onPasswordWriteSuccess();
+ void onWriteSuccess();
/**
* 鍏ㄩ儴鍐欏叆瀹屾垚锛堟垚鍔熶负 true锛�
--
Gitblit v1.8.0