open method

Future<int> open(
  1. AppInfo appInfo, {
  2. void onDeviceArrival(
    1. CaptureHelperDevice
    )?,
  3. void onDeviceRemoval(
    1. CaptureHelperDevice
    )?,
  4. void onDecodedData(
    1. DecodedData,
    2. CaptureHelperDevice
    )?,
  5. void onError(
    1. CaptureException
    )?,
  6. void onBatteryLevel(
    1. int,
    2. CaptureHelperDevice
    )?,
  7. void onPowerState(
    1. int,
    2. CaptureHelperDevice
    )?,
  8. void onButtons(
    1. int,
    2. CaptureHelperDevice
    )?,
  9. void onSocketCamCanceled(
    1. CaptureHelperDevice
    )?,
  10. void onDiscoveredDevice(
    1. DiscoveredDeviceInfo
    )?,
  11. void onDiscoveryEnd(
    1. int
    )?,
  12. void onLogTrace(
    1. String
    )?,
})

Opens an SDK session using the provided appInfo.

All callbacks are optional named parameters. Events arriving before openClient returns are buffered and replayed in order after open completes.

Throws CaptureException with SktErrors.ESKT_ALREADYDONE if called while already open. Throws CaptureException on connection failure.

Implementation

Future<int> open(
  AppInfo appInfo, {
  void Function(CaptureHelperDevice)? onDeviceArrival,
  void Function(CaptureHelperDevice)? onDeviceRemoval,
  void Function(DecodedData, CaptureHelperDevice)? onDecodedData,
  void Function(CaptureException)? onError,
  // Phase 2 callbacks (stored but not yet routed)
  void Function(int, CaptureHelperDevice)? onBatteryLevel,
  void Function(int, CaptureHelperDevice)? onPowerState,
  void Function(int, CaptureHelperDevice)? onButtons,
  void Function(CaptureHelperDevice)? onSocketCamCanceled,
  void Function(DiscoveredDeviceInfo)? onDiscoveredDevice,
  void Function(int)? onDiscoveryEnd,
  void Function(String)? onLogTrace,
}) async {
  if (_capture != null) {
    throw CaptureException(
      SktErrors.ESKT_ALREADYDONE,
      'CaptureHelper is already open. Call close() before opening again.',
    );
  }

  _onDeviceArrival = onDeviceArrival;
  _onDeviceRemoval = onDeviceRemoval;
  _onDecodedData = onDecodedData;
  _onError = onError;
  _onBatteryLevel = onBatteryLevel;
  _onPowerState = onPowerState;
  _onButtons = onButtons;
  _onSocketCamCanceled = onSocketCamCanceled;
  _onDiscoveredDevice = onDiscoveredDevice;
  _onDiscoveryEnd = onDiscoveryEnd;
  _onLogTrace = onLogTrace;

  final Capture capture = _captureFactory();
  _opening = true;

  try {
    await capture.openClient(appInfo, _handleEvent);
  } catch (e) {
    _opening = false;
    _bufferedEvents.clear();
    _clearCallbacks();
    if (e is CaptureException) {
      rethrow;
    }
    throw CaptureException(
      SktErrors.ESKT_COMMUNICATIONERROR,
      'Failed to open CaptureSDK session.',
      e.toString(),
    );
  }

  _capture = capture;
  _opening = false;

  if (Platform.isAndroid) {
    final int clientHandle = _capture!.clientOrDeviceHandle ?? 0;
    await CapturePlugin.startSocketCamExtension(clientHandle, (_) {});
  }

  // Drain buffered events that arrived during openClient
  final List<({CaptureEvent event, int handle})> buffered =
      List<({CaptureEvent event, int handle})>.from(_bufferedEvents);
  _bufferedEvents.clear();
  for (final ({CaptureEvent event, int handle}) item in buffered) {
    await _handleEvent(item.event, item.handle);
  }

  return _capture!.clientOrDeviceHandle ?? 0;
}