Commit bee54b14 authored by 张宏's avatar 张宏
parents 9cd24522 24eabbab
......@@ -53,6 +53,7 @@ class BleBluetoothManager<T> {
Uuid? notifyCharacteristicUuid,
Uuid? writeCharacteristicUuid,
this.connectionTimeout = const Duration(seconds: 15),
this.enableCharacteristicDiscovery = false,
}) : _parser = parser,
_ble = ble ?? FlutterReactiveBle(),
serviceUuid =
......@@ -68,6 +69,7 @@ class BleBluetoothManager<T> {
final Uuid notifyCharacteristicUuid;
final Uuid writeCharacteristicUuid;
final Duration connectionTimeout;
final bool enableCharacteristicDiscovery;
final _scanDevicesController =
StreamController<List<BluetoothScanDevice>>.broadcast();
......@@ -94,6 +96,7 @@ class BleBluetoothManager<T> {
StreamSubscription<List<int>>? _notifySubscription;
Timer? _scanTimer;
Future<void> _writeQueue = Future<void>.value();
Future<void>? _connectInProgress;
final Map<String, BluetoothScanDevice> _scannedDevices = {};
......@@ -253,7 +256,23 @@ class BleBluetoothManager<T> {
return connectToDeviceId(device.remoteId);
}
Future<void> connectToDeviceId(String deviceId) async {
Future<void> connectToDeviceId(String deviceId) {
final pending = _connectInProgress;
if (pending != null) {
debugPrint(
'🔥🔥🔥 BLE_CONNECT_DEBUG connect_join_existing deviceId=$deviceId',
);
return pending;
}
final task = _connectToDeviceIdLocked(deviceId);
_connectInProgress = task.whenComplete(() {
_connectInProgress = null;
});
return _connectInProgress!;
}
Future<void> _connectToDeviceIdLocked(String deviceId) async {
debugPrint(
'🔥🔥🔥 BLE_CONNECT_DEBUG connect_to_device_id start deviceId=$deviceId');
if (deviceId.isEmpty) {
......@@ -271,6 +290,7 @@ class BleBluetoothManager<T> {
}
await disconnect();
await Future<void>.delayed(const Duration(milliseconds: 600));
_connectedDeviceId = deviceId;
_notifyCharacteristic = QualifiedCharacteristic(
deviceId: deviceId,
......@@ -310,7 +330,9 @@ class BleBluetoothManager<T> {
debugPrint(
'🔥🔥🔥 BLE_CONNECT_DEBUG state_connected deviceId=$deviceId');
try {
await _discoverServicesAndResolveWriteCharacteristic(deviceId);
if (enableCharacteristicDiscovery) {
await _discoverServicesAndResolveWriteCharacteristic(deviceId);
}
final notifyReady = await _enableNotifications(deviceId);
if (notifyReady) {
activateDataSource();
......@@ -536,7 +558,6 @@ class BleBluetoothManager<T> {
) async {
try {
Log.warn('🔥🔥🔥 BLE_DISCOVER_DEBUG start deviceId=$deviceId');
await _ble.discoverAllServices(deviceId);
final services = await _ble.getDiscoveredServices(deviceId);
QualifiedCharacteristic? fallbackWriteCharacteristic;
QualifiedCharacteristic? fallbackNotifyCharacteristic;
......
......@@ -47,6 +47,7 @@ class MonitoringIndexCubit extends Cubit<MonitoringIndexState> {
bool _isAutoReconnectEnabled = false;
bool _isAutoConnectingBluetooth = false;
bool _isManualUnbindingBluetooth = false;
int _bluetoothReconnectAttempts = 0;
bool _isMqttReconnectEnabled = false;
bool _isMqttConnecting = false;
String? _mqttDeviceSn;
......@@ -586,8 +587,13 @@ class MonitoringIndexCubit extends Cubit<MonitoringIndexState> {
return;
}
final delaySeconds = ((_bluetoothReconnectAttempts + 1) * 5).clamp(5, 30);
debugPrint(
'🔥🔥🔥 BLE_CONNECT_DEBUG auto_reconnect_scheduled '
'delay=${delaySeconds}s attempts=$_bluetoothReconnectAttempts',
);
_bluetoothReconnectTimer ??= Timer(
const Duration(seconds: 5),
Duration(seconds: delaySeconds),
() {
_bluetoothReconnectTimer = null;
_connectBoundBluetoothDevice();
......@@ -629,7 +635,11 @@ class MonitoringIndexCubit extends Cubit<MonitoringIndexState> {
: '蓝牙自动连接未就绪,等待重试',
));
}
if (_bluetoothManager.isConnected) {
_bluetoothReconnectAttempts = 0;
}
} catch (e) {
_bluetoothReconnectAttempts++;
debugPrint(
'🔥🔥🔥 BLE_CONNECT_DEBUG auto_connect_error deviceId=$deviceId error=$e');
if (!isClosed) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment