diff --git a/lib/data/services/ble_manager.dart b/lib/data/services/ble_manager.dart index 1bd4b11c5..a855ec38d 100644 --- a/lib/data/services/ble_manager.dart +++ b/lib/data/services/ble_manager.dart @@ -113,7 +113,7 @@ class BleManager extends GetxService { // 经典蓝牙实例 // 当前连接的设备信息 - String? _currentDeviceAddress; + String? currentDeviceAddress; String? _currentDeviceName; // BLE连接状态 @@ -513,7 +513,7 @@ class BleManager extends GetxService { (firmwareVersion != null || batteryLevel != null)) { Logger.info('lxm--设备信息不存在,创建新的设备信息对象'); // 使用保存的设备信息创建新的DeviceInfo对象 - String deviceAddress = _currentDeviceAddress ?? ""; + String deviceAddress = currentDeviceAddress ?? ""; String deviceName = _currentDeviceName ?? ""; Logger.info('lxm--创建新的设备信息对象: address=$deviceAddress, name=$deviceName'); @@ -561,7 +561,7 @@ class BleManager extends GetxService { void clearDeviceInfo() { Logger.info('清除设备信息'); deviceInfo.value = null; - _currentDeviceAddress = null; + currentDeviceAddress = null; _currentDeviceName = null; } @@ -573,7 +573,7 @@ class BleManager extends GetxService { // 确保设备信息对象存在 if (deviceInfo.value == null && _isConnected.value) { // 创建初始设备信息对象 - String deviceAddress = _currentDeviceAddress ?? ""; + String deviceAddress = currentDeviceAddress ?? ""; String deviceName = _currentDeviceName ?? ""; Logger.info('初始化设备信息对象: address=$deviceAddress, name=$deviceName'); @@ -1036,12 +1036,12 @@ class BleManager extends GetxService { try { // 保存设备地址信息 if (macAddress != null) { - _currentDeviceAddress = macAddress; + currentDeviceAddress = macAddress; // 从蓝牙管理器获取设备名称 final bluetoothManager = Get.find(); _currentDeviceName = bluetoothManager.currentDevice?.name ?? ""; Logger.info( - '保存设备信息: address=$_currentDeviceAddress, name=$_currentDeviceName'); + '保存设备信息: address=$currentDeviceAddress, name=$_currentDeviceName'); } return await _bleService.registerBackgroundScan(macAddress: macAddress); diff --git a/lib/modules/devices/controllers/devices_controller.dart b/lib/modules/devices/controllers/devices_controller.dart index 65d6b24b1..ad2a74a73 100644 --- a/lib/modules/devices/controllers/devices_controller.dart +++ b/lib/modules/devices/controllers/devices_controller.dart @@ -24,29 +24,36 @@ class DevicesController extends GetxController { @override void onInit() { super.onInit(); + print("初始化设备管理1"); _bleManager = Get.find(); - + print("初始化设备管理2"); // 在初始化时加载设备数据 _loadDevices(); } // 加载设备数据 void _loadDevices() async { - devices.clear(); + Logger.info("加载设备"); final response = await Api.getdevices(); if (response != null) { - var devices = (response['devices'] as List) - .map((e) => UserDevice.fromJson(e as Map)) - .toList(); - User.instance.devices = devices; + if (response['devices'] != null) { + var devices = (response['devices'] as List) + .map((e) => UserDevice.fromJson(e as Map)) + .toList(); + User.instance.devices = devices; + } else { + User.instance.devices = []; + } + Logger.info("加载设备 ${User.instance.devices.length}"); } - + devices.clear(); for (var device in User.instance.devices) { if (!devices.containsKey(device.devicetype)) { devices[device.devicetype] = []; } devices[device.devicetype]!.add(device); } + devices.refresh(); } @override @@ -59,12 +66,22 @@ class DevicesController extends GetxController { final response = await Api.unbinddevice({ "id": device.id, }); + Logger.info("解绑设备 $response"); if (response != null) { // 响应式更新 - devices[device.devicetype]?.remove(device); - devices.refresh(); // 触发UI更新 - - if (device.devicemac == _bleManager.deviceInfo.value?.address) { + User.instance.devices.remove(device); + devices.clear(); + for (var device in User.instance.devices) { + if (!devices.containsKey(device.devicetype)) { + devices[device.devicetype] = []; + } + devices[device.devicetype]!.add(device); + } + devices.refresh(); + Logger.info( + "解绑设备 ${device.devicemac} 当前使用设备:${_bleManager.currentDeviceAddress}"); + if (device.devicemac == _bleManager.currentDeviceAddress) { + Logger.info("解绑设备 $device 重新匹配!"); User.instance.removeUserDevice(device.id); _bleManager.clearPairedDevices(); _bleManager.disconnect(); diff --git a/lib/modules/devices/views/devices_view.dart b/lib/modules/devices/views/devices_view.dart index f7ffc67f4..846004a4e 100644 --- a/lib/modules/devices/views/devices_view.dart +++ b/lib/modules/devices/views/devices_view.dart @@ -118,57 +118,51 @@ class DevicesView extends GetView { } Widget _buildDevicesList(bool isDarkMode) { - final devices = User.instance.devices; + return Obx(() { + // 使用控制器的响应式数据,这会触发控制器的实例化 + final groupedDevices = controller.devices; - if (devices.isEmpty) { - return _buildEmptyState(isDarkMode); - } - - // 按设备类型分组 - final Map> groupedDevices = {}; - for (var device in devices) { - final deviceType = DeviceType.fromValue(device.devicetype); - if (!groupedDevices.containsKey(deviceType)) { - groupedDevices[deviceType] = []; + if (groupedDevices.isEmpty) { + return _buildEmptyState(isDarkMode); } - groupedDevices[deviceType]!.add(device); - } - return ListView( - padding: EdgeInsets.all(20.w), - children: [ - // 遍历每个设备类型 - ...groupedDevices.entries.map((entry) { - final deviceType = entry.key; - final deviceList = entry.value; + return ListView( + padding: EdgeInsets.all(20.w), + children: [ + // 遍历每个设备类型 + ...groupedDevices.entries.map((entry) { + final deviceTypeValue = entry.key; + final deviceList = entry.value; + final deviceType = DeviceType.fromValue(deviceTypeValue); - return Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - // 设备类型标题 - Padding( - padding: EdgeInsets.only(bottom: 12.h), - child: Text( - deviceType.displayName, - style: TextStyle( - fontSize: 16.sp, - fontWeight: FontWeight.w600, - color: isDarkMode ? Colors.white : Colors.black87, + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + // 设备类型标题 + Padding( + padding: EdgeInsets.only(bottom: 12.h), + child: Text( + deviceType.displayName, + style: TextStyle( + fontSize: 16.sp, + fontWeight: FontWeight.w600, + color: isDarkMode ? Colors.white : Colors.black87, + ), ), ), - ), - // 该类型下的设备列表 - ...deviceList.map((device) => _buildDeviceCard( - device: device, - deviceType: deviceType, - isDarkMode: isDarkMode, - )), - SizedBox(height: 20.h), - ], - ); - }).toList(), - ], - ); + // 该类型下的设备列表 + ...deviceList.map((device) => _buildDeviceCard( + device: device, + deviceType: deviceType, + isDarkMode: isDarkMode, + )), + SizedBox(height: 20.h), + ], + ); + }).toList(), + ], + ); + }); } Widget _buildEmptyState(bool isDarkMode) {