Flutter Blue未列出可用器械

r6l8ljro  于 2023-03-09  发布在  Flutter
关注(0)|答案(1)|浏览(126)

我尝试列出可用的ble设备,但列表为空

flutterBlue.startScan(timeout: Duration(seconds: 4));
var subscription = flutterBlue.scanResults.listen((results) {
  for (ScanResult r in results) {
    for (ScanResult r in results) {
      print('${r.device.name} found! rssi: ${r.rssi}');
    }
  }
});
5t7ly7z5

5t7ly7z51#

这对我很有效

//Get bluetooth results
Future<List<ScanResult>> bluetoothScan()async{
  List<ScanResult> bluetoothDevices = [];
  FlutterBlue flutterBlue = FlutterBlue.instance;
  //Stop scan before restarting scan too prevent errors when trying to scan
  await flutterBlue.stopScan();
  print("Start scan");
  // Start scanning
  List<dynamic> foundDevices = await flutterBlue.startScan(
    timeout: const Duration(
      seconds: 10,
    ),
  ).asStream().toList();
  await flutterBlue.stopScan();
  print("Scan end");
  for(dynamic devices in foundDevices){
    for(ScanResult device in devices){
      bluetoothDevices.add(device);
    }
  }
  return bluetoothDevices;
}

但是,由于某种原因,设备名称为空

相关问题