Flutter_reactive_ble 连接流不更新连接状态

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

我在使用 flutter_reactive_ble 包和 Riverpod 生成器时遇到问题。

我能够扫描设备,但是当我选择设备时我想要连接到 我第一次尝试连接时,我只将其输出到控制台中。

颤振:设备未连接 颤动:开始连接到 3d19f04a-1e9e-1406-68de-2b6085bf185c

如果我第二次再试一次,我会得到这个:

颤动:设备未连接 颤动:开始连接到 3d19f04a-1e9e-1406-68de-2b6085bf185c flutter:设备 3d19f...f185c 的 ConnectionState :DeviceConnectionState.connecting 颤动:设备连接

然后就没有别的了,我从来没有得到过它已连接的更新。

我的View Widget构建方法调用连接是这样的

@override
  Widget build(context, ref) {
    final bleStateUpdate = ref.watch(bleStatusMonitorProvider);
    final connectionStateUpdate = ref.watch(bleDeviceConnectorProvider);
    final connectionState = connectionStateUpdate.connectionState ??
        DeviceConnectionState.disconnected;

    if (bleStateUpdate == BleStatus.ready) {
      switch (connectionState) {
        case DeviceConnectionState.connected:
          debugPrint('device connected');
          break;
        case DeviceConnectionState.disconnected:
          debugPrint('Device not connected');
          ref
              .read(bleDeviceConnectorProvider.notifier)
              .connect(ref.read(notioDeviceProvider)!.id.toString());
          break;
        case DeviceConnectionState.connecting:
          debugPrint('device connecting');
          break;
        case DeviceConnectionState.disconnecting:
          debugPrint('device disconnecting');
          break;
        default:
          debugPrint('unknown connection state');
      }
    }

    ....

这是我的提供者

.... Imports ... 

@Riverpod(keepAlive: true)
class BleDeviceConnector extends _$BleDeviceConnector {
  final FlutterReactiveBle _ble = locator.get<FlutterReactiveBle>();
  StreamSubscription<ConnectionStateUpdate>? _connectionStreamSub;
  final Duration _connectionTimeout = const Duration(seconds: 10);

  @override
  ConnectionStateUpdate build() {
    return const ConnectionStateUpdate(
        deviceId: "",
        connectionState: DeviceConnectionState.disconnected,
        failure: null);
  }

  Future<void> connect(String deviceId) async {
    debugPrint('Start connecting to $deviceId');
    _connectionStreamSub = _ble
        .connectToDevice(id: deviceId, connectionTimeout: _connectionTimeout)
        .listen(
          handleConnectionState,
          onError: (Object e) =>
              debugPrint('Connecting to device $deviceId resulted in error $e'),
        );
  }

  Future<void> disconnect(String deviceId) async {
    try {
      debugPrint('disconnecting to device: $deviceId');
      await _connectionStreamSub?.cancel();
    } on Exception catch (e, _) {
      debugPrint("Error disconnecting from a device: $e");
    } finally {
      state = ConnectionStateUpdate(
        deviceId: deviceId,
        connectionState: DeviceConnectionState.disconnected,
        failure: null,
      );
    }
  }

  Future<void> handleConnectionState(ConnectionStateUpdate event) async {
    debugPrint(
        'ConnectionState for device ${event.deviceId} : ${event.connectionState}');
    state = event;
  }

  Future<void> dispose() async {
    await _connectionStreamSub?.cancel();
  }
}

我不确定这是否是 Flutter_reactive_ble 问题,或者这是否是我使用 bleDeviceConnectorProvider 的方式。

flutter bluetooth-lowenergy riverpod riverpod-generator
1个回答
0
投票

我通过使用 flutter_blue_plus 解决了我的问题,这不是兼容性问题。

© www.soinside.com 2019 - 2024. All rights reserved.