Commit e28bbcc2 authored by akari's avatar akari

fix: 蓝牙连接优化

parent bee54b14
...@@ -256,7 +256,10 @@ class BleBluetoothManager<T> { ...@@ -256,7 +256,10 @@ class BleBluetoothManager<T> {
return connectToDeviceId(device.remoteId); return connectToDeviceId(device.remoteId);
} }
Future<void> connectToDeviceId(String deviceId) { Future<void> connectToDeviceId(
String deviceId, {
bool prescan = false,
}) {
final pending = _connectInProgress; final pending = _connectInProgress;
if (pending != null) { if (pending != null) {
debugPrint( debugPrint(
...@@ -265,14 +268,17 @@ class BleBluetoothManager<T> { ...@@ -265,14 +268,17 @@ class BleBluetoothManager<T> {
return pending; return pending;
} }
final task = _connectToDeviceIdLocked(deviceId); final task = _connectToDeviceIdLocked(deviceId, prescan: prescan);
_connectInProgress = task.whenComplete(() { _connectInProgress = task.whenComplete(() {
_connectInProgress = null; _connectInProgress = null;
}); });
return _connectInProgress!; return _connectInProgress!;
} }
Future<void> _connectToDeviceIdLocked(String deviceId) async { Future<void> _connectToDeviceIdLocked(
String deviceId, {
required bool prescan,
}) async {
debugPrint( debugPrint(
'🔥🔥🔥 BLE_CONNECT_DEBUG connect_to_device_id start deviceId=$deviceId'); '🔥🔥🔥 BLE_CONNECT_DEBUG connect_to_device_id start deviceId=$deviceId');
if (deviceId.isEmpty) { if (deviceId.isEmpty) {
...@@ -308,14 +314,24 @@ class BleBluetoothManager<T> { ...@@ -308,14 +314,24 @@ class BleBluetoothManager<T> {
debugPrint('🔥🔥🔥 BLE_CONNECT_DEBUG connecting deviceId=$deviceId'); debugPrint('🔥🔥🔥 BLE_CONNECT_DEBUG connecting deviceId=$deviceId');
final completer = Completer<void>(); final completer = Completer<void>();
_connectionSubscription = _ble final connectionStream = prescan
.connectToDevice( ? _ble.connectToAdvertisingDevice(
id: deviceId, id: deviceId,
servicesWithCharacteristicsToDiscover: { withServices: const [],
serviceUuid: [notifyCharacteristicUuid, writeCharacteristicUuid], prescanDuration: const Duration(seconds: 5),
}, servicesWithCharacteristicsToDiscover: {
connectionTimeout: connectionTimeout, serviceUuid: [notifyCharacteristicUuid, writeCharacteristicUuid],
) },
connectionTimeout: connectionTimeout,
)
: _ble.connectToDevice(
id: deviceId,
servicesWithCharacteristicsToDiscover: {
serviceUuid: [notifyCharacteristicUuid, writeCharacteristicUuid],
},
connectionTimeout: connectionTimeout,
);
_connectionSubscription = connectionStream
.listen( .listen(
(update) async { (update) async {
switch (update.connectionState) { switch (update.connectionState) {
...@@ -333,6 +349,7 @@ class BleBluetoothManager<T> { ...@@ -333,6 +349,7 @@ class BleBluetoothManager<T> {
if (enableCharacteristicDiscovery) { if (enableCharacteristicDiscovery) {
await _discoverServicesAndResolveWriteCharacteristic(deviceId); await _discoverServicesAndResolveWriteCharacteristic(deviceId);
} }
await Future<void>.delayed(const Duration(milliseconds: 500));
final notifyReady = await _enableNotifications(deviceId); final notifyReady = await _enableNotifications(deviceId);
if (notifyReady) { if (notifyReady) {
activateDataSource(); activateDataSource();
...@@ -370,8 +387,10 @@ class BleBluetoothManager<T> { ...@@ -370,8 +387,10 @@ class BleBluetoothManager<T> {
'failure=${update.failure}', 'failure=${update.failure}',
); );
_clearCharacteristics(); _clearCharacteristics();
if (!completer.isCompleted && update.failure != null) { if (!completer.isCompleted) {
completer.completeError(update.failure!); completer.completeError(
update.failure ?? StateError('蓝牙连接已断开'),
);
} }
break; break;
} }
......
...@@ -587,7 +587,7 @@ class MonitoringIndexCubit extends Cubit<MonitoringIndexState> { ...@@ -587,7 +587,7 @@ class MonitoringIndexCubit extends Cubit<MonitoringIndexState> {
return; return;
} }
final delaySeconds = ((_bluetoothReconnectAttempts + 1) * 5).clamp(5, 30); final delaySeconds = ((_bluetoothReconnectAttempts + 1) * 8).clamp(8, 40);
debugPrint( debugPrint(
'🔥🔥🔥 BLE_CONNECT_DEBUG auto_reconnect_scheduled ' '🔥🔥🔥 BLE_CONNECT_DEBUG auto_reconnect_scheduled '
'delay=${delaySeconds}s attempts=$_bluetoothReconnectAttempts', 'delay=${delaySeconds}s attempts=$_bluetoothReconnectAttempts',
...@@ -623,7 +623,7 @@ class MonitoringIndexCubit extends Cubit<MonitoringIndexState> { ...@@ -623,7 +623,7 @@ class MonitoringIndexCubit extends Cubit<MonitoringIndexState> {
); );
try { try {
await _bluetoothManager.connectToDeviceId(deviceId); await _bluetoothManager.connectToDeviceId(deviceId, prescan: true);
debugPrint( debugPrint(
'🔥🔥🔥 BLE_CONNECT_DEBUG auto_connect_success ' '🔥🔥🔥 BLE_CONNECT_DEBUG auto_connect_success '
'deviceId=$deviceId isConnected=${_bluetoothManager.isConnected}', 'deviceId=$deviceId isConnected=${_bluetoothManager.isConnected}',
......
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