[英]flutter_blue device connect hangs forever
我正在尝试使用 flutter_blue 库打开与设备的蓝牙 LE 连接。 一切正常,除了连接方法总是超时。
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
...
这是 output:
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:我使用带有蓝牙 LE 的真实设备、我的笔记本电脑和另一部智能手机进行了测试,错误始终相同。 此日志是在尝试连接到另一部智能手机 (Android) 时生成的。 此外,该设备正在运行,我使用一些第三方蓝牙工具对其进行了测试,它能够接收到一些数据。
我对我所做的是遵循此问题线程中发布的解决方案。
连接超时后尝试断开连接,然后再尝试重新连接。
try{
// Attempt connection
} catch (Exception e){
// Clear connection
device.disconnect();
return e;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.