januslo / react-native-bluetooth-escpos-printer

React-Native plugin for the bluetooth ESC/POS & TSC printers.
MIT License
367 stars 424 forks source link

Bluetooth printing error: Printer feeds paper but doesn't complete printing #247

Open JackTorresR opened 5 months ago

JackTorresR commented 5 months ago

Sometimes, even though my Bluetooth app is connected, the printer doesn't print; it just begins feeding the paper as if it's about to print but doesn't complete the action, making it seem like the process is finished. The issue might be due to my code ignoring something or improperly using Promise.resolve(null), causing the printer to fail to print correctly. To resolve the issue, you may need to restart the app using the reload function, or if you have the app as an .apk, you may need to forcefully shut it down via Android.

Code:

import { BluetoothEscposPrinter } from "react-native-bluetooth-escpos-printer";
import { agefisLogo } from "../assets/agefisLogo";
import dayjs from "dayjs";
import { getInspectorName, getDocumentNumber } from ".";

const PrintMeasurement = async (measurementDetails = {}) => {
  const { soundMeter, measurementItems = [], measurementInspectors = [] } = measurementDetails;

  const measurementColumns = [3, 13, 10, 6];
  const inspectorColumns = [20, 12];

  const printMeasurementRow = async (measurement, index) => {
    await BluetoothEscposPrinter.printColumn(
      measurementColumns,
      [
        BluetoothEscposPrinter.ALIGN.CENTER,
        BluetoothEscposPrinter.ALIGN.CENTER,
        BluetoothEscposPrinter.ALIGN.CENTER,
        BluetoothEscposPrinter.ALIGN.CENTER,
      ],
      [
        `${index + 1}°`,
        `${measurement?.sourceNoise} dB(A)`,
        dayjs(measurement?.dateTime).format("DD/MM/YY"),
        dayjs(measurement?.dateTime).format("HH:mm"),
      ],
      { encoding: "UTF-8" }
    );
  };

  const printInspectorRow = async (inspector) => {
    await BluetoothEscposPrinter.printColumn(
      inspectorColumns,
      [
        BluetoothEscposPrinter.ALIGN.LEFT,
        BluetoothEscposPrinter.ALIGN.CENTER,
      ],
      [
        getInspectorName(inspector),
        inspector?.registrationNumber?.toString(),
      ],
      {}
    );
  };

  try {
    await BluetoothEscposPrinter.printText("\r\n\r\n\r\n", {});
    await BluetoothEscposPrinter.printPic(agefisLogo, {
      width: 320,
      left: 25,
    });
    await BluetoothEscposPrinter.printerAlign(
      BluetoothEscposPrinter.ALIGN.CENTER
    );
    await BluetoothEscposPrinter.printText(
      "Measurement of Sound Pressure Level\r\n",
      {}
    );
    await BluetoothEscposPrinter.printText("\r\n\r\n", {});
    await BluetoothEscposPrinter.printColumn(
      [32],
      [BluetoothEscposPrinter.ALIGN.CENTER],
      [`AUTO N°: ${getDocumentNumber(measurementDetails)}`],
      { encoding: "UTF-8" }
    );
    await BluetoothEscposPrinter.printText("\r\n\r\n", {});
    await BluetoothEscposPrinter.printColumn(
      measurementColumns,
      [
        BluetoothEscposPrinter.ALIGN.CENTER,
        BluetoothEscposPrinter.ALIGN.CENTER,
        BluetoothEscposPrinter.ALIGN.CENTER,
        BluetoothEscposPrinter.ALIGN.CENTER,
      ],
      ["", "Measurement", "Date", "Time"],
      {}
    );
    for (const [index, measurement] of measurementItems?.entries()) {
      await printMeasurementRow(measurement, index);
    }
    await BluetoothEscposPrinter.printText("\r\n", {});
    await BluetoothEscposPrinter.printColumn(
      [32],
      [BluetoothEscposPrinter.ALIGN.CENTER],
      ["RESULT: *** INFRACTION ***"],
      {}
    );
    await BluetoothEscposPrinter.printColumn(
      [32],
      [BluetoothEscposPrinter.ALIGN.CENTER],
      ["Equipment Serial Number:"],
      { encoding: "UTF-8" }
    );
    await BluetoothEscposPrinter.printColumn(
      [32],
      [BluetoothEscposPrinter.ALIGN.CENTER],
      [soundMeter],
      {}
    );
    await BluetoothEscposPrinter.printText("\r\n", {});
    await BluetoothEscposPrinter.printColumn(
      inspectorColumns,
      [
        BluetoothEscposPrinter.ALIGN.CENTER,
        BluetoothEscposPrinter.ALIGN.CENTER,
      ],
      ["Inspector", "Registration Number"],
      {}
    );

    for (const inspector of measurementInspectors) {
      await printInspectorRow(inspector);
    }

    await BluetoothEscposPrinter.printText("\r\n", {});
    await BluetoothEscposPrinter.printColumn(
      [32],
      [BluetoothEscposPrinter.ALIGN.CENTER],
      ["AGEFIS"],
      {}
    );
    await BluetoothEscposPrinter.printColumn(
      [32],
      [BluetoothEscposPrinter.ALIGN.CENTER],
      ["Agency of Inspection"],
      {}
    );
    await BluetoothEscposPrinter.printColumn(
      [32],
      [BluetoothEscposPrinter.ALIGN.CENTER],
      ["of Fortaleza"],
      {}
    );
    await BluetoothEscposPrinter.printText("\r\n\r\n\r\n", {});
  } catch (e) {
    alert(e.message || "ERROR");
  }
};

export default PrintMeasurement;

Package.json

{
  "name": "agefis-fiscal",
  "displayName": "Agefis Fiscal",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "android": "react-native run-android --no-jetifier",
    "ios": "react-native run-ios --no-jetifier",
    "start": "react-native start"
  },
  "dependencies": {
    "@emotion/react": "^11.11.4",
    "@emotion/styled": "^11.11.0",
    "@react-navigation/drawer": "^6.6.15",
    "@react-navigation/native": "^6.1.17",
    "@react-navigation/stack": "^6.3.29",
    "@reduxjs/toolkit": "^1.9.5",
    "@rneui/base": "^4.0.0-rc.8",
    "@rneui/themed": "^4.0.0-rc.8",
    "axios": "^1.6.8",
    "dayjs": "^1.11.10",
    "react": "17.0.2",
    "react-native": "0.68.2",
    "react-native-bluetooth-escpos-printer": "git+https://github.com/hudoro-solusi-digital/react-native-bluetooth-escpos-printer",
    "react-native-eject": "^0.2.0",
    "react-native-element-dropdown": "^2.10.4",
    "react-native-gesture-handler": "^2.15.0",
    "react-native-permissions": "^3.3.1",
    "react-native-reanimated": "^3.8.1",
    "react-native-safe-area-context": "^4.9.0",
    "react-native-screens": "3.25.0",
    "react-native-sqlite-storage": "^6.0.1",
    "react-native-toast-notifications": "^3.4.0",
    "react-native-vector-icons": "^10.0.3",
    "react-redux": "^8.1.1"
  },
  "devDependencies": {
    "@babel/core": "^7.18.5",
    "@babel/runtime": "^7.18.3",
    "@react-native-community/eslint-config": "^3.0.2",
    "eslint": "^8.18.0",
    "metro-react-native-babel-preset": "^0.71.1"
  }
}