|
|
|
@ -22,6 +22,10 @@ class HomeController extends GetxController { |
|
|
|
// 连接状态相关 |
|
|
|
final _isFullyConnected = false.obs; |
|
|
|
|
|
|
|
// 缓存最后连接的设备信息,用于断开后短暂显示 |
|
|
|
String? _lastConnectedDeviceName; |
|
|
|
String? _lastConnectedDeviceAddress; |
|
|
|
|
|
|
|
// Getter方法 |
|
|
|
bool get isFullyConnected => _isFullyConnected.value; |
|
|
|
RxBool get isFullyConnectedRx => _isFullyConnected; // 响应式getter供UI使用 |
|
|
|
@ -82,10 +86,8 @@ class HomeController extends GetxController { |
|
|
|
// 设置监听器 |
|
|
|
_setupStateListeners(); |
|
|
|
|
|
|
|
// 延迟检查初始连接状态,确保监听器已经设置 |
|
|
|
Future.delayed(const Duration(milliseconds: 100), () { |
|
|
|
_checkConnectionStatus(); |
|
|
|
}); |
|
|
|
// 立即检查初始连接状态 |
|
|
|
_checkConnectionStatus(); |
|
|
|
} |
|
|
|
|
|
|
|
/// 设置状态监听器 |
|
|
|
@ -102,29 +104,26 @@ class HomeController extends GetxController { |
|
|
|
// 经典蓝牙连接监听 |
|
|
|
_classicBluetoothWorker = ever(_bluetoothManager.currentDeviceRx, (BluetoothDevice? device) { |
|
|
|
Logger.d(_tag, '经典蓝牙: ${device != null ? "已连接" : "已断开"}'); |
|
|
|
// 保存设备信息用于断开后显示 |
|
|
|
if (device != null) { |
|
|
|
_lastConnectedDeviceName = device.name; |
|
|
|
_lastConnectedDeviceAddress = device.address; |
|
|
|
} |
|
|
|
// 立即检查状态,不使用延迟 |
|
|
|
_checkConnectionStatus(); |
|
|
|
// 额外的刷新确保UI更新 |
|
|
|
Future.delayed(const Duration(milliseconds: 50), () { |
|
|
|
_isFullyConnected.refresh(); |
|
|
|
update(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
// BLE连接监听 |
|
|
|
_bleConnectionWorker = ever(_bleManager.connectionStateRx, (int state) { |
|
|
|
Logger.d(_tag, 'BLE状态变化: $state (${state == 2 ? "已连接" : "未连接"})'); |
|
|
|
// 立即检查状态,不使用延迟 |
|
|
|
_checkConnectionStatus(); |
|
|
|
// 额外的刷新确保UI更新 |
|
|
|
Future.delayed(const Duration(milliseconds: 50), () { |
|
|
|
_isFullyConnected.refresh(); |
|
|
|
update(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
/// 刷新设备连接状态 |
|
|
|
Future<void> refreshDeviceConnection() async { |
|
|
|
_checkConnectionStatus(); |
|
|
|
await _checkConnectionStatus(); |
|
|
|
} |
|
|
|
|
|
|
|
// 导航和功能方法 |
|
|
|
@ -147,47 +146,53 @@ class HomeController extends GetxController { |
|
|
|
// UI辅助方法 |
|
|
|
|
|
|
|
/// 检查连接状态 |
|
|
|
void _checkConnectionStatus() { |
|
|
|
// 直接检查,不使用延迟 |
|
|
|
bool hasClassicBluetooth = currentDevice != null; |
|
|
|
bool hasBleConnection = _bleManager.connectionState == 2; // 直接检查状态值,2 = connected |
|
|
|
|
|
|
|
Logger.d(_tag, '连接状态检查: 经典蓝牙=$hasClassicBluetooth, BLE=$hasBleConnection (状态值=${_bleManager.connectionState})'); |
|
|
|
|
|
|
|
bool wasConnected = _isFullyConnected.value; |
|
|
|
bool isNowConnected = hasClassicBluetooth && hasBleConnection; |
|
|
|
|
|
|
|
// 只有状态真正改变时才更新 |
|
|
|
if (_isFullyConnected.value != isNowConnected) { |
|
|
|
// 先更新状态 |
|
|
|
_isFullyConnected.value = isNowConnected; |
|
|
|
Logger.d(_tag, '连接状态变更: ${wasConnected ? "已连接" : "未连接"} -> ${isNowConnected ? "已连接" : "未连接"}'); |
|
|
|
Future<void> _checkConnectionStatus() async { |
|
|
|
try { |
|
|
|
// 主动从原生层获取最新的BLE状态 |
|
|
|
final currentBleState = await _bleManager.getConnectionState(); |
|
|
|
|
|
|
|
bool hasClassicBluetooth = currentDevice != null; |
|
|
|
bool hasBleConnection = currentBleState == 2; // 使用实时查询的状态 |
|
|
|
|
|
|
|
// 强制触发响应式更新 |
|
|
|
_isFullyConnected.refresh(); |
|
|
|
Logger.d(_tag, '连接状态检查: 经典蓝牙=$hasClassicBluetooth, BLE=$hasBleConnection (实时状态值=$currentBleState)'); |
|
|
|
|
|
|
|
// 如果从未连接变为连接,主动获取耳机信息 |
|
|
|
if (!wasConnected && isNowConnected) { |
|
|
|
Logger.d(_tag, '设备连接成功,获取耳机信息'); |
|
|
|
_bleManager.fetchDeviceInfo(); |
|
|
|
bool wasConnected = _isFullyConnected.value; |
|
|
|
bool isNowConnected = hasClassicBluetooth && hasBleConnection; |
|
|
|
|
|
|
|
// 状态一致性检查:如果声称已连接但没有设备信息,强制断开 |
|
|
|
if (isNowConnected && currentDevice == null) { |
|
|
|
Logger.w(_tag, '状态不一致:已连接但无设备信息,强制更新为未连接'); |
|
|
|
isNowConnected = false; |
|
|
|
} |
|
|
|
|
|
|
|
// 延迟一帧后再次刷新,确保UI更新 |
|
|
|
Future.delayed(Duration.zero, () { |
|
|
|
update(); |
|
|
|
// 再次强制刷新响应式变量 |
|
|
|
// 只有状态真正改变时才更新 |
|
|
|
if (_isFullyConnected.value != isNowConnected) { |
|
|
|
// 立即同步更新状态 |
|
|
|
_isFullyConnected.value = isNowConnected; |
|
|
|
Logger.d(_tag, '连接状态变更: ${wasConnected ? "已连接" : "未连接"} -> ${isNowConnected ? "已连接" : "未连接"}'); |
|
|
|
|
|
|
|
// 立即触发UI更新,不使用延迟 |
|
|
|
_isFullyConnected.refresh(); |
|
|
|
}); |
|
|
|
|
|
|
|
// 延迟稍长时间再刷新一次,处理某些特殊情况 |
|
|
|
Future.delayed(const Duration(milliseconds: 100), () { |
|
|
|
update(); |
|
|
|
|
|
|
|
// 如果从未连接变为连接,主动获取耳机信息 |
|
|
|
if (!wasConnected && isNowConnected) { |
|
|
|
Logger.d(_tag, '设备连接成功,获取耳机信息'); |
|
|
|
await _bleManager.fetchDeviceInfo(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 确保UI刷新 |
|
|
|
update(); |
|
|
|
} catch (e) { |
|
|
|
Logger.e(_tag, '检查连接状态失败: $e'); |
|
|
|
// 出错时设置为未连接 |
|
|
|
if (_isFullyConnected.value) { |
|
|
|
_isFullyConnected.value = false; |
|
|
|
_isFullyConnected.refresh(); |
|
|
|
}); |
|
|
|
update(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 总是调用update确保UI刷新 |
|
|
|
update(); |
|
|
|
} |
|
|
|
|
|
|
|
/// 获取连接状态对应的颜色 |
|
|
|
@ -209,7 +214,29 @@ class HomeController extends GetxController { |
|
|
|
|
|
|
|
/// 获取设备名称 |
|
|
|
String getDeviceName() { |
|
|
|
return currentDevice?.name ?? 'unknownEquipment'.tr; |
|
|
|
// 状态一致性检查 |
|
|
|
if (_isFullyConnected.value && currentDevice == null) { |
|
|
|
Logger.w(_tag, '状态不一致:显示已连接但无设备信息'); |
|
|
|
// 立即重新检查状态 |
|
|
|
_checkConnectionStatus(); |
|
|
|
} |
|
|
|
|
|
|
|
// 优先使用当前设备名称 |
|
|
|
if (currentDevice?.name != null && currentDevice!.name!.isNotEmpty) { |
|
|
|
return currentDevice!.name!; |
|
|
|
} |
|
|
|
|
|
|
|
// 如果当前没有设备但有BLE设备信息,使用BLE的设备名 |
|
|
|
if (deviceInfo.value?.name != null && deviceInfo.value!.name.isNotEmpty) { |
|
|
|
return deviceInfo.value!.name; |
|
|
|
} |
|
|
|
|
|
|
|
// 如果刚断开连接,使用缓存的设备名称(提供更好的用户体验) |
|
|
|
if (!_isFullyConnected.value && _lastConnectedDeviceName != null && _lastConnectedDeviceName!.isNotEmpty) { |
|
|
|
return _lastConnectedDeviceName!; |
|
|
|
} |
|
|
|
|
|
|
|
return 'unknownEquipment'.tr; |
|
|
|
} |
|
|
|
|
|
|
|
/// 获取信号强度(模拟数据) |
|
|
|
|