我正在尝试获取所有扫描的ble设备的列表
这是我的代码。
void main() async {
String _data = await getDevices();
print('abcd: ${_data}');
runApp(new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text("Bluetooth low Energy"),
centerTitle: true,
backgroundColor: Colors.orangeAccent,
),
body: new Center(
child: new Text('Loading...'),
),
),
));
}
Future<String> getDevices() async {
BluetoothDevice device;
List<String> dvs = [];
FlutterBlue flutterBlue = FlutterBlue.instance;
var beaconName;
StreamSubscription scn;
scn = await flutterBlue.scan().listen((scanResult) {
device = scanResult.device;
beaconName = device.name;
if (beaconName != "") {
dvs.add(beaconName);
print(beaconName);
if(dvs.length == 10){
scn.cancel();
return dvs;
}
}
});
}
但是最后我得到一个空列表。如何将所有设备添加到要显示在UI上的列表中。