connectionState property

Stream<BrilliantDevice> get connectionState

Stream of connection state changes for the device.

Implementation

Stream<BrilliantDevice> get connectionState {
  return FlutterBluePlus.events.onConnectionStateChanged
      .where((event) =>
          event.connectionState == BluetoothConnectionState.connected ||
          (event.connectionState == BluetoothConnectionState.disconnected &&
              event.device.disconnectReason!.code != 23789258))
      .asyncMap((event) async {
    if (event.connectionState == BluetoothConnectionState.connected) {
      _log.info("Connection state stream: Connected");
      try {
        return await BrilliantBluetooth.enableServices(event.device);
      } catch (error) {
        _log.warning("Connection state stream: Invalid due to $error");
        return Future.error(BrilliantBluetoothException(error.toString()));
      }
    }
    _log.info(
        "Connection state stream: Disconnected due to ${event.device.disconnectReason!.description}");
    if (Platform.isAndroid) {
      event.device.connect(timeout: const Duration(days: 365));
    }
    return BrilliantDevice(
      state: BrilliantConnectionState.disconnected,
      device: event.device,
    );
  });
}