扫描设备列表在flutterblue中返回null

时间:2019-11-14 08:34:49

标签: flutter bluetooth-lowenergy

我正在尝试获取所有扫描的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上的列表中。

0 个答案:

没有答案