【问题标题】:Dart Async Does Not Wait for CompletionDart Async 不等待完成
【发布时间】:2018-12-20 00:31:59
【问题描述】:

我目前正在尝试等待 BLE 连接产生以下两种结果之一:

  • 设备连接成功,或
  • 扫描超时后设备连接失败

不会根据需要返回 true 或 false 值,而是立即返回 null,而无需等待函数完成。

我正在使用 dart 的 Future 和异步功能来等待连接功能的完成。下面是我的代码:

BLE 连接方法:

    static Future<bool> connect(BluetoothDevice d) async {
    // Connect to device
    Duration timeout = new Duration(seconds: 5);

    deviceConnection = _flutterBlue.connect(d, timeout: timeout).listen((s) {
      deviceState = s;
      if (s == BluetoothDeviceState.connected) {
        device = d;

        device.discoverServices().then((s) {
          ... Some service discovery stuff ...
        });
      }
    }, onDone: () {
      return deviceState == BluetoothDeviceState.connected;
    });
  }

从哪里调用connect方法:

bool isConnected = await FlutterBLE.connect(device);

if(isConnected) {
    ... Do some stuff ...
} else {
    ... Do some other stuff ...
}

我在这里做错了什么?

【问题讨论】:

    标签: dart flutter dart-async


    【解决方案1】:

    正如 Günther Zöchbauer 指出的那样,错误出现在 onDone 部分。你在那里返回了一个没人会看到的值,并且你没有从周围的函数返回任何东西。

    你在一个异步函数中,所以你可以使用await for 来迭代流。 您还希望在第一次收到连接事件时停止收听流,因为您只关心第一次连接。连接事件流本身永远不会停止。

    static Future<bool> connect(BluetoothDevice d) async {
      // Connect to device
      Duration timeout = const Duration(seconds: 5);
    
      await for (var s in _flutterBlue.connect(d, timeout: timeout)) {
        deviceState = s;
        if (s == BluetoothDeviceState.connected) {
          device = d;
    
          device.discoverServices().then((s) {
            ... Some service discovery stuff ...
          });
          return true;
        }
      }
      // The stream ended somehow, there will be no further events.
      return false;
    }
    

    如果你不想使用await for(并且不使用异步函数开始),我建议使用firstWhere 来查找listen 上的第一个连接(如果有的话):

    static Future<bool> connect(BluetoothDevice d) {
      // Connect to device
      Duration timeout = const Duration(seconds: 5);
    
      return _flutterBlue.connect(d, timeout: timeout).firstWhere((s) {
        return s == BluetoothDeviceState.connected;
      }, orElse: () => null).then((s) {
        if (s == null) return false;
        deviceState = s;
        device = d;
        device.discoverServices().then((s) {
          //... Some service discovery stuff ...
        });
        return true;
      });
    

    也有点怀疑没有人等待device.discoverServices().then(...) 返回的未来。确保这是正确的。

    【讨论】:

    • Irn,我已经尝试了您的第一个解决方案(我想保持此功能异步),但它似乎对我不起作用。与 Gunther 的解决方案一样,该函数在连接成功时不会返回 true,而是从不返回值。也像 Gunther 的解决方案,当 Duration 超时时,返回正确的值(false)。
    • 我不知道代码是什么意思,所以这只是猜测:您想在第一个s == BlutoothDeviceState.connected 成功时停止收听吗?如果是这样,请在if 的正文末尾添加一个break。这将打破循环。
    • 这非常有效。我承认我很困惑——break 的位置对我来说很有意义,但该方法从未返回值的事实让我感到困惑。感谢您的帮助!
    • 您正在侦听连接事件流,并且仅当流完成或您停止侦听时才会停止侦听。我猜流永远不会关闭(蓝牙设备永远不会停止监听连接),所以你必须通过退出 await for 循环来停止监听。我将更新示例代码。
    【解决方案2】:

    onDone 部分不符合您的预期。
    试试吧:

    static Future<bool> connect(BluetoothDevice d) async {
    // Connect to device
    Duration timeout = new Duration(seconds: 5);
    
    await _flutterBlue.connect(d, timeout: timeout).listen((s) {
      deviceState = s;
      if (s == BluetoothDeviceState.connected) {
        device = d;
    
        device.discoverServices().then((s) {
          ... Some service discovery stuff ...
        });
      }
    }).asFuture();
    return deviceState == BluetoothDeviceState.connected;
    }
    

    【讨论】:

    • Günter,我已经在我的代码中尝试了你的解决方案,但它似乎并不总是有效,我假设这是因为我的另一个误解。如果设备无法连接此解决方案,则 Duration 超时并且该方法返回 false,正如计划的那样。但是,当设备成功连接时,函数永远不会返回,我永远不会从函数返回 bool 值,因为我有方法来检查返回值。可能是什么问题?
    • 你也检查过 lrn 的方法吗?
    • 是的,我有,他的方法产生了相同的结果。为什么函数会这样“挂”?
    • 我不知道flutterBlue。也许_flutterBlue.connect(d, timeout: timeout). 永远不会完成流。你可以试试.first 而不是asFuture()
    猜你喜欢
    • 2021-01-26
    • 2014-06-27
    • 2023-03-10
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 2014-10-25
    • 2019-04-14
    • 2019-04-08
    相关资源
    最近更新 更多