flutterBlue.connect(device, timeout: const Duration(seconds:



抱歉,我是初学者,英语很差 我在小蝶中遇到了麻烦。 我参考 https://pub.dartlang.org/packages/flutter_blue 我想使用颤振与耳温计设备通信。 当我重新打开蓝牙并且可以工作时,但我重新打开应用程序并执行第二个它显示错误。 我猜蓝牙 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

飞镖错误:未处理的异常: 平台异常(already_connected,与设备的连接已存在,空(

错误 2

飞镖错误:未处理的异常: PlatformException(get_services_error,没有蓝牙 Gatt 实例,你先连接了吗?,空(

错误 1

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

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

您可以在此处做的是检查任何连接的设备,并且 然后断开它们。我写了一个函数来做到这一点, 在这里:

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 讨论:

  • 连接的" 发现服务 " 没有得到 #426
  • 发现服务已损坏 #535

最新更新