Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 平台异常(连接,未找到外围设备,空)_Flutter_Bluetooth - Fatal编程技术网

Flutter 平台异常(连接,未找到外围设备,空)

Flutter 平台异常(连接,未找到外围设备,空),flutter,bluetooth,Flutter,Bluetooth,使用Flatter_blue软件包连接到Flatter中的蓝牙设备时出错。软件包版本为^0.6.3+1 这是我的源代码: void scanForDevices() async { sController = StreamController(); //await checkConnectedDevices(); sController.addStream(FlutterBlue.instance.scanResults); FlutterBlue.inst

使用Flatter_blue软件包连接到Flatter中的蓝牙设备时出错。软件包版本为^0.6.3+1

这是我的源代码:

void scanForDevices() async {
    sController = StreamController();

    //await checkConnectedDevices();

    sController.addStream(FlutterBlue.instance.scanResults);

    FlutterBlue.instance.startScan(timeout: Duration(seconds: 10));

    scanSubscription = sController.stream.listen((devices) async {
      final result = devices.where((currentDev) => currentDev.advertisementData.localName.contains((_dev as BaseDevice).psn)).toList();

      if (null == deviceToConnect && null != result && result.length > 0) {
        final dev = result.first;
        deviceToConnect = dev.device;
        print(dev.advertisementData.localName);

        try {
          await dev.device.connect();

          await checkState();

          setState(ViewState.Idle);
        } catch (e) {
          setState(ViewState.Idle);

          print(e);
        }

        //stopScanning();

        setState(ViewState.Idle);
      }
    });
  }

void stopScanning() async {
    print('stopping scan');

    await FlutterBlue.instance.stopScan();

    if (null != scanSubscription) await scanSubscription.cancel();

    if (null != sController) await sController.close();
  }
很好,但并不总是这样。例如:

  • 第一次很好
  • 第二次不行
  • 第三次很好
  • 第四次不行

提供有关错误的一些详细信息,如完全异常等。它会返回平台异常(连接、未找到外围设备、空)异常。没有其他内容。提供有关错误的一些详细信息,例如complete exception etcOnly返回PlatformException(连接、未找到外围设备、空)异常。没有别的了。