connect method

  1. @override
Future<ConnectionResponse> connect({
  1. Duration? timeout = const Duration(seconds: 5),
})
override

Implementation

@override
Future<ConnectionResponse> connect(
    {Duration? timeout: const Duration(seconds: 5)}) async {
  if (Platform.isWindows) {
    try {
      docInfo = calloc<DOC_INFO_1>()
        ..ref.pDocName = pDocName
        ..ref.pOutputFile = nullptr
        ..ref.pDatatype = pDataType;
      szPrinterName = printer.name!.toNativeUtf16();

      final phPrinter = calloc<HANDLE>();
      if (OpenPrinter(szPrinterName, phPrinter, nullptr) == FALSE) {
        PosPrinterManager.logger.error("can not open");
        this.isConnected = false;
        this.printer.connected = false;
        return Future<ConnectionResponse>.value(
            ConnectionResponse.printerNotConnected);
      } else {
        PosPrinterManager.logger.info("szPrinterName: $szPrinterName");
        this.hPrinter = phPrinter.value;
        this.isConnected = true;
        this.printer.connected = true;
        return Future<ConnectionResponse>.value(ConnectionResponse.success);
      }
    } catch (e) {
      this.isConnected = false;
      this.printer.connected = false;
      return Future<ConnectionResponse>.value(ConnectionResponse.timeout);
    }
  } else if (Platform.isAndroid) {
    var usbDevice = await usbPrinter.connect(vendorId!, productId!);
    if (usbDevice != null) {
      print("vendorId $vendorId, productId $productId ");
      this.isConnected = true;
      this.printer.connected = true;
      return Future<ConnectionResponse>.value(ConnectionResponse.success);
    } else {
      this.isConnected = false;
      this.printer.connected = false;
      return Future<ConnectionResponse>.value(ConnectionResponse.timeout);
    }
  } else {
    return Future<ConnectionResponse>.value(ConnectionResponse.timeout);
  }
}