gpt4 book ai didi

flutterBlue.connect(device, timeout : const Duration(seconds: 10), ).listen(null);在 flutter 中

转载 作者:行者123 更新时间:2023-12-05 07:29:04 31 4
gpt4 key购买 nike

对不起,我是初学者,我的英语很差我在 Flutter 遇到了麻烦。我引用 https://pub.dartlang.org/packages/flutter_blue我想使用 flutter 与耳温计设备进行通信。当我重新打开蓝牙并且它可以工作时,但是我重新打开应用程序并执行第二次它显示错误。我猜蓝牙 gatt cahce 导致错误。我该如何解决?

我的代码

deviceConnection = flutterBlue.scan(timeout: const Duration(seconds: 5),).listen((scanResult) async {
device = scanResult.device;
deviceConnection2 = await flutterBlue.connect(device, timeout: const Duration(seconds: 10),).listen(null);//this line error is error 1
deviceStateSubscription = device.onStateChanged().listen((s) async {
await device.discoverServices().then((s) async {//this is error 2
.........
}
}
}

它显示这些错误

错误 1

Dart Error: Unhandled exception: PlatformException(already_connected, connection with device already exists, null)

错误2

Dart Error: Unhandled exception: PlatformException(get_services_error, no instance of BluetoothGatt, have you connected first?, null)

最佳答案

错误 1

Dart Error: Unhandled exception: PlatformException(already_connected, connection with device already exists, null)

由于该应用检测到蓝牙设备已连接,一种解决方法是检查是否有任何已连接的设备将其断开。您可以创建一个像 this GitHub thread 中那样执行此操作的函数:

What you can do here is that you check for any connected devices andthen disconnect them. I have written a function that does just that,here it is:

checkConnectedDevices() async{

connectedDevices = await flutterBlue.connectedDevices.whenComplete((){
print(connectedDevices);
for(int i = 0; i < connectedDevices.length; i++){
connectedDevices[i].disconnect();
}
});

}

错误 2

Dart Error: Unhandled exception: PlatformException(get_services_error, no instance of BluetoothGatt, have you connected first?, null)

由于连接到设备时出错,您将无法调用 discoverServices()。因为你需要连接到设备才能调用这个函数。检查以下与此错误相关的 GitHub 讨论:

关于flutterBlue.connect(device, timeout : const Duration(seconds: 10), ).listen(null);在 flutter 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52946926/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com