connect method

Future<void> connect({
  1. bool setDarwinOpenExclusive = false,
})

Implementation

Future<void> connect({bool setDarwinOpenExclusive = false}) async {
  if (_isConnected && _api != null && _provider != null) return;

  if (_connectingCompleter != null) {
    return _connectingCompleter!.future;
  }

  final completer = Completer<void>();
  _connectingCompleter = completer;
  try {
    final productId = fetchHidapiVitureProductIds(
      setDarwinOpenExclusive: setDarwinOpenExclusive,
    );
    if (productId == null) {
      throw StateError('Unable to find the glasses.');
    }

    final dylib = ffi.DynamicLibrary.open(_resolveDylibPath());
    final api = bindings.VitureKitBindings(dylib);
    final provider = api.xr_device_provider_create(productId);

    if (provider == ffi.nullptr) {
      throw StateError('Unable to find the glasses.');
    }

    api.xr_device_provider_initialize(provider, ffi.nullptr, ffi.nullptr);
    api.xr_device_provider_start(provider);

    _api = api;
    _provider = provider;
    _isConnected = true;

    _registerStateCallback();

    completer.complete();
  } catch (e) {
    _api = null;
    _provider = null;
    _isConnected = false;
    completer.completeError(e);
    rethrow;
  } finally {
    _connectingCompleter = null;
  }
}