使用 flutter_ble_lib 扫描 BLE 设备时出现问题流超时过早

问题描述

我目前正在使用 Dart/Flutter 和 Flutter_ble_lib https://github.com/Polidea/FlutterBleLib 为 Android 和 iOS 编写应用程序。 以下代码在 iOS 设备上的表现有点奇怪,我无法弄清楚发生了什么。在第一次扫描时,它就像魅力一样。但是,当我第二次扫描时,该函数立即跳转到超时块。我是不是遗漏了什么,还是我使用了流监听器或超时错误

我会非常感谢您的建议。

最好的问候!

  Future<dynamic> start_ble_scan() async {
    Completer completer = new Completer();

    if (_mydevice != null) {
      bool connected = await _mydevice.isConnected();
      print("DeviceState: " + connected.toString());
      if (connected) {
        try {
          await _responseSubscription?.cancel();
          await _characSubscription?.cancel();
          await _condeviceStateSubscription?.cancel();
          await _mydevice.disconnectOrCancelConnection();
          _mydevice = null;
          _currentDeviceConnected = false;
        } catch (e) {
          print("disconnecting device before new scan process");
        }
      }
    }

    SharedPreferences prefs = await SharedPreferences.getInstance();
    String savedDevice = prefs.getString("Devicename");
    String savedIdentifier = prefs.getString("macnum");
    bool alreadyStoppedScanning = false;

    _bleonSubscription = _activateBleManager
        .startperipheralscan()
        .timeout(Duration(seconds: 30),onTimeout: (timeout) async {
      if (!alreadyStoppedScanning) {
        await _activateBleManager.stopperipheralscan();
      }
    }).listen(
      (data) async {
        if ((data.peripheral.name == savedDevice) ||
            (data.peripheral.identifier == savedIdentifier)) {
          if (!alreadyStoppedScanning) {
            alreadyStoppedScanning = true;
            _mydevice = data.peripheral;
            print("Device found: " + _mydevice.toString());
            await _activateBleManager.stopperipheralscan();
            _bleonSubscription?.cancel();
          }

        }
      },onError: (err) {
        print('Error!: $err');
      },cancelOnError: true,onDone: () async {
        print("Device found: " + _mydevice.toString());
        completer.complete(true);
      },);
    return completer.future;
  }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)