绑定设备+覆盖模式+控制显示bug+在线更新

This commit is contained in:
2026-09-01 08:59:20 +08:00
parent 0fa67a127b
commit 61bce2c1cf
49 changed files with 2424 additions and 720 deletions

View File

@@ -21,6 +21,7 @@ class _BluetoothScanModalState extends State<BluetoothScanModal> {
int _connectingCountdown = 15;
Timer? _connectingTimer;
bool _isStartingScan = false;
String _searchQuery = '';
StreamSubscription<BluetoothAdapterState>? _stateSubscription;
StreamSubscription<List<ScanResult>>? _scanSubscription;
@@ -123,7 +124,7 @@ class _BluetoothScanModalState extends State<BluetoothScanModal> {
await BleManager.instance.openBluetooth();
}
/// 排序:已连接设备排第一,有名称设备优先
/// 排序:已连接设备排第一,纯数字名称(时间戳样式)置顶,有名称设备优先
List<ScanResult> _sortResults(List<ScanResult> results) {
final mgrConnected = BleManager.instance.connectedDevice;
final sorted = List<ScanResult>.from(results);
@@ -133,7 +134,12 @@ class _BluetoothScanModalState extends State<BluetoothScanModal> {
final bConnected = b.device.remoteId == mgrConnected?.remoteId;
if (aConnected && !bConnected) return -1;
if (!aConnected && bConnected) return 1;
// 第二级:有名称的排前面
// 第二级:纯数字名称(时间戳样式,如 17xxxx)排前面
final aIsNumeric = _isNumericName(a);
final bIsNumeric = _isNumericName(b);
if (aIsNumeric && !bIsNumeric) return -1;
if (!aIsNumeric && bIsNumeric) return 1;
// 第三级:有名称的排前面
final aHasName = _hasDeviceName(a);
final bHasName = _hasDeviceName(b);
if (aHasName && !bHasName) return -1;
@@ -152,6 +158,27 @@ class _BluetoothScanModalState extends State<BluetoothScanModal> {
r.device.advName.isNotEmpty;
}
/// 获取设备显示名称
String _getDeviceName(ScanResult r) {
final advData = r.advertisementData;
return advData.advName.isNotEmpty
? advData.advName
: (r.device.platformName.isNotEmpty
? r.device.platformName
: (advData.localName?.isNotEmpty == true
? advData.localName!
: (r.device.advName.isNotEmpty
? r.device.advName
: '${r.device.remoteId}')));
}
/// 判断设备名称是否为纯数字(时间戳样式,如 1777272534998)
bool _isNumericName(ScanResult r) {
final name = _getDeviceName(r);
if (name.isEmpty) return false;
return RegExp(r'^\d+$').hasMatch(name);
}
/// 从详情页返回时同步连接状态(包括连接中和已连接)
void _syncConnectedDevice() {
final mgrConnected = BleManager.instance.connectedDevice;
@@ -448,12 +475,22 @@ class _BluetoothScanModalState extends State<BluetoothScanModal> {
Widget _buildDeviceList() {
// 过滤掉已连接设备(它已在状态卡片下方单独显示)
final otherResults = _connectedDevice != null
var otherResults = _connectedDevice != null
? _scanResults
.where((r) => r.device.remoteId != _connectedDevice!.remoteId)
.toList()
.where((r) => r.device.remoteId != _connectedDevice!.remoteId)
.toList()
: _scanResults;
// 按搜索关键词过滤
if (_searchQuery.isNotEmpty) {
final query = _searchQuery.toLowerCase();
otherResults = otherResults.where((r) {
final name = _getDeviceName(r).toLowerCase();
final mac = r.device.remoteId.toString().toLowerCase();
return name.contains(query) || mac.contains(query);
}).toList();
}
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
@@ -466,6 +503,53 @@ class _BluetoothScanModalState extends State<BluetoothScanModal> {
),
),
const SizedBox(height: 12),
// 搜索框
Container(
margin: const EdgeInsets.only(bottom: 12),
padding: const EdgeInsets.symmetric(horizontal: 12),
decoration: BoxDecoration(
color: const Color(0xFFF2F3F5),
borderRadius: BorderRadius.circular(8),
),
child: Row(
children: [
const Icon(Icons.search, size: 18, color: Color(0xFF86909C)),
const SizedBox(width: 8),
Expanded(
child: TextField(
onChanged: (value) {
setState(() => _searchQuery = value);
},
decoration: const InputDecoration(
hintText: '搜索设备名称或MAC',
hintStyle: TextStyle(
fontSize: 13,
color: Color(0xFF86909C),
),
border: InputBorder.none,
isDense: true,
contentPadding: EdgeInsets.symmetric(vertical: 10),
),
style: const TextStyle(
fontSize: 13,
color: Color(0xFF1D2129),
),
),
),
if (_searchQuery.isNotEmpty)
GestureDetector(
onTap: () {
setState(() => _searchQuery = '');
},
child: const Icon(
Icons.close,
size: 16,
color: Color(0xFF86909C),
),
),
],
),
),
if (!_isBluetoothOn)
const Padding(
padding: EdgeInsets.symmetric(vertical: 24),
@@ -487,20 +571,20 @@ class _BluetoothScanModalState extends State<BluetoothScanModal> {
),
)
else if (otherResults.isEmpty)
const Padding(
padding: EdgeInsets.symmetric(vertical: 24),
Padding(
padding: const EdgeInsets.symmetric(vertical: 24),
child: Center(
child: Column(
children: [
SizedBox(
width: 24,
height: 24,
child: CircularProgressIndicator(strokeWidth: 2),
child: const CircularProgressIndicator(strokeWidth: 2),
),
SizedBox(height: 12),
const SizedBox(height: 12),
Text(
'正在扫描附近设备...',
style: TextStyle(color: Color(0xFF86909C)),
_searchQuery.isNotEmpty ? '未找到匹配的设备' : '正在扫描附近设备...',
style: const TextStyle(color: Color(0xFF86909C)),
),
],
),
@@ -581,10 +665,7 @@ class _BluetoothScanModalState extends State<BluetoothScanModal> {
),
child: const Text(
'已连接',
style: TextStyle(
fontSize: 10,
color: Colors.white,
),
style: TextStyle(fontSize: 10, color: Colors.white),
),
),
],