wjc
2026-03-24 c4ae4589c6c001329ebb731589b209e8ddcbf7ca
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,14 @@
                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");
            }//
        }
@@ -423,7 +429,7 @@
                }
                return;
            }
            Log.e(TAG, "onCharacteristicWrite: 写入成功回复");
            Log.d(TAG, "onCharacteristicWrite: 写入成功回复");
            if (characteristic.getUuid().equals(charUuid)) {
                //写入成功
                if (writeListener != null) {
@@ -435,7 +441,7 @@
        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            Log.e(TAG, "onCharacteristicChanged: " + characteristic.getUuid());
            Log.d(TAG, "onCharacteristicChanged: " + characteristic.getUuid());
            //收到蓝牙回复的数
            if (characteristic.getUuid().equals(notifyCharUuid) && writeListener != null) {
                byte[] value = characteristic.getValue();
@@ -458,6 +464,7 @@
                }
            }
        }
        @Override
        public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
            //todo 后面兼用mtc分包的,目前默认设置512字节