如何解决使用 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 举报,一经查实,本站将立刻删除。