flutter_blue设备连接永远挂起

问题描述

我正在尝试使用Flutter_blue库打开与设备的Bluetooth LE连接。 一切正常,除了方法connect总是会超时。

final device = await startScanAndListenForDevice();
print('norDIC -> Found device. Analysing ...');
print('norDIC -> Found JT Tag device. Relevant info:');
print('norDIC ->   - id   : ${device.id}');
print('norDIC ->   - type : ${device.type}');
print('norDIC ->   - name : ${device.name}');
print('norDIC -> Connecting ...');

// Check if device is already connected
try {

  // Hangs here
  await device.connect(autoConnect: true,timeout: const Duration(seconds: 30));

} on PlatformException catch (e) {
  if (e.code != 'already_connected') {
    print('norDIC -> Failed to connect: $e');
    return null;
  }
  print('norDIC -> Already connected. Ignoring.');
} on Exception catch (e) {
  print('norDIC -> Failed to connect: $e');
  return null;
}

// Code to run after connection
...

以下是输出

I/Flutter (30273): norDIC -> Found device 38:9A:F6:25:BC:DC/galaxy J5 Prime
I/Flutter (30273): norDIC -> Bluetooth scan cleared
I/Flutter (30273): norDIC -> Closing stream
I/Flutter (30273): norDIC -> Found device. Analysing ...
I/Flutter (30273): norDIC -> Found JT Tag device. Relevant info:
I/Flutter (30273): norDIC ->   - id   : 38:9A:F6:25:BC:DC
I/Flutter (30273): norDIC ->   - type : BluetoothDeviceType.classic
I/Flutter (30273): norDIC ->   - name : galaxy J5 Prime
I/Flutter (30273): norDIC -> Connecting ...
D/BluetoothManager(30273): getConnectedDevices
D/BluetoothGatt(30273): connect() - device: 38:9A:F6:25:BC:DC,auto: true
D/BluetoothGatt(30273): registerapp()
D/BluetoothGatt(30273): registerapp() - UUID=d490238f-6fd1-4d5b-805b-6ad0ff46a260
D/BluetoothGatt(30273): onClientRegistered() - status=0 clientIf=7
D/BluetoothAdapter(30273): isLeEnabled(): ON
D/BluetoothLeScanner(30273): Could not find callback wrapper
D/BluetoothManager(30273): getConnectionState()
D/BluetoothManager(30273): getConnectedDevices
... timeout ...

OBS:我使用带Bluetooth LE,笔记本电脑和其他智能手机的真实设备进行了测试,错误始终是相同的。尝试连接到其他智能手机(Android)时生成了此日志。另外,该设备正在运行,我使用某些第三方蓝牙工具对其进行了测试,并且能够接收一些数据。

解决方法

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

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

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