This commit is contained in:
wyf
2025-05-20 14:00:44 +08:00
parent 75ddfca402
commit 0a8cffa4c6
48 changed files with 5221 additions and 1733 deletions

View File

@@ -1,6 +1,9 @@
import 'dart:async';
import 'package:easydevice/easydevice.dart';
import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:flutter_svg/svg.dart';
import 'package:flutterflow_ui/flutterflow_ui.dart';
import 'package:vbvs_app/common/color/appConstants.dart';
@@ -15,6 +18,8 @@ import 'package:vbvs_app/controller/main_bottom/global_controller.dart';
import 'package:vbvs_app/controller/person/person_controller.dart';
import 'package:vbvs_app/controller/theme_controller/ThemeController.dart';
import 'package:vbvs_app/controller/user_info_controller.dart';
import 'package:vbvs_app/model/BleDeviceData.dart';
import 'package:vbvs_app/pages/device_bind/blueteeth_device_page.dart';
import 'package:vbvs_app/pages/device_bind/componnet/bind_dialog.dart';
class WifiPage extends StatefulWidget {
@@ -36,10 +41,69 @@ class _WifiPageState extends State<WifiPage> {
@override
void initState() {
super.initState();
blueteethBindController.connectStatus.value = 0;
blueteethBindController.wifiList = [].obs;
blueteethBindController.wifiStatus = 0.obs;
blueteethBindController.connect_wifi.value = {};
initWifiStatusAndWifiList();
blueteethBindController.selectWifi.value = {};
if (widget.type == null) {
THapp bledevice = blueteethBindController.currentDevice!;
bledevice.device.connect().then((Value) {
var res2 = bledevice.isConnected;
if (res2) {
// WidgetsBinding.instance.addPostFrameCallback((_) {
// TopSlideNotification.show(
// context,
// text: "蓝牙绑定.连接成功".tr,
// textColor: themeController.currentColor.sc2,
// );
// });
blueteethBindController.currentDevice = bledevice;
if (lisObj != null) {
lisObj!.cancel();
}
var aa;
lisObj = blueteethBindController.currentDevice!.statusStream
.listen((onData) async {
if (onData.status == BleEventType.recvLineLog) {
final line = onData.val;
print("[bleee]:" + line);
}
if (onData.status == BleEventType.ready) {
aa = await getDeviceNetVersion(
blueteethBindController.currentDevice!, 1);
if (aa == "4g") {
TopSlideNotification.show(
context,
text: "4g设备配置wifi提示".tr,
textColor: themeController.currentColor.sc2,
);
blueteethBindController.connectStatus.value = 1;
blueteethBindController.updateAll();
Future.delayed(const Duration(seconds: 1), () {
Get.toNamed("/calibrationPage", arguments: 1);
});
} else {
await initWifiStatusAndWifiList();
}
}
});
} else {
WidgetsBinding.instance.addPostFrameCallback((_) {
TopSlideNotification.show(
context,
text: "连接失败".tr,
textColor: themeController.currentColor.sc9,
);
});
}
});
} else {
dealWifi(widget.type).then((aa) {
print("object");
});
}
}
@override
@@ -79,14 +143,38 @@ class _WifiPageState extends State<WifiPage> {
alignment: Alignment.center,
children: [
/// 居中标题
Text(
'wifi页.标题'.tr,
style: FlutterFlowTheme.of(context).bodyMedium.override(
fontFamily: 'Readex Pro',
color: themeController.currentColor.sc3,
letterSpacing: 0,
fontSize: 30.rpx,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'wifi页.标题'.tr,
style:
FlutterFlowTheme.of(context).bodyMedium.override(
fontFamily: 'Readex Pro',
color: themeController.currentColor.sc3,
letterSpacing: 0,
fontSize: 30.rpx,
),
),
SizedBox(
width: 14.rpx,
),
Obx(() {
if (blueteethBindController.connectStatus.value ==
0) {
return SizedBox(
width: 24.rpx,
height: 24.rpx,
child: CircularProgressIndicator(
strokeWidth: 1,
valueColor:
AlwaysStoppedAnimation<Color>(Colors.white),
),
);
}
return Container();
}),
],
),
/// 左边返回按钮
@@ -100,15 +188,16 @@ class _WifiPageState extends State<WifiPage> {
child: CustomCard(
borderRadius: 20.rpx,
onTap: () async {
if (blueteethBindController.wifiStatus.value != 1) {
TopSlideNotification.show(
context,
text: "wifi页.需配网".tr,
textColor: themeController.currentColor.sc9,
);
} else {
Get.toNamed("/calibrationPage", arguments: 1);
}
// if (blueteethBindController.wifiStatus.value != 1) {
// TopSlideNotification.show(
// context,
// text: "wifi页.需配网".tr,
// textColor: themeController.currentColor.sc9,
// );
// } else {
// Get.toNamed("/calibrationPage", arguments: 1);
// }
Get.toNamed("/calibrationPage", arguments: 1);
},
colors: [
themeController.currentColor.sc1,
@@ -460,8 +549,11 @@ class _WifiPageState extends State<WifiPage> {
wifiItem['ssid'] ??
'未命名'.tr,
onConfirm: () async {
showLoadingDialog(
context); // 显示 loading
// showLoadingDialog(
// context); // 显示 loading
blueteethBindController
.selectWifi
.value = wifiItem;
bool flag = await sendWifiSetting(
wifiItem,
blueteethBindController
@@ -478,7 +570,10 @@ class _WifiPageState extends State<WifiPage> {
var aa = await getDeviceWifiStatus(
blueteethBindController
.currentDevice!,
20);
1);
blueteethBindController
.selectWifi
.value = {};
if (aa != null &&
aa is Map) {
wifiStatus = true;
@@ -503,7 +598,7 @@ class _WifiPageState extends State<WifiPage> {
? 1
: 0;
if (wifiStatus) {
Navigator.pop(context);
// Navigator.pop(context);
TopSlideNotification
.show(
context,
@@ -519,7 +614,7 @@ class _WifiPageState extends State<WifiPage> {
blueteethBindController
.updateAll();
} else {
Navigator.pop(context);
// Navigator.pop(context);
TopSlideNotification
.show(
context,
@@ -536,7 +631,7 @@ class _WifiPageState extends State<WifiPage> {
.updateAll();
}
} else {
Navigator.pop(context);
// Navigator.pop(context);
TopSlideNotification.show(
context,
text: "wifi页.配网失败".tr,
@@ -574,7 +669,24 @@ class _WifiPageState extends State<WifiPage> {
.sc3,
),
),
getWifiIconByRsso(wifiItem),
if (blueteethBindController
.selectWifi.value ==
wifiItem)
SizedBox(
width: 32.rpx,
height: 32.rpx,
child:
CircularProgressIndicator(
strokeWidth: 1,
valueColor:
AlwaysStoppedAnimation<
Color>(
Colors.white),
),
)
else
getWifiIconByRsso(
wifiItem),
],
),
))
@@ -589,6 +701,9 @@ class _WifiPageState extends State<WifiPage> {
horizontal: 20.rpx, vertical: 10.rpx),
borderRadius: 20.rpx,
onTap: () async {
blueteethBindController
.connectStatus.value = 0;
blueteethBindController.updateAll();
print("点击刷新");
await initWifiList();
TopSlideNotification.show(
@@ -645,93 +760,92 @@ class _WifiPageState extends State<WifiPage> {
);
}
Widget _buildDeviceCard(BuildContext context,
{required String title, required String imageUrl, required String type}) {
return CustomCard(
borderRadius: 20.rpx, // 圆角大小
onTap: () {
if (type != null) {
if (type == '1') {
Get.toNamed("/blueteethDevice");
}
}
},
colors: [themeController.currentColor.sc17], // 背景色
child: Container(
width: double.infinity,
height: MediaQuery.sizeOf(context).height * 0.135,
constraints: BoxConstraints(
minHeight: 220.rpx,
),
padding: EdgeInsetsDirectional.fromSTEB(77.rpx, 0, 21.rpx, 0),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
title,
style: FlutterFlowTheme.of(context).bodyMedium.override(
fontFamily: 'Inter',
color: const Color(0xFFC2CED7),
fontSize: 30.rpx,
letterSpacing: 0.0,
),
),
ClipRRect(
borderRadius: BorderRadius.circular(8.rpx),
child: Image.asset(
imageUrl,
width: 212.rpx,
height: 168.rpx,
),
),
],
),
),
);
}
void initWifiStatusAndWifiList() {
Future<void> initWifiStatusAndWifiList() async {
if (lisObj != null) {
lisObj!.cancel();
}
bool wifiStatus = false;
var aa =
await getDeviceWifiStatus(blueteethBindController.currentDevice!, 1);
if (aa != null && aa is Map) {
wifiStatus = true;
blueteethBindController.connect_wifi.value = aa;
}
blueteethBindController.wifiStatus.value = wifiStatus == true ? 1 : 0;
List wifiList = [];
try {
final result = await getWifiList(blueteethBindController.currentDevice!);
if (result is List) {
wifiList = result;
}
} catch (e) {
print("异常: $e");
}
if (wifiList.length > 0) {
blueteethBindController.connectStatus.value = 1;
blueteethBindController.updateAll();
// Navigator.pop(context);
TopSlideNotification.show(
context,
text: "获取wifi列表成功".tr,
textColor: themeController.currentColor.sc2,
);
blueteethBindController.wifiList.value = wifiList;
blueteethBindController.updateAll();
} else {
// Navigator.pop(context);
TopSlideNotification.show(
context,
text: "获取wifi列表失败".tr,
textColor: themeController.currentColor.sc9,
);
}
lisObj = blueteethBindController.currentDevice!.statusStream
.listen((onData) async {
if (onData.status == BleEventType.recvLineLog) {
final line = onData.val;
print("[bleee]:" + line);
}
if (onData.status == BleEventType.ready) {
showLoadingDialog(context, title: "获取wifi列表中...".tr);
bool wifiStatus = false;
var aa = await getDeviceWifiStatus(
blueteethBindController.currentDevice!, 10);
if (aa != null && aa is Map) {
wifiStatus = true;
blueteethBindController.connect_wifi.value = aa;
}
blueteethBindController.wifiStatus.value = wifiStatus == true ? 1 : 0;
List wifiList =
await getWifiList(blueteethBindController.currentDevice!);
if (wifiList.length > 0) {
Navigator.pop(context);
TopSlideNotification.show(
context,
text: "获取wifi列表成功".tr,
textColor: themeController.currentColor.sc2,
);
blueteethBindController.wifiList.value = wifiList;
blueteethBindController.updateAll();
} else {
Navigator.pop(context);
TopSlideNotification.show(
context,
text: "获取wifi列表失败".tr,
textColor: themeController.currentColor.sc9,
);
}
}
// if (onData.status == BleEventType.ready) {
// // showLoadingDialog(context, title: "获取wifi列表中...".tr);
// bool wifiStatus = false;
// var aa = await getDeviceWifiStatus(
// blueteethBindController.currentDevice!, 1);
// if (aa != null && aa is Map) {
// wifiStatus = true;
// blueteethBindController.connect_wifi.value = aa;
// }
// blueteethBindController.wifiStatus.value = wifiStatus == true ? 1 : 0;
// List wifiList = [];
// try {
// final result =
// await getWifiList(blueteethBindController.currentDevice!);
// if (result is List) {
// wifiList = result;
// }
// } catch (e) {
// print("异常: $e");
// }
// if (wifiList.length > 0) {
// blueteethBindController.connectStatus.value = 1;
// blueteethBindController.updateAll();
// // Navigator.pop(context);
// TopSlideNotification.show(
// context,
// text: "获取wifi列表成功".tr,
// textColor: themeController.currentColor.sc2,
// );
// blueteethBindController.wifiList.value = wifiList;
// blueteethBindController.updateAll();
// } else {
// Navigator.pop(context);
// TopSlideNotification.show(
// context,
// text: "获取wifi列表失败".tr,
// textColor: themeController.currentColor.sc9,
// );
// }
// }
});
}
@@ -740,6 +854,8 @@ class _WifiPageState extends State<WifiPage> {
var wifiList = await getWifiList(blueteethBindController.currentDevice!);
print(wifiList);
if (wifiList.length > 0) {
blueteethBindController.connectStatus.value = 1;
blueteethBindController.wifiList.value = wifiList;
blueteethBindController.updateAll();
}
@@ -802,4 +918,150 @@ class _WifiPageState extends State<WifiPage> {
);
}
}
Future<void> dealWifi(String mac) async {
// bodyDeviceController.wifiMac = mac;
// Get.toNamed("/wifiPage", arguments: 2);
// return;
final blueteethBindController = Get.find<BlueteethBindController>();
final themeController = Get.find<ThemeController>();
// 显示加载对话框
// showLoadingDialog(Get.context!, title: "连接中...".tr);
// 设置超时定时器
Timer? timeoutTimer;
bool isConnected = false;
try {
// 开始扫描蓝牙设备
await FlutterBluePlus.startScan(timeout: Duration(seconds: 10));
// 设置超时20秒
timeoutTimer = Timer(Duration(seconds: 20), () {
try {
if (!isConnected) {
// Navigator.of(context).pop(); // 先关闭 dialog
WidgetsBinding.instance.addPostFrameCallback((_) {
TopSlideNotification.show(
context,
text: "设备连接超时,请重试".tr,
textColor: themeController.currentColor.sc9,
);
});
FlutterBluePlus.stopScan();
}
} catch (e) {
print(e);
}
});
// 监听扫描结果
StreamSubscription<List<ScanResult>>? scanSubscription;
scanSubscription = FlutterBluePlus.scanResults.listen((results) async {
// 过滤出符合条件的设备
ScanResult? targetDevice;
for (var r in results) {
if (r.advertisementData.manufacturerData.containsKey(0xFFED)) {
List<int> rawData = r.advertisementData.manufacturerData[0xFFED]!;
BleDeviceData deviceData = parseBleData(rawData);
String deviceMac =
deviceData.deviceId.replaceAll(':', '').toLowerCase();
if (deviceMac == mac.toLowerCase()) {
targetDevice = r;
break;
}
}
}
if (targetDevice != null && !isConnected) {
isConnected = true;
FlutterBluePlus.stopScan();
scanSubscription?.cancel();
timeoutTimer?.cancel();
try {
// 连接设备
// await targetDevice.device.connect();
THapp bledevice = THapp(device: targetDevice.device);
await bledevice.device.connect();
var res2 = bledevice.isConnected;
if (res2) {
// Navigator.pop(context);
TopSlideNotification.show(
context,
text: "蓝牙绑定.连接成功".tr,
textColor: themeController.currentColor.sc2,
);
blueteethBindController.currentDevice = bledevice;
if (lisObj != null) {
lisObj!.cancel();
}
var aa;
lisObj = blueteethBindController.currentDevice!.statusStream
.listen((onData) async {
if (onData.status == BleEventType.recvLineLog) {
final line = onData.val;
print("[bleee]:" + line);
}
if (onData.status == BleEventType.ready) {
aa = await getDeviceNetVersion(
blueteethBindController.currentDevice!, 1);
if (aa == "4g") {
// TopSlideNotification.show(
// Get.context!,
// text: "4g设备配置wifi提示".tr,
// textColor: themeController.currentColor.sc9,
// );
WidgetsBinding.instance.addPostFrameCallback((_) {
TopSlideNotification.show(
context,
text: "4g设备配置wifi提示".tr,
textColor: themeController.currentColor.sc9,
);
});
return;
} else {
// Get.toNamed("/wifiPage", arguments: 2);
await initWifiStatusAndWifiList();
}
}
});
// Get.toNamed("/wifiPage", arguments: {bledevice});
} else {
// Navigator.pop(context);
TopSlideNotification.show(
context,
text: "蓝牙绑定.连接失败".tr,
textColor: themeController.currentColor.sc9,
);
}
} catch (e) {
// Navigator.of(Get.context!).pop(); // 关闭加载对话框
TopSlideNotification.show(
Get.context!,
text: "设备连接失败".tr,
textColor: themeController.currentColor.sc9,
);
}
}
});
// 等待扫描完成
await Future.delayed(Duration(seconds: 20));
} catch (e) {
timeoutTimer?.cancel();
Navigator.of(Get.context!).pop(); // 关闭加载对话框
TopSlideNotification.show(
Get.context!,
text: "扫描过程中发生错误".tr,
textColor: themeController.currentColor.sc9,
);
} finally {
timeoutTimer?.cancel();
await FlutterBluePlus.stopScan();
}
}
}