Commit bf7e72d1 authored by akari's avatar akari

fix: 修复蓝牙相关

parent 65c9e3b0
...@@ -53,6 +53,7 @@ class BleBluetoothManager<T> { ...@@ -53,6 +53,7 @@ class BleBluetoothManager<T> {
Uuid? notifyCharacteristicUuid, Uuid? notifyCharacteristicUuid,
Uuid? writeCharacteristicUuid, Uuid? writeCharacteristicUuid,
this.connectionTimeout = const Duration(seconds: 15), this.connectionTimeout = const Duration(seconds: 15),
this.enableCharacteristicDiscovery = false,
}) : _parser = parser, }) : _parser = parser,
_ble = ble ?? FlutterReactiveBle(), _ble = ble ?? FlutterReactiveBle(),
serviceUuid = serviceUuid =
...@@ -68,6 +69,7 @@ class BleBluetoothManager<T> { ...@@ -68,6 +69,7 @@ class BleBluetoothManager<T> {
final Uuid notifyCharacteristicUuid; final Uuid notifyCharacteristicUuid;
final Uuid writeCharacteristicUuid; final Uuid writeCharacteristicUuid;
final Duration connectionTimeout; final Duration connectionTimeout;
final bool enableCharacteristicDiscovery;
final _scanDevicesController = final _scanDevicesController =
StreamController<List<BluetoothScanDevice>>.broadcast(); StreamController<List<BluetoothScanDevice>>.broadcast();
...@@ -94,6 +96,7 @@ class BleBluetoothManager<T> { ...@@ -94,6 +96,7 @@ class BleBluetoothManager<T> {
StreamSubscription<List<int>>? _notifySubscription; StreamSubscription<List<int>>? _notifySubscription;
Timer? _scanTimer; Timer? _scanTimer;
Future<void> _writeQueue = Future<void>.value(); Future<void> _writeQueue = Future<void>.value();
Future<void>? _connectInProgress;
final Map<String, BluetoothScanDevice> _scannedDevices = {}; final Map<String, BluetoothScanDevice> _scannedDevices = {};
...@@ -253,7 +256,23 @@ class BleBluetoothManager<T> { ...@@ -253,7 +256,23 @@ class BleBluetoothManager<T> {
return connectToDeviceId(device.remoteId); 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( 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) {
...@@ -271,6 +290,7 @@ class BleBluetoothManager<T> { ...@@ -271,6 +290,7 @@ class BleBluetoothManager<T> {
} }
await disconnect(); await disconnect();
await Future<void>.delayed(const Duration(milliseconds: 600));
_connectedDeviceId = deviceId; _connectedDeviceId = deviceId;
_notifyCharacteristic = QualifiedCharacteristic( _notifyCharacteristic = QualifiedCharacteristic(
deviceId: deviceId, deviceId: deviceId,
...@@ -310,7 +330,9 @@ class BleBluetoothManager<T> { ...@@ -310,7 +330,9 @@ class BleBluetoothManager<T> {
debugPrint( debugPrint(
'🔥🔥🔥 BLE_CONNECT_DEBUG state_connected deviceId=$deviceId'); '🔥🔥🔥 BLE_CONNECT_DEBUG state_connected deviceId=$deviceId');
try { try {
await _discoverServicesAndResolveWriteCharacteristic(deviceId); if (enableCharacteristicDiscovery) {
await _discoverServicesAndResolveWriteCharacteristic(deviceId);
}
final notifyReady = await _enableNotifications(deviceId); final notifyReady = await _enableNotifications(deviceId);
if (notifyReady) { if (notifyReady) {
activateDataSource(); activateDataSource();
...@@ -536,7 +558,6 @@ class BleBluetoothManager<T> { ...@@ -536,7 +558,6 @@ class BleBluetoothManager<T> {
) async { ) async {
try { try {
Log.warn('🔥🔥🔥 BLE_DISCOVER_DEBUG start deviceId=$deviceId'); Log.warn('🔥🔥🔥 BLE_DISCOVER_DEBUG start deviceId=$deviceId');
await _ble.discoverAllServices(deviceId);
final services = await _ble.getDiscoveredServices(deviceId); final services = await _ble.getDiscoveredServices(deviceId);
QualifiedCharacteristic? fallbackWriteCharacteristic; QualifiedCharacteristic? fallbackWriteCharacteristic;
QualifiedCharacteristic? fallbackNotifyCharacteristic; QualifiedCharacteristic? fallbackNotifyCharacteristic;
......
...@@ -47,6 +47,7 @@ class MonitoringIndexCubit extends Cubit<MonitoringIndexState> { ...@@ -47,6 +47,7 @@ class MonitoringIndexCubit extends Cubit<MonitoringIndexState> {
bool _isAutoReconnectEnabled = false; bool _isAutoReconnectEnabled = false;
bool _isAutoConnectingBluetooth = false; bool _isAutoConnectingBluetooth = false;
bool _isManualUnbindingBluetooth = false; bool _isManualUnbindingBluetooth = false;
int _bluetoothReconnectAttempts = 0;
bool _isMqttReconnectEnabled = false; bool _isMqttReconnectEnabled = false;
bool _isMqttConnecting = false; bool _isMqttConnecting = false;
String? _mqttDeviceSn; String? _mqttDeviceSn;
...@@ -586,8 +587,13 @@ class MonitoringIndexCubit extends Cubit<MonitoringIndexState> { ...@@ -586,8 +587,13 @@ class MonitoringIndexCubit extends Cubit<MonitoringIndexState> {
return; return;
} }
final delaySeconds = ((_bluetoothReconnectAttempts + 1) * 5).clamp(5, 30);
debugPrint(
'🔥🔥🔥 BLE_CONNECT_DEBUG auto_reconnect_scheduled '
'delay=${delaySeconds}s attempts=$_bluetoothReconnectAttempts',
);
_bluetoothReconnectTimer ??= Timer( _bluetoothReconnectTimer ??= Timer(
const Duration(seconds: 5), Duration(seconds: delaySeconds),
() { () {
_bluetoothReconnectTimer = null; _bluetoothReconnectTimer = null;
_connectBoundBluetoothDevice(); _connectBoundBluetoothDevice();
...@@ -629,7 +635,11 @@ class MonitoringIndexCubit extends Cubit<MonitoringIndexState> { ...@@ -629,7 +635,11 @@ class MonitoringIndexCubit extends Cubit<MonitoringIndexState> {
: '蓝牙自动连接未就绪,等待重试', : '蓝牙自动连接未就绪,等待重试',
)); ));
} }
if (_bluetoothManager.isConnected) {
_bluetoothReconnectAttempts = 0;
}
} catch (e) { } catch (e) {
_bluetoothReconnectAttempts++;
debugPrint( debugPrint(
'🔥🔥🔥 BLE_CONNECT_DEBUG auto_connect_error deviceId=$deviceId error=$e'); '🔥🔥🔥 BLE_CONNECT_DEBUG auto_connect_error deviceId=$deviceId error=$e');
if (!isClosed) { 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