Merge branch 'develop_sky_liyi' into 'develop_sky'
fix:调整mtu设置 See merge request StarlockTeam/app-starlock!273
This commit is contained in:
commit
1b611ca9de
@ -144,7 +144,7 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver, BaseWidget {
|
|||||||
break;
|
break;
|
||||||
case AppLifecycleState.paused:
|
case AppLifecycleState.paused:
|
||||||
// AppLog.log('App--->进入后台');
|
// AppLog.log('App--->进入后台');
|
||||||
BlueManage().disconnect();
|
BlueManage.instance.disconnect();
|
||||||
break;
|
break;
|
||||||
case AppLifecycleState.resumed:
|
case AppLifecycleState.resumed:
|
||||||
// AppLog.log('App--->进入前台');
|
// AppLog.log('App--->进入前台');
|
||||||
|
|||||||
@ -85,6 +85,12 @@ class BlueManage {
|
|||||||
return _manager;
|
return _manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 提供全局单例访问入口
|
||||||
|
static BlueManage get instance {
|
||||||
|
_manager ??= BlueManage._init();
|
||||||
|
return _manager!;
|
||||||
|
}
|
||||||
|
|
||||||
BlueManage? get manager => shareManager();
|
BlueManage? get manager => shareManager();
|
||||||
|
|
||||||
void _initBlue() {
|
void _initBlue() {
|
||||||
@ -94,9 +100,13 @@ class BlueManage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _initGetMtuSubscription() {
|
void _initGetMtuSubscription() {
|
||||||
_mtuSubscription ??= bluetoothConnectDevice!.mtu.listen((int value) {
|
// 先取消之前的订阅,避免重复监听
|
||||||
|
_mtuSubscription?.cancel();
|
||||||
|
_mtuSubscription = null;
|
||||||
|
|
||||||
|
_mtuSubscription = bluetoothConnectDevice!.mtu.listen((int value) {
|
||||||
_mtuSize = value - 3;
|
_mtuSize = value - 3;
|
||||||
AppLog.log('_mtuSizeValue:$value mtuSize:$_mtuSize');
|
AppLog.log('设备MTU变化 - 原始值:$value 计算后MTU:$_mtuSize 设备:${bluetoothConnectDevice?.remoteId.str}');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -954,6 +964,12 @@ class BlueManage {
|
|||||||
Future<void> disconnect() async {
|
Future<void> disconnect() async {
|
||||||
try {
|
try {
|
||||||
connectDeviceMacAddress = '';
|
connectDeviceMacAddress = '';
|
||||||
|
|
||||||
|
// 清理MTU监听
|
||||||
|
_mtuSubscription?.cancel();
|
||||||
|
_mtuSubscription = null;
|
||||||
|
_mtuSize = 20; // 重置MTU为默认值
|
||||||
|
|
||||||
if (bluetoothConnectionState == BluetoothConnectionState.connected) {
|
if (bluetoothConnectionState == BluetoothConnectionState.connected) {
|
||||||
//加快蓝牙断连
|
//加快蓝牙断连
|
||||||
await bluetoothConnectDevice!.disconnect(timeout: 3);
|
await bluetoothConnectDevice!.disconnect(timeout: 3);
|
||||||
@ -975,10 +991,29 @@ class BlueManage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取当前MTU信息,用于调试
|
||||||
|
String getMtuDebugInfo() {
|
||||||
|
return 'MTU Debug Info:\n'
|
||||||
|
'- Current MTU Size: $_mtuSize\n'
|
||||||
|
'- Connected Device: ${bluetoothConnectDevice?.remoteId.str ?? "None"}\n'
|
||||||
|
'- Device Name: $connectDeviceName\n'
|
||||||
|
'- Connection State: $bluetoothConnectionState\n'
|
||||||
|
'- MTU Subscription Active: ${_mtuSubscription != null}';
|
||||||
|
}
|
||||||
|
|
||||||
void disposed() {
|
void disposed() {
|
||||||
_sendStreamSubscription?.cancel();
|
_sendStreamSubscription?.cancel();
|
||||||
_mtuSubscription!.cancel();
|
_mtuSubscription?.cancel();
|
||||||
_adapterStateStateSubscription!.cancel();
|
_adapterStateStateSubscription?.cancel();
|
||||||
_connectionStateSubscription!.cancel();
|
_connectionStateSubscription?.cancel();
|
||||||
|
|
||||||
|
// 重置状态
|
||||||
|
_mtuSize = 20;
|
||||||
|
connectDeviceName = '';
|
||||||
|
connectDeviceMacAddress = '';
|
||||||
|
bluetoothConnectDevice = null;
|
||||||
|
scanDevices.clear();
|
||||||
|
allData.clear();
|
||||||
|
lastTimeData.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user