flutter_blue设备连接永远挂起

时间:2020-09-01 04:33:58

标签: android flutter bluetooth bluetooth-lowenergy android-bluetooth

我正在尝试使用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)时生成了此日志。另外,该设备正在运行,我使用某些第三方蓝牙工具对其进行了测试,并且能够接收一些数据。

0 个答案:

没有答案