更新眠花糖兼容wifi配置

This commit is contained in:
wyf
2025-10-28 14:51:28 +08:00
parent 7cfd3bf8f5
commit 3c5db3bdf2
14 changed files with 1259 additions and 882 deletions

View File

@@ -44,6 +44,9 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
StreamSubscription<List<ScanResult>>? _scanSubscription;
bool _isDisposed = false;
ScanResult? targetDevice;
bool dealing = false; //是否正在刷新
DateTime _lastTapTime = DateTime.now();
bool haveSuccess = false;
@override
void initState() {
@@ -55,6 +58,7 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
}
void _initController() {
dealing = false;
blueteethBindController.netType.value = 0;
blueteethBindController.connectStatus.value = 0;
blueteethBindController.wifiList = [].obs;
@@ -71,11 +75,11 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
void dispose() {
_isDisposed = true;
_cleanupResources();
_disconnectDevice();
// _disconnectDevice();
super.dispose();
}
void _cleanupResources() {
Future<void> _cleanupResources() async {
// 取消监听器
lisObj?.cancel();
lisObj = null;
@@ -88,12 +92,7 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
_scanSubscription?.cancel();
_scanSubscription = null;
// 断开蓝牙连接
if (blueteethBindController.currentDevice != null) {
blueteethBindController.currentDevice!.disconnect();
blueteethBindController.currentDevice = null;
}
await _disconnectDevice();
// 停止扫描
FlutterBluePlus.stopScan();
}
@@ -183,7 +182,7 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(
themeController.currentColor.sc1,
Colors.white,
),
),
);
@@ -196,7 +195,7 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(
themeController.currentColor.sc1,
Colors.white,
),
),
);
@@ -526,12 +525,13 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
wifiItem['ssid'] ??
'未命名'.tr,
onConfirm: () async {
blueteethBindController
.currentDevice!
.logingStream
.listen((event) {
ef.log("[命令日志]:$event");
});
// blueteethBindController
// .currentDevice!
// .logingStream
// .listen((event) {
// ef.log("[命令日志]:$event");
// });
haveSuccess = false;
blueteethBindController
.selectWifi
.value = wifiItem;
@@ -541,11 +541,53 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
.model.wifiPass!,
blueteethBindController
.currentDevice!);
//todo 查询内存如果关键内存小于20000k重启设备
bool memoryFlag =
await queryMemory(
blueteethBindController
.currentDevice!);
if (!memoryFlag) {
await rebootDevice(
blueteethBindController
.currentDevice!);
await dealWifi(
widget.deviceInfo
.value[
'bind_mac_a'],
needSuccess: true);
// return;
}
if (!flag) {
blueteethBindController
.selectWifi
.value = {};
// TopSlideNotification.show(
// context,
// textColor:
// themeController
// .currentColor
// .sc9,
// text:
// '蓝牙已断开,请点击下方刷新按钮重试'
// .tr);
return;
}
if (flag) {
var aa = await getDeviceWifiStatus(
blueteethBindController
.currentDevice!,
1,link: true);
1,
link: true);
aa = await restoreWifi(
blueteethBindController
.currentDevice!,
aa);
if (!memoryFlag) {
aa = await rebootDeviceCurrent(
blueteethBindController
.currentDevice!,
aa);
}
blueteethBindController
.selectWifi
.value = {};
@@ -558,6 +600,10 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
blueteethBindController
.connect_wifi
.value = {};
blueteethBindController
.wifiStatus
.value ==
0;
}
blueteethBindController
.wifiStatus
@@ -572,19 +618,28 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
blueteethBindController
.currentDeviceMac!
.value);
TopSlideNotification
.show(
context,
text: "配网成功".tr,
textColor:
themeController
.currentColor
.sc2,
);
if (!haveSuccess) {
TopSlideNotification
.show(
context,
text: "配网成功".tr,
textColor:
themeController
.currentColor
.sc2,
);
}
} else {
//读取不到wifi信息
blueteethBindController
.connect_wifi
.value = {};
blueteethBindController
.selectWifi
.value = {};
blueteethBindController
.wifiStatus
.value = 0;
TopSlideNotification
.show(
context,
@@ -599,6 +654,11 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
blueteethBindController
.connect_wifi
.value = {};
blueteethBindController
.selectWifi
.value = {};
blueteethBindController
.wifiStatus.value = 0;
TopSlideNotification.show(
context,
text: "配网失败".tr,
@@ -639,9 +699,7 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
valueColor:
AlwaysStoppedAnimation<
Color>(
themeController
.currentColor
.sc1,
Colors.white,
),
),
)
@@ -662,6 +720,20 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
horizontal: 20.rpx, vertical: 10.rpx),
borderRadius: 20.rpx,
onTap: () async {
// if (dealing) {
// return;
// }
if (DateTime.now()
.difference(_lastTapTime) <
Duration(seconds: 2)) {
return; // 防止快速重复点击
}
_lastTapTime = DateTime.now();
blueteethBindController.netType.value = 0;
blueteethBindController
.blueConnectFlag.value = 0;
blueteethBindController.wifiList.value =
[];
var bluetoothState =
await FlutterBluePlus.isOn;
blueteethBindController.bluetoothStatus
@@ -678,10 +750,17 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
1) {
blueteethBindController
.blueConnectFlag.value = 0;
dealWifi(widget.deviceInfo.macA)
.then((aa) {
print("object");
});
try {
await dealWifi(
widget
.deviceInfo['bind_mac_a'],
needTip: false)
.then((aa) {
print("object");
});
} catch (e) {
ef.log("$e");
}
return;
}
@@ -730,6 +809,7 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
.currentColor.sc9,
);
});
await initWifiStatusAndWifiList();
} else {
blueteethBindController.netType.value =
1;
@@ -782,35 +862,54 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
);
}
Future<void> initWifiStatusAndWifiList() async {
Future<void> initWifiStatusAndWifiList(
{int retryCount = 0, bool needSuccess = false}) async {
if (_isDisposed) return;
// 取消之前的监听器
if (lisObj != null) {
lisObj!.cancel();
lisObj = null;
}
bool wifiStatus = false;
var aa =
await getDeviceWifiStatus(blueteethBindController.currentDevice!, 0);
if (aa != null && aa is Map) {
updateDeviceBindStatus(blueteethBindController.currentDeviceMac!.value);
wifiStatus = true;
blueteethBindController.connect_wifi.value = aa;
} else {
wifiStatus = false;
try {
// 获取WiFi状态
var aa =
await getDeviceWifiStatus(blueteethBindController.currentDevice!, 0);
if (aa != null && aa is Map) {
updateDeviceBindStatus(blueteethBindController.currentDeviceMac!.value);
wifiStatus = true;
blueteethBindController.connect_wifi.value = aa;
if (needSuccess) {
haveSuccess = true;
TopSlideNotification.show(
context,
text: "配网成功".tr,
textColor: themeController.currentColor.sc2,
);
}
} else {
wifiStatus = false;
}
blueteethBindController.wifiStatus.value = wifiStatus ? 1 : 0;
} catch (e) {
print("获取WiFi状态异常: $e");
blueteethBindController.wifiStatus.value = 0;
}
blueteethBindController.wifiStatus.value = wifiStatus ? 1 : 0;
List wifiList = [];
try {
// 获取WiFi列表
final result = await getWifiList(blueteethBindController.currentDevice!);
blueteethBindController.wifiConnectStatus.value = 1;
blueteethBindController.updateAll();
if (result is List) {
wifiList = result;
}
} catch (e) {
print("异常: $e");
print("获取WiFi列表异常: $e");
blueteethBindController.wifiConnectStatus.value = 0;
blueteethBindController.updateAll();
}
@@ -818,32 +917,87 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
if (_isDisposed) return;
if (wifiList.isNotEmpty) {
// 成功获取WiFi列表
blueteethBindController.connectStatus.value = 1;
blueteethBindController.updateAll();
TopSlideNotification.show(
context,
text: "获取wifi列表成功".tr,
textColor: themeController.currentColor.sc2,
);
// TopSlideNotification.show(
// context,
// text: "获取wifi列表成功".tr,
// textColor: themeController.currentColor.sc2,
// );
blueteethBindController.wifiList.value = wifiList;
blueteethBindController.updateAll();
} else {
// 获取WiFi列表失败尝试重新扫描
if (retryCount < 2) {
// 最多重试2次
print("${retryCount + 1}次获取WiFi列表失败尝试重新扫描...");
// TopSlideNotification.show(
// context,
// text: "正在重新扫描WiFi...".tr,
// textColor: themeController.currentColor.sc2,
// );
try {
// 发送关闭并重新打开WiFi扫描命令
await sendCloseAndOpenWscanCommand(
blueteethBindController.currentDevice!);
// 等待一段时间让设备重新扫描
await Future.delayed(Duration(seconds: 3));
// 递归调用自身进行重试
await initWifiStatusAndWifiList(retryCount: retryCount + 1);
return; // 重试后直接返回,避免执行后面的代码
} catch (e) {
print("重新扫描WiFi失败: $e");
// 继续执行下面的失败处理
}
}
// 重试次数用尽或重试失败,显示最终失败提示
TopSlideNotification.show(
context,
text: "获取wifi列表失败".tr,
textColor: themeController.currentColor.sc9,
);
// 可选重置WiFi连接状态
blueteethBindController.connectStatus.value = 0;
blueteethBindController.updateAll();
}
lisObj = blueteethBindController.currentDevice!.statusStream
.listen((onData) async {
if (onData.status == BleEventType.recvLineLog) {
final line = onData.val;
print("[bleee]:" + line);
edm.EasyDartModule.logger.info("[bleee]:" + line);
DailyLogUtils.writeLog("[bleee]:" + line);
}
});
// 设置日志监听器
if (!_isDisposed && blueteethBindController.currentDevice != null) {
lisObj = blueteethBindController.currentDevice!.statusStream
.listen((onData) async {
if (_isDisposed) return;
if (onData.status == BleEventType.recvLineLog) {
final line = onData.val;
print("[bleee]: $line");
edm.EasyDartModule.logger.info("[bleee]: $line");
DailyLogUtils.writeLog("[bleee]: $line");
}
// 添加连接状态监听
if (onData.status == BleEventType.disconnected) {
print("蓝牙连接已断开");
blueteethBindController.blueConnectFlag.value = 1;
blueteethBindController.updateAll();
// 显示断开提示
// if (!_isDisposed) {
// TopSlideNotification.show(
// context,
// text: "蓝牙连接已断开,请重新连接".tr,
// textColor: themeController.currentColor.sc9,
// );
// }
_autoReconnect();
}
});
}
}
Widget getWifiIconByRsso(wifiItem) {
@@ -894,16 +1048,20 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
}
}
Future<void> dealWifi(String mac) async {
if (_isDisposed) return;
final blueteethBindController = Get.find<BlueteethBindController>();
final themeController = Get.find<ThemeController>();
// 清理之前的资源
_cleanupResources();
Future<void> dealWifi(String mac,
{bool needTip = true, bool needSuccess = false}) async {
if (dealing) {
return;
}
try {
dealing = true;
if (_isDisposed) return;
final blueteethBindController = Get.find<BlueteethBindController>();
final themeController = Get.find<ThemeController>();
// 清理之前的资源
await _cleanupResources();
// 检查蓝牙状态
var bluetoothState = await FlutterBluePlus.isOn;
blueteethBindController.bluetoothStatus.value = bluetoothState;
@@ -922,13 +1080,14 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
_timeoutTimer = Timer(Duration(seconds: 20), () {
if (!isConnected && !_isDisposed) {
blueteethBindController.blueConnectFlag.value = 1;
blueteethBindController.updateAll();
blueteethBindController.netType.value = 3;
TopSlideNotification.show(
context,
text: "设备连接超时,请重试".tr,
text: "设备连接超时,请点击刷新重试".tr,
textColor: themeController.currentColor.sc9,
);
FlutterBluePlus.stopScan();
blueteethBindController.updateAll();
}
});
@@ -954,33 +1113,76 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
_timeoutTimer?.cancel();
_scanSubscription?.cancel();
var streamlog;
try {
THapp bledevice = THapp(device: targetDevice!.device);
await bledevice.device.connect();
var res2 = bledevice.isConnected;
blueteethBindController.currentDevice = bledevice;
streamlog = blueteethBindController.currentDevice!.logingStream
.listen((log) {
ef.log("[传感器设备日志]: $log");
edm.EasyDartModule.logger.info("[传感器设备日志]: $log");
});
bledevice.autoConnect = true;
if (res2 && !_isDisposed) {
const int maxRetry = 3;
int retryCount = 0;
bool connected = false;
while (retryCount < maxRetry && !_isDisposed) {
try {
await bledevice.device.connect();
connected = bledevice.isConnected;
if (connected) break;
} catch (e) {
retryCount++;
print("[蓝牙连接错误] 第${retryCount}次重试: $e");
edm.EasyDartModule.logger
.info("[蓝牙连接错误] 第${retryCount}次重试: $e");
DailyLogUtils.writeLog("[蓝牙连接错误] 第${retryCount}次重试: $e");
// 延迟重试
await Future.delayed(const Duration(seconds: 2));
}
}
if (connected && !_isDisposed) {
blueteethBindController.blueConnectFlag.value = 2;
TopSlideNotification.show(
context,
text: "连接成功".tr,
textColor: themeController.currentColor.sc2,
);
blueteethBindController.currentDevice = bledevice;
lisObj = blueteethBindController.currentDevice!.statusStream
.listen((onData) async {
// TopSlideNotification.show(
// context,
// text: "蓝牙连接成功".tr,
// textColor: themeController.currentColor.sc2,
// );
lisObj = bledevice.statusStream.listen((onData) async {
if (_isDisposed) return;
if (onData.status == BleEventType.recvLineLog) {
final line = onData.val;
print("[bleee]:" + line);
edm.EasyDartModule.logger.info("[bleee]:" + line);
DailyLogUtils.writeLog("[bleee]:" + line);
print("[bleee]: $line");
edm.EasyDartModule.logger.info("[bleee]: $line");
DailyLogUtils.writeLog("[bleee]: $line");
}
// 添加连接状态监听
if (onData.status == BleEventType.disconnected) {
print("蓝牙连接已断开");
blueteethBindController.blueConnectFlag.value = 1;
blueteethBindController.updateAll();
// 显示断开提示
// if (!_isDisposed) {
// TopSlideNotification.show(
// context,
// text: "蓝牙连接已断开,请重新连接".tr,
// textColor: themeController.currentColor.sc9,
// );
// }
_autoReconnect();
}
if (onData.status == BleEventType.ready) {
var aa = await getDeviceNetVersion(
blueteethBindController.currentDevice!, 0);
var aa = await getDeviceNetVersion(bledevice, 0);
if (aa == "4g") {
blueteethBindController.netType.value = 2;
blueteethBindController.updateAll();
@@ -1007,27 +1209,30 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
blueteethBindController.updateAll();
blueteethBindController.wifiConnectStatus.value = 0;
blueteethBindController.updateAll();
await initWifiStatusAndWifiList();
await initWifiStatusAndWifiList(needSuccess: needSuccess);
}
dealing = false;
}
});
} else {
TopSlideNotification.show(
context,
text: "蓝牙绑定.连接失败".tr,
text: "设备连接失败,请点击刷新重试".tr,
textColor: themeController.currentColor.sc9,
);
}
} catch (e) {
blueteethBindController.blueConnectFlag.value = 1;
TopSlideNotification.show(
context,
text: "设备连接失败".tr,
text: "设备连接失败,请点击刷新重试".tr,
textColor: themeController.currentColor.sc9,
);
} finally {
streamlog.close();
}
}
});
await Future.delayed(Duration(seconds: 20));
} catch (e) {
if (!_isDisposed) {
@@ -1036,10 +1241,12 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
text: "扫描过程中发生错误".tr,
textColor: themeController.currentColor.sc9,
);
dealing = false;
}
} finally {
_timeoutTimer?.cancel();
await FlutterBluePlus.stopScan();
dealing = false;
}
}
@@ -1105,12 +1312,74 @@ class _MHTWifiAfterPageState extends State<MHTWifiAfterPage> {
Future<void> _disconnectDevice() async {
try {
// THapp bledevice = THapp(device: widget.deviceInfo.scanResult.device);
THapp bledevice = THapp(device: targetDevice!.device);
await bledevice.disconnect();
if (blueteethBindController.currentDevice != null) {
await blueteethBindController.currentDevice!.disconnect();
// blueteethBindController.currentDevice = null;
}
DailyLogUtils.writeLog("关闭蓝牙连接成功".tr);
} catch (e) {
DailyLogUtils.writeError("关闭蓝牙连接失败: $e");
} finally {
// dealing = false;
}
}
Future restoreWifi(THapp tHapp, aa) async {
if (aa != null && aa is Map) {
return aa;
}
await sendCloseAndOpenWscanCommand(tHapp);
return await getDeviceWifiStatus(blueteethBindController.currentDevice!, 4,
link: true);
}
Future rebootDeviceCurrent(THapp tHapp, aa) async {
if (aa != null && aa is Map) {
return aa;
}
blueteethBindController.connect_wifi.value = {};
await rebootDevice(
blueteethBindController.currentDevice!,
);
await dealWifi(
widget.deviceInfo['bind_mac_a'],
);
return await getDeviceWifiStatus(blueteethBindController.currentDevice!, 1,
link: true);
}
int _retryCount = 0;
final int _maxRetry = 3;
Future<void> _autoReconnect() async {
if (blueteethBindController.currentDevice == null) return;
while (_retryCount < _maxRetry) {
try {
print("[蓝牙重连] 第${_retryCount + 1}次尝试连接...");
await blueteethBindController.currentDevice!.connect();
// 检查是否连接成功
var isConnected =
await blueteethBindController.currentDevice!.isConnected;
if (isConnected) {
print("[蓝牙重连] 连接成功 ✅");
_retryCount = 0; // 重置重试次数
return;
} else {
throw Exception("连接失败");
}
} catch (e) {
_retryCount++;
print("[蓝牙重连] 第$_retryCount次连接失败$e");
if (_retryCount >= _maxRetry) {
print("[蓝牙重连] 已达到最大重试次数($_maxRetry),停止重连 ❌");
break;
}
// 等待 2 秒后重试
await Future.delayed(Duration(seconds: 2));
}
}
}
}