Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
laki_icu_app
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张宏
laki_icu_app
Commits
e28bbcc2
Commit
e28bbcc2
authored
Jun 25, 2026
by
akari
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: 蓝牙连接优化
parent
bee54b14
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
15 deletions
+34
-15
bluetooth_manager.dart
lib/utils/bluetooth/bluetooth_manager.dart
+32
-13
monitoring_index_cubit.dart
lib/views/monitoring/index/cubit/monitoring_index_cubit.dart
+2
-2
No files found.
lib/utils/bluetooth/bluetooth_manager.dart
View file @
e28bbcc2
...
@@ -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
;
}
}
...
...
lib/views/monitoring/index/cubit/monitoring_index_cubit.dart
View file @
e28bbcc2
...
@@ -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
,
3
0
);
final
delaySeconds
=
((
_bluetoothReconnectAttempts
+
1
)
*
8
).
clamp
(
8
,
4
0
);
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}
'
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment