AIR版的功能添加
This commit is contained in:
@@ -52,6 +52,14 @@ class _BleDeviceDetailPageState extends State<BleDeviceDetailPage> {
|
||||
super.initState();
|
||||
_initConnectionListener();
|
||||
_syncInitialState();
|
||||
|
||||
// 🔥 页面加载完成后自动连接设备(如果尚未连接且未在连接中)
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (!mounted) return;
|
||||
if (!_isConnected && !_isConnecting) {
|
||||
_connect();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// 监听 BleManager 的全局连接状态流
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
||||
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
||||
import '../../../../../core/di/injection.dart';
|
||||
import '../../../../../core/app/app_user_cubit.dart';
|
||||
@@ -10,6 +11,7 @@ import '../../../../../components/tcp_status_indicator.dart';
|
||||
import '../../../../../components/device_status_modal.dart';
|
||||
import '../../../../v2/site/presentation/cubit/site_cubit.dart';
|
||||
import '../../../site/presentation/widgets/site_selector_widget.dart';
|
||||
import '../../../../main_container/domain/tab_config.dart';
|
||||
import '../bloc/device_status_bloc.dart' as DeviceListBloc;
|
||||
import '../bloc/device_status_event.dart' as DeviceListEvent;
|
||||
import '../bloc/device_status_state.dart' as DeviceListState;
|
||||
@@ -27,6 +29,7 @@ import '../widgets/bluetooth_scan_modal.dart';
|
||||
import '../../domain/entities/drone_station_entity.dart'; // 🔥 添加 DroneStationEntity 导入
|
||||
import 'robot_list_page.dart';
|
||||
import 'robot_control_page.dart';
|
||||
import 'ble_device_detail_page.dart';
|
||||
import 'drone_station_detail_page.dart';
|
||||
import '../../../../../core/network/mqtt/data/datasources/drone_osd_datasource.dart';
|
||||
import 'qr_scanner_page.dart';
|
||||
@@ -74,6 +77,23 @@ class _DeviceStatusViewState extends State<DeviceStatusView> {
|
||||
void initState() {
|
||||
super.initState();
|
||||
_currentSiteId = sl<SiteCubit>().state.selectedSite?.id;
|
||||
// personal 角色隐藏了场站选择器(不再由 SiteSelectorWidget 触发 loadSites),
|
||||
// 这里主动加载场站并默认选中,保证设备数据能正常展示:
|
||||
// 1) loadSites 内部有幂等守卫,会自动选中(恢复上次或首个)场站;
|
||||
// 2) 兜底:若加载后仍未选中(如 loadSites 因已加载而提前返回、
|
||||
// 或历史选中项被清除),且存在场站,则默认选中第一个;
|
||||
// 3) 选中后经 _siteSub 监听触发设备列表刷新。
|
||||
if (_isPersonalRole) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||||
final siteCubit = sl<SiteCubit>();
|
||||
await siteCubit.loadSites();
|
||||
if (!mounted) return;
|
||||
final siteState = siteCubit.state;
|
||||
if (siteState.selectedSite == null && siteState.sites.isNotEmpty) {
|
||||
siteCubit.selectSite(siteState.sites.first);
|
||||
}
|
||||
});
|
||||
}
|
||||
_siteSub = sl<SiteCubit>().stream.listen((siteState) {
|
||||
if (!mounted) return;
|
||||
final newSiteId = siteState.selectedSite?.id;
|
||||
@@ -85,6 +105,10 @@ class _DeviceStatusViewState extends State<DeviceStatusView> {
|
||||
});
|
||||
}
|
||||
|
||||
/// 当前登录用户是否为个人角色(roleKey == 'personal')
|
||||
bool get _isPersonalRole =>
|
||||
sl<AppUserCubit>().state.user?.roleKey == TabConfig.personalRoleKey;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_siteSub?.cancel();
|
||||
@@ -174,6 +198,7 @@ class _DeviceStatusViewState extends State<DeviceStatusView> {
|
||||
}
|
||||
|
||||
Widget _buildAppBar(BuildContext context) {
|
||||
final isPersonal = _isPersonalRole;
|
||||
return Container(
|
||||
height: 44.0,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
||||
@@ -182,10 +207,12 @@ class _DeviceStatusViewState extends State<DeviceStatusView> {
|
||||
Expanded(
|
||||
child: Row(
|
||||
children: [
|
||||
const Flexible(child: SiteSelectorWidget(compact: true)),
|
||||
const SizedBox(width: 8),
|
||||
Container(width: 1, height: 20, color: const Color(0xFFE5E6EB)),
|
||||
const SizedBox(width: 8),
|
||||
if (!isPersonal) ...[
|
||||
const Flexible(child: SiteSelectorWidget(compact: true)),
|
||||
const SizedBox(width: 8),
|
||||
Container(width: 1, height: 20, color: const Color(0xFFE5E6EB)),
|
||||
const SizedBox(width: 8),
|
||||
],
|
||||
Text(
|
||||
AppLocalizations.of(
|
||||
context,
|
||||
@@ -354,24 +381,22 @@ class _DeviceStatusViewState extends State<DeviceStatusView> {
|
||||
}
|
||||
|
||||
Widget _buildTypeFilterBar(BuildContext context) {
|
||||
final typeCodes = [
|
||||
'all',
|
||||
'robot',
|
||||
'drone_station',
|
||||
'inverter',
|
||||
'combiner_box',
|
||||
'module',
|
||||
'monitor',
|
||||
];
|
||||
final typeLabels = [
|
||||
AppLocalizations.of(context).translate('device_list_v2.all'),
|
||||
AppLocalizations.of(context).translate('device_list_v2.robot'),
|
||||
AppLocalizations.of(context).translate('device_list_v2.drone_station'),
|
||||
AppLocalizations.of(context).translate('device_list_v2.inverter'),
|
||||
AppLocalizations.of(context).translate('device_list_v2.combiner_box'),
|
||||
AppLocalizations.of(context).translate('device_list_v2.module'),
|
||||
AppLocalizations.of(context).translate('device_list_v2.monitor'),
|
||||
];
|
||||
final isPersonal = _isPersonalRole;
|
||||
final typeCodes = isPersonal
|
||||
? ['all', 'robot', 'drone_station']
|
||||
: [
|
||||
'all',
|
||||
'robot',
|
||||
'drone_station',
|
||||
'inverter',
|
||||
'combiner_box',
|
||||
'module',
|
||||
'monitor',
|
||||
];
|
||||
final typeLabels = typeCodes
|
||||
.map((code) =>
|
||||
AppLocalizations.of(context).translate('device_list_v2.$code'))
|
||||
.toList();
|
||||
|
||||
return BlocBuilder<
|
||||
DeviceListBloc.DeviceStatusBloc,
|
||||
@@ -480,6 +505,7 @@ class _DeviceStatusViewState extends State<DeviceStatusView> {
|
||||
}
|
||||
|
||||
if (state is DeviceListState.DeviceStatusLoaded) {
|
||||
final isPersonal = _isPersonalRole;
|
||||
List filteredByType = state.devices;
|
||||
if (state.selectedType != 'all') {
|
||||
filteredByType = state.devices.where((device) {
|
||||
@@ -496,7 +522,11 @@ class _DeviceStatusViewState extends State<DeviceStatusView> {
|
||||
device.name.startsWith(prefix) ||
|
||||
device.deviceId.startsWith(prefix),
|
||||
);
|
||||
return !isRobot;
|
||||
if (isRobot) return false;
|
||||
// personal 角色只允许 全部/机器人/无人机;
|
||||
// 机器人与无人机已由专用组件渲染,这里排除其余所有普通设备(逆变器/汇流箱/组件/监控)
|
||||
if (isPersonal) return false;
|
||||
return true;
|
||||
}).toList();
|
||||
}
|
||||
|
||||
@@ -611,7 +641,34 @@ class _DeviceStatusViewState extends State<DeviceStatusView> {
|
||||
debugPrint(
|
||||
'🔴🔴🔴 [全部-选中机器人] name: ${robot.name}, id: ${robot.id}, type: ${robot.type}',
|
||||
);
|
||||
debugPrint(
|
||||
'🔴🔴🔴 [全部-选中机器人] hasHost: ${robot.hasHost}',
|
||||
);
|
||||
|
||||
// 🔥 hasHost 字段判定:false → 蓝牙连接流程
|
||||
if (!robot.hasHost) {
|
||||
debugPrint(
|
||||
'⚠️ [全部-EmbeddedRobotList] hasHost=false,进入蓝牙连接流程',
|
||||
);
|
||||
final timestamp = _extractTimestamp(robot.name);
|
||||
if (timestamp == null || timestamp.isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('设备名称格式错误,无法提取时间戳'),
|
||||
duration: Duration(seconds: 2),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
_connectToDeviceViaBluetooth(
|
||||
context,
|
||||
robot.name,
|
||||
timestamp,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// hasHost == true → 走原来的逻辑
|
||||
// 1. 将当前机器人设置为全局待控制设备
|
||||
final device = DeviceEntity(
|
||||
deviceName: robot.name,
|
||||
@@ -792,7 +849,18 @@ class _DeviceStatusViewState extends State<DeviceStatusView> {
|
||||
DeviceListState.DeviceStatusLoaded state,
|
||||
) {
|
||||
// 从实际设备列表计算统计数据(不再使用硬编码的假数据)
|
||||
final devices = state.devices;
|
||||
// personal 角色列表只展示 机器人 + 无人机机场,统计口径需与列表保持一致,
|
||||
// 否则会出现「共 N 台」与下方可见设备数量对不上的问题。
|
||||
final devices = _isPersonalRole
|
||||
? state.devices.where((device) {
|
||||
if (device.type == 'drone_station') return true;
|
||||
return _robotPrefixes.any(
|
||||
(prefix) =>
|
||||
device.name.startsWith(prefix) ||
|
||||
device.deviceId.startsWith(prefix),
|
||||
);
|
||||
}).toList()
|
||||
: state.devices;
|
||||
final totalCount = devices.length;
|
||||
final onlineCount = devices.where((d) => d.status == '在线').length;
|
||||
final exceptionCount = devices.where((d) => d.status == '异常').length;
|
||||
@@ -1166,4 +1234,178 @@ class _DeviceStatusViewState extends State<DeviceStatusView> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 🔥 从设备名中提取13位时间戳
|
||||
String? _extractTimestamp(String deviceName) {
|
||||
final regex = RegExp(r'(\d{13})');
|
||||
final match = regex.firstMatch(deviceName);
|
||||
return match?.group(1);
|
||||
}
|
||||
|
||||
/// 🔥 通过蓝牙连接设备
|
||||
Future<void> _connectToDeviceViaBluetooth(
|
||||
BuildContext context,
|
||||
String deviceName,
|
||||
String timestamp,
|
||||
) async {
|
||||
final bleManager = BleManager.instance;
|
||||
|
||||
// 0. 优先检查:如果该设备已经连接,直接跳转详情页,不再扫描
|
||||
final connectedDevice = bleManager.connectedDevice;
|
||||
if (connectedDevice != null) {
|
||||
final connectedName = bleManager.connectedDeviceName ?? '';
|
||||
debugPrint('🔍 [蓝牙连接] 已连接设备名: $connectedName,匹配时间戳: $timestamp');
|
||||
if (connectedName.contains(timestamp)) {
|
||||
debugPrint('✅ [蓝牙连接] 设备已连接,直接跳转详情页: $connectedName');
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => BleDeviceDetailPage(device: connectedDevice),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 1. 检查蓝牙是否打开
|
||||
final isBluetoothOn = await bleManager.checkBluetooth();
|
||||
if (!isBluetoothOn) {
|
||||
if (!context.mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('请先打开手机蓝牙'),
|
||||
backgroundColor: Colors.orange,
|
||||
duration: Duration(seconds: 2),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 显示加载圈
|
||||
showDialog(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (_) => const Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
CircularProgressIndicator(),
|
||||
SizedBox(height: 16),
|
||||
Text(
|
||||
'正在搜索蓝牙设备...',
|
||||
style: TextStyle(color: Colors.white),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// 3. 启动蓝牙扫描
|
||||
try {
|
||||
await bleManager.startScan(continuous: false);
|
||||
} catch (e) {
|
||||
debugPrint('❌ [蓝牙连接] 启动扫描失败: $e');
|
||||
}
|
||||
|
||||
// 4. 等待扫描结果
|
||||
await Future.delayed(const Duration(milliseconds: 1500));
|
||||
|
||||
// 5. 关闭加载圈
|
||||
if (!context.mounted) return;
|
||||
Navigator.of(context).pop();
|
||||
|
||||
// 6. 获取扫描结果查找匹配设备
|
||||
final scanResults = bleManager.getScanResults();
|
||||
BluetoothDevice? matchedDevice;
|
||||
for (final result in scanResults) {
|
||||
final dev = result.device;
|
||||
final devName = dev.platformName.isEmpty
|
||||
? (dev.advName.isEmpty ? dev.remoteId.toString() : dev.advName)
|
||||
: dev.platformName;
|
||||
if (devName.contains(timestamp)) {
|
||||
matchedDevice = dev;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (matchedDevice != null) {
|
||||
final device = matchedDevice;
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => BleDeviceDetailPage(device: device),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
_showNotFoundDialogAndMonitor(context, deviceName, timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
/// 🔥 未找到设备弹窗 + 后台持续监测(只监测最新点击的设备)
|
||||
void _showNotFoundDialogAndMonitor(
|
||||
BuildContext context,
|
||||
String deviceName,
|
||||
String timestamp,
|
||||
) {
|
||||
// 检查是否已有弹窗存在,如果有则先关闭(防止弹窗叠加)
|
||||
if (Navigator.of(context).canPop()) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
bool dialogShown = true;
|
||||
StreamSubscription? subscription;
|
||||
showDialog(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (dialogContext) {
|
||||
dialogShown = true;
|
||||
return AlertDialog(
|
||||
title: const Text('未找到设备'),
|
||||
content: const Text('请确认机器已上线并打开蓝牙了'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
dialogShown = false;
|
||||
Navigator.pop(dialogContext);
|
||||
},
|
||||
child: const Text('取消'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
dialogShown = false;
|
||||
Navigator.pop(dialogContext);
|
||||
},
|
||||
child: const Text('确认'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
).then((_) {});
|
||||
|
||||
final bleManager = BleManager.instance;
|
||||
subscription = bleManager.scanResults.listen((results) {
|
||||
if (!dialogShown) {
|
||||
subscription?.cancel();
|
||||
return;
|
||||
}
|
||||
for (final result in results) {
|
||||
final dev = result.device;
|
||||
final devName = dev.platformName.isEmpty
|
||||
? (dev.advName.isEmpty ? dev.remoteId.toString() : dev.advName)
|
||||
: dev.platformName;
|
||||
if (devName.contains(timestamp)) {
|
||||
subscription?.cancel();
|
||||
if (Navigator.of(context).canPop()) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => BleDeviceDetailPage(device: dev),
|
||||
),
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
||||
import '../../../../../core/di/injection.dart';
|
||||
import '../../../../../core/logging/i_logger_service.dart';
|
||||
import '../../../../../core/bluetooth/ble_manager.dart';
|
||||
import '../../../../devices/presentation/bloc/devices_cubit.dart';
|
||||
import '../../../../devices/domain/entities/device_entity.dart';
|
||||
import '../../../../remote_control/presentation/bloc/remote_control_cubit.dart';
|
||||
@@ -13,8 +17,11 @@ import '../bloc/robot_list_bloc.dart';
|
||||
import '../bloc/robot_list_event.dart';
|
||||
import '../bloc/robot_list_state.dart';
|
||||
import '../widgets/robot_item_card.dart';
|
||||
import '../../../../../core/bluetooth/ble_manager.dart';
|
||||
import 'robot_control_page.dart';
|
||||
import 'cleaning_weeding_robot_task_page.dart';
|
||||
import 'ble_device_detail_page.dart';
|
||||
import 'ble_device_detail_page.dart';
|
||||
|
||||
/// 机器人列表页面
|
||||
class RobotListPage extends StatelessWidget {
|
||||
@@ -86,7 +93,8 @@ class _RobotListViewState extends State<RobotListView> {
|
||||
children: [
|
||||
_buildStatsCard(state),
|
||||
_buildQuickActions(state),
|
||||
_buildCurrentTask(state),
|
||||
// 🔥 暂时注释掉「当前任务」信息栏(展示的是硬编码假数据)
|
||||
// _buildCurrentTask(state),
|
||||
..._buildRobotList(state),
|
||||
],
|
||||
),
|
||||
@@ -631,9 +639,34 @@ class _RobotListViewState extends State<RobotListView> {
|
||||
'🔴🔴🔴 [选中机器人] name: ${robot.name}, id: ${robot.id}, type: ${robot.type}',
|
||||
);
|
||||
debugPrint(
|
||||
'🔴🔴🔴 [选中机器人] status: ${robot.status}, battery: ${robot.battery}, task: ${robot.task}',
|
||||
'🔴🔴🔴 [选中机器人] hasHost: ${robot.hasHost}',
|
||||
);
|
||||
|
||||
// 🔥 hasHost 字段判定:false → 蓝牙连接流程
|
||||
if (!robot.hasHost) {
|
||||
debugPrint('⚠️ [RobotListPage] hasHost=false,进入蓝牙连接流程');
|
||||
|
||||
// 从设备名中提取时间戳
|
||||
final timestamp = _extractTimestamp(robot.name);
|
||||
if (timestamp == null || timestamp.isEmpty) {
|
||||
debugPrint('❌ [RobotListPage] 未提取到时间戳');
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('设备名称格式错误,无法提取时间戳'),
|
||||
duration: Duration(seconds: 2),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
debugPrint('✅ [RobotListPage] 提取到时间戳: $timestamp');
|
||||
_connectToDeviceViaBluetooth(context, robot.name, timestamp);
|
||||
return;
|
||||
}
|
||||
|
||||
// hasHost == true → 走原来的逻辑
|
||||
debugPrint('✅ [RobotListPage] hasHost=true,走正常控制流程');
|
||||
|
||||
// 1. 将当前机器人设置为全局待控制设备(用 robot.name 作为 deviceName)
|
||||
final device = DeviceEntity(
|
||||
deviceName: robot.name, // ✅ 修正: 用 name 而不是 id
|
||||
@@ -674,4 +707,178 @@ class _RobotListViewState extends State<RobotListView> {
|
||||
)
|
||||
.toList();
|
||||
}
|
||||
|
||||
/// 🔥 从设备名中提取13位时间戳
|
||||
String? _extractTimestamp(String deviceName) {
|
||||
final regex = RegExp(r'(\d{13})');
|
||||
final match = regex.firstMatch(deviceName);
|
||||
return match?.group(1);
|
||||
}
|
||||
|
||||
/// 🔥 通过蓝牙连接设备
|
||||
Future<void> _connectToDeviceViaBluetooth(
|
||||
BuildContext context,
|
||||
String deviceName,
|
||||
String timestamp,
|
||||
) async {
|
||||
final bleManager = BleManager.instance;
|
||||
|
||||
// 0. 优先检查:如果该设备已经连接,直接跳转详情页,不再扫描
|
||||
final connectedDevice = bleManager.connectedDevice;
|
||||
if (connectedDevice != null) {
|
||||
final connectedName = bleManager.connectedDeviceName ?? '';
|
||||
debugPrint('🔍 [蓝牙连接] 已连接设备名: $connectedName,匹配时间戳: $timestamp');
|
||||
if (connectedName.contains(timestamp)) {
|
||||
debugPrint('✅ [蓝牙连接] 设备已连接,直接跳转详情页: $connectedName');
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => BleDeviceDetailPage(device: connectedDevice),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 1. 检查蓝牙是否打开
|
||||
final isBluetoothOn = await bleManager.checkBluetooth();
|
||||
if (!isBluetoothOn) {
|
||||
if (!context.mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('请先打开蓝牙'),
|
||||
backgroundColor: Colors.orange,
|
||||
duration: Duration(seconds: 2),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 显示加载圈
|
||||
showDialog(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (_) => const Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
CircularProgressIndicator(),
|
||||
SizedBox(height: 16),
|
||||
Text(
|
||||
'正在搜索蓝牙设备...',
|
||||
style: TextStyle(color: Colors.white),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// 3. 启动蓝牙扫描
|
||||
try {
|
||||
await bleManager.startScan(continuous: false);
|
||||
} catch (e) {
|
||||
debugPrint('❌ [蓝牙连接] 启动扫描失败: $e');
|
||||
}
|
||||
|
||||
// 4. 等待扫描结果
|
||||
await Future.delayed(const Duration(milliseconds: 1500));
|
||||
|
||||
// 5. 关闭加载圈
|
||||
if (!context.mounted) return;
|
||||
Navigator.of(context).pop();
|
||||
|
||||
// 6. 获取扫描结果查找匹配设备
|
||||
final scanResults = bleManager.getScanResults();
|
||||
BluetoothDevice? matchedDevice;
|
||||
for (final result in scanResults) {
|
||||
final dev = result.device;
|
||||
final devName = dev.platformName.isEmpty
|
||||
? (dev.advName.isEmpty ? dev.remoteId.toString() : dev.advName)
|
||||
: dev.platformName;
|
||||
if (devName.contains(timestamp)) {
|
||||
matchedDevice = dev;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (matchedDevice != null) {
|
||||
final device = matchedDevice;
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => BleDeviceDetailPage(device: device),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
_showNotFoundDialogAndMonitor(context, deviceName, timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
/// 🔥 未找到设备弹窗 + 后台持续监测(只监测最新点击的设备)
|
||||
void _showNotFoundDialogAndMonitor(
|
||||
BuildContext context,
|
||||
String deviceName,
|
||||
String timestamp,
|
||||
) {
|
||||
// 检查是否已有弹窗存在,如果有则先关闭(防止弹窗叠加)
|
||||
if (Navigator.of(context).canPop()) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
bool dialogShown = true;
|
||||
StreamSubscription? subscription;
|
||||
showDialog(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (dialogContext) {
|
||||
dialogShown = true;
|
||||
return AlertDialog(
|
||||
title: const Text('未找到设备'),
|
||||
content: const Text('请确认机器已上线并打开蓝牙了'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
dialogShown = false;
|
||||
Navigator.pop(dialogContext);
|
||||
},
|
||||
child: const Text('取消'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
dialogShown = false;
|
||||
Navigator.pop(dialogContext);
|
||||
},
|
||||
child: const Text('确认'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
).then((_) {});
|
||||
|
||||
final bleManager = BleManager.instance;
|
||||
subscription = bleManager.scanResults.listen((results) {
|
||||
if (!dialogShown) {
|
||||
subscription?.cancel();
|
||||
return;
|
||||
}
|
||||
for (final result in results) {
|
||||
final dev = result.device;
|
||||
final devName = dev.platformName.isEmpty
|
||||
? (dev.advName.isEmpty ? dev.remoteId.toString() : dev.advName)
|
||||
: dev.platformName;
|
||||
if (devName.contains(timestamp)) {
|
||||
subscription?.cancel();
|
||||
if (Navigator.of(context).canPop()) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => BleDeviceDetailPage(device: dev),
|
||||
),
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user