writeBytes method

  1. @override
Future<ConnectionResponse> writeBytes(
  1. List<int> data, {
  2. bool isDisconnect = true,
})
override

Implementation

@override
Future<ConnectionResponse> writeBytes(List<int> data,
    {bool isDisconnect = true}) async {
  if (Platform.isWindows) {
    try {
      if (!this.isConnected) {
        await connect();
        PosPrinterManager.logger.info("connect()");
      }

      // Inform the spooler the document is beginning.
      final dwJob = StartDocPrinter(hPrinter, 1, docInfo!);
      if (dwJob == 0) {
        PosPrinterManager.logger.error("dwJob == 0");
        ClosePrinter(hPrinter);
        return ConnectionResponse.printInProgress;
      }
      // Start a page.
      if (StartPagePrinter(hPrinter) == 0) {
        PosPrinterManager.logger.error("StartPagePrinter == 0");
        EndDocPrinter(hPrinter);
        ClosePrinter(hPrinter);
        return ConnectionResponse.printerNotSelected;
      }

      // Send the data to the printer.
      final lpData = data.toUint8();
      dwCount = data.length;
      if (WritePrinter(hPrinter, lpData, dwCount!, dwBytesWritten!) == 0) {
        PosPrinterManager.logger.error("WritePrinter == 0");
        EndPagePrinter(hPrinter);
        EndDocPrinter(hPrinter);
        ClosePrinter(hPrinter);
        return ConnectionResponse.printerNotWritable;
      }

      // End the page.
      if (EndPagePrinter(hPrinter) == 0) {
        PosPrinterManager.logger.error("EndPagePrinter == 0");
        EndDocPrinter(hPrinter);
        ClosePrinter(hPrinter);
      }

      // Inform the spooler that the document is ending.
      if (EndDocPrinter(hPrinter) == 0) {
        PosPrinterManager.logger.error("EndDocPrinter == 0");
        ClosePrinter(hPrinter);
      }

      // Check to see if correct number of bytes were written.
      if (dwBytesWritten!.value != dwCount) {
        PosPrinterManager.logger.error("dwBytesWritten.value != dwCount");
      }

      if (isDisconnect) {
        // Tidy up the printer handle.
        ClosePrinter(hPrinter);
        // await disconnect();
      }
      return ConnectionResponse.success;
    } catch (e) {
      PosPrinterManager.logger.error("Error : $e");
      return ConnectionResponse.unknown;
    }
  } else if (Platform.isAndroid) {
    if (!this.isConnected) {
      await connect();
      PosPrinterManager.logger.info("connect()");
    }

    PosPrinterManager.logger("start write");
    var bytes = Uint8List.fromList(data);
    int max = 16384;

    /// maxChunk limit on android
    var datas = bytes.chunkBy(max);
    await Future.forEach(
        datas, (dynamic data) async => await usbPrinter.write(data));
    PosPrinterManager.logger("end write bytes.length${bytes.length}");

    if (isDisconnect) {
      try {
        await usbPrinter.close();
        this.isConnected = false;
        this.printer.connected = false;
      } catch (e) {
        PosPrinterManager.logger.error("Error : $e");
        return ConnectionResponse.unknown;
      }
    }
    return ConnectionResponse.success;
  } else {
    return ConnectionResponse.unsupport;
  }
}