You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter使用Reactive Blue库开发蓝牙应用如何筛选指定名称的扫描设备

解决方案

你可以在遍历生成列表项之前,先对扫描到的设备列表做过滤处理,添加where筛选条件即可,同时兼容设备名称为空的边界场景,避免运行时报错。

修改后的代码

Flexible(
  child: ListView(
    children: widget.scannerState.discoveredDevices
        // 新增过滤条件,仅保留名称包含mydevice的设备
        .where((device) => device.name?.contains('mydevice') ?? false)
        .map(
          (device) => ListTile(
            title: Text(device.name),
            subtitle: Text("${device.id}\nRSSI: ${device.rssi}"),
            leading: const BluetoothIcon(),
            onTap: () async {
              widget.stopScan();
              await Navigator.push<void>(
                  context,
                  MaterialPageRoute(
                      builder: (_) =>
                          DeviceDetailScreen(device: device)));
            },
          ),
        )
        .toList(),
  ),
),

补充说明

  • 如果需要不区分大小写匹配,可以把过滤条件改成:device.name?.toLowerCase().contains('mydevice'.toLowerCase()) ?? false
  • 如果你需要完全匹配设备名称而不是包含匹配,把contains方法替换为==即可:device.name == 'mydevice'

内容的提问来源于stack exchange,提问作者Jaeseo Lee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 21:57:00