iOS connectToDevice() 查找设备 ID 失败并出现 PlatformException

问题描述 投票:0回答:1

使用 flutter_reactive_ble 我执行以下操作

await _flutterReactiveBle
    .scanForDevices(withServices: services)
    .timeout(Duration(seconds: scanTimeoutDurationSeconds))
    .firstWhere((device) {
  _deviceId = device.id;
  debugPrint('device discovered: $device');

  return true;
}).onError((error, stackTrace) {
  debugPrint('error: $error');
  debugPrint('stackTrace: $stackTrace');
  _bleConnectionState.value = BleConnectionStatus.disconnected;
  throw BleBikeNotFoundException();
});

_connectionStateStream = _flutterReactiveBle
    .connectToDevice(
  id: _deviceId!,
)
    .listen((update) {
  _handleConnectionStreamUpdate(update);
},
  onError: (Object e) => {
        debugPrint('Connecting to device $_deviceId resulted in error $e'),
        dispose(),
        throw BleConnectionError(),
      });

由此产生的日志如下:

device discovered: DiscoveredDevice(id: E390E5E7-EC0B-37B9-3F26-27068C768EB7, name: imx8mmea-ucom, serviceData: {}, serviceUuids: [a25478a0-c922-11ee-9603-d3a9796f3cfa], manufacturerData: [], rssi: -56, connectable: Connectable.available)

Connecting to device E390E5E7-EC0B-37B9-3F26-27068C768EB7 resulted in error PlatformException(reactive_ble_mobile.Central.(unknown context at $105b88464).Failure:2, The operation couldn’t be completed. (reactive_ble_mobile.Central.(unknown context at $105b88464).Failure error 2.), {}, null)

这在 Android 上运行良好,但在 iOS 上失败。

我在 iPhone 13 mini 上运行最新的 iOS 17.3.1 我已添加并启用所有权限

有人可以提供建议吗?我还在他们的官方 GitHub 问题页面上发布了内容。

flutter bluetooth ios-bluetooth
1个回答
0
投票

经过一番头痛之后,我发现了一个类似的问题,即使用

discoverServices()
(现已弃用)时,包会抛出相同的错误。 https://github.com/PhilipsHue/flutter_reactive_ble/issues/724

出于某种原因,我必须在连接和订阅之间使用延迟,如下所示:

await _flutterReactiveBle
    .scanForDevices(withServices: services)
    .timeout(Duration(seconds: scanTimeoutDurationSeconds))
    .firstWhere((device) {
  _deviceId = device.id;
  logDebug('device discovered: $device');

  return true;
}).onError((error, stackTrace) {
  logDebug('error: $error');
  logDebug('stackTrace: $stackTrace');
  _bleConnectionState.value = BleConnectionStatus.disconnected;
  throw BleBikeNotFoundException();
});

//DELAY HERE
await Future.delayed(const Duration(seconds: 3));

_connectionStateStream = _flutterReactiveBle
    .connectToDevice(
  id: _deviceId!,
)
    .listen((update) {
  _handleConnectionStreamUpdate(update);
},
        onError: (Object e) => {
              logDebug('Connecting to device $_deviceId resulted in error $e'),
              dispose(),
            });
//DELAY HERE
await Future.delayed(const Duration(seconds: 3));

final characteristic = QualifiedCharacteristic(characteristicId: bleCharacteristicGuid, serviceId: bikeNft.uuid, deviceId: _deviceId!);


_subscriptionStream = _flutterReactiveBle.subscribeToCharacteristic(characteristic).listen((data) {
  _handleSubscriptionStreamData(data);
}, onError: (Object error) {
  logDebug('Subscribing to device $_deviceId resulted in error $error');
  _bleConnectionState.value = BleConnectionStatus.disconnected;
  throw BleBikeNotFoundException();
});
© www.soinside.com 2019 - 2024. All rights reserved.