簡體   English   中英

flutter_blue 設備連接永遠掛起

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM