Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print only work the first time and I need to restart the bluetooth to print again. #416

Open
MarwenBenTalebAli opened this issue Dec 6, 2022 · 1 comment

Comments

@MarwenBenTalebAli
Copy link

MarwenBenTalebAli commented Dec 6, 2022

I want to create a REST service with Node.js using Express that prints tickets to ESC/POS bluetooth printer.

The actual result my service print only the first time and I need to restart the bluetooth to print again.

I tried update my service to only look for the printer and return it but it only works a couple of times. I received this error in the console:

(node:16608) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [Device]. Use emitter.setMaxListeners() to increase limit (Use node --trace-warnings ... to show where the warning was created)

The expected result print data multiple times

Here is my printerService.js :

const { Printer } = require("escpos");
const Bluetooth = require("escpos-bluetooth");

const foundPrinter = async (name, address, channel, printData) => {
  const PRINTER_NAME = name;
  const PRINTER_ADDRESS = address;
  const PRINTER_CHANNEL = channel;
  const preferredPrinterAddress = "(" + PRINTER_ADDRESS + ")" + PRINTER_NAME;
  const availablePrinters = await Bluetooth.findPrinters();
  console.log("availablePrinters", availablePrinters);
  const btPrinter =
    availablePrinters && availablePrinters.length > 0
      ? availablePrinters.filter(
          (p) =>
            p &&
            p.name === PRINTER_NAME &&
            p.address === preferredPrinterAddress
        )[0]
      : false;

  console.log("btPrinter", btPrinter);
  return new Promise((resolve, reject) => {
    if (btPrinter) {
      if (!printData) {
        resolve({ printer: btPrinter });
      } else {
        const device = new Bluetooth(PRINTER_ADDRESS, PRINTER_CHANNEL);
        const printer = new Printer(device);
        device.open(function (deviceError) {
          if (!deviceError) {
            printer
              .font("a")
              .align("ct")
              .style("bu")
              .size(1, 1)
              .text("Text1")
              .text("Text2")
              .barcode("1234567", "EAN8")
              .qrimage("qrImage", function (qrImageError) {
                if (!qrImageError) {
                  this.cut();
                  this.close();
                } else {
                  reject({
                    name: qrImageError.name,
                    message: qrImageError.message,
                  });
                }
              });
            resolve({ printer: btPrinter });
          } else {
            reject({ name: deviceError.name, message: deviceError.message });
          }
        });
      }
    } else {
      reject({
        errorType: "ERROR_PRINTER_NOT_FOUND",
        errorName: "PRINTER_NOT_FOUND",
        message: "printer not found",
        fullMessage:
          "printer not found, availablePrinters: " +
          availablePrinters.toString(),
      });
    }
  });
};
@allicodev
Copy link

Hello @MarwenBenTalebAli how did you install escpos-bluetooth btw ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants