Surile / react-native-sunmi-printer

商米内置打印机
MIT License
44 stars 28 forks source link

Problem with K2 inbuilt printer #24

Closed amcjml closed 9 months ago

amcjml commented 1 year ago

The package is working fine with Sunmi T2s model inbuilt printer.

I tested it with K2 printer but I get a ticket with a different font size and the align function is not working.

Here is the example of tickets for reference.

Can you help me please to identify the source of the problem.

Thank you.

Surile commented 1 year ago

Is the size of the paper the same?

amcjml commented 1 year ago

Yes, the size of paper the same.

amcjml commented 1 year ago

You can find the AIDL for K2 here for reference.

Surile commented 1 year ago

Maybe the resolution is different,I suggest you consult the customer service of sunmi。

amcjml commented 1 year ago

Ok thank you.

I see in the AIDL file for K2 the functions definition for align and font size are different: setAlignMode() and setFontZoom()

is this can be help?

I already asked the customer service of Sunmi and they send me that AIDL file. Anyway, I will ask them again. If I found a solution I will put it here.

davimateus1 commented 9 months ago

Hello, I have a machine of the same model, K2. If you have already resolved the problem, what step by step can I follow?

"react-native": "0.72.4"

  import SunmiPrinter, { AlignValue } from "@heasy/react-native-sunmi-printer";

  const printPassword = ({ cpf, code }: { cpf: string; code: string }) => {
    try {
      SunmiPrinter.setAlignment(AlignValue.CENTER);
      SunmiPrinter.printTextWithFont("Order code:\n", "gh", 18);
      SunmiPrinter.lineWrap(3);

      SunmiPrinter.setAlignment(AlignValue.CENTER);
      SunmiPrinter.printTextWithFont(code, "gh", 24);
      SunmiPrinter.lineWrap(3);

      SunmiPrinter.setAlignment(AlignValue.CENTER);
      SunmiPrinter.printerText("================================");
      SunmiPrinter.lineWrap(3);

      SunmiPrinter.setAlignment(AlignValue.CENTER);
      SunmiPrinter.printTextWithFont("CPF:\n", "gh", 18);
      SunmiPrinter.lineWrap(3);

      SunmiPrinter.setAlignment(AlignValue.CENTER);
      SunmiPrinter.printTextWithFont(cpf, "gh", 24);
    } catch (error) {
      alert("Não foi possível encontrar a impressora");
    }
  };

Is there any error in this code?

Error:

jsEngine: "hermes"
message: "Cannot read property 'setAlignment' of null"
stack: "TypeError: Cannot read property 'setAlignment' of null\n    at printPassword (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:216845:54)\n    at anonymous (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:216865:22)\n    at map (native)\n    at anonymous (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:216864:19)\n    at commitHookEffectListMount (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:81785:38)\n    at commitPassiveMountOnFiber (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:82986:44)\n    at commitPassiveMountEffects_complete (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:82958:40)\n    at commitPassiveMountEffects_begin (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:82948:47)\n    at commitPassiveMountEffects (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:82938:40)\n    at flushPassiveEffectsImpl (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:84614:34)\n    at flushPassiveEffects (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:84569:43)\n    at performSyncWorkOnRoot (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:83859:28)\n    at flushSyncCallbacks (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:73058:36)\n    at flushSyncCallbacksOnlyInLegacyMode (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:73040:29)\n    at batchedUpdates$1 (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:83912:47)\n    at batchedUpdates (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:70313:36)\n    at _receiveRootNodeIDEvent (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:70437:23)\n    at receiveTouches (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:70526:34)\n    at apply (native)\n    at __callFunction (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:2896:36)\n    at anonymous (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:2616:31)\n    at __guard (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:2833:15)\n    at callFunctionReturnFlushedQueue (http://10.131.4.127:8081/node_modules%5Cexpo-router%5Centry.bundle//&platform=android&dev=true&hot=false&lazy=true:2615:21)"
amcjml commented 9 months ago

Hello, I created my custom method printCenteredText that uses printColumnsText:

const printCenteredText = async (text, isBold = false) => {
  await fontBold(isBold);
  await SunmiV2Printer.printColumnsText(["", text, ""], [1, 46, 1], [1, 1, 1]);
  await fontBold(false);
};

Hope this helps.

davimateus1 commented 9 months ago

Hello!

The problem that is happening is that my code cannot identify the Sunmi Printer K2 printer, what should I add to the React Native code so that the printer is identified correctly? I believe that if you do this, it should work.

davimateus1 commented 9 months ago

Where does the fontBold function come from?

amcjml commented 9 months ago

My problem was that setAlignment is not working on K2 so I wrote my own align function. You should ask Sunmi support about this problem: "My code cannot identify the Sunmi K2 printer".

fontBold function is a custom function I created my self. It's optional in your case:

import { Buffer } from "buffer";
const fontBold = async (active = true) => {
  var code = active ? [0x1b, 0x45, 0x1] : [0x1b, 0x45, 0x0];
  await sendRawData(code);
};
const sendRawData = async (code) => {
  var codeUint8 = new Uint8Array(code);
  var codeBase64 = BufferToBase64(codeUint8);
  await SunmiV2Printer.sendRAWData(codeBase64);
};
function BufferToBase64(buf) {
  var binstr = Array.prototype.map
    .call(buf, function (ch) {
      return String.fromCharCode(ch);
    })
    .join("");
  return Buffer.from(binstr, "utf8").toString("base64");
}
davimateus1 commented 9 months ago

Thank you!

This is my actual code:

  const printCenteredText = async (text: string, isBold = false) => {
    await fontBold(isBold);
    SunmiPrinter.printColumnsText(["", text, ""], [1, 46, 1], [1, 1, 1]);
    await fontBold(false);
  };

  const fontBold = async (active = true) => {
    var code = active ? [0x1b, 0x45, 0x1] : [0x1b, 0x45, 0x0];
    await sendRawData(code);
  };

  const sendRawData = async (code: Array<number>) => {
    var codeUint8 = new Uint8Array(code);
    var codeBase64 = BufferToBase64(codeUint8);
    SunmiPrinter.sendRAWData(codeBase64);
  };

  const BufferToBase64 = (buf: Uint8Array) => {
    var binstr = Array.prototype.map
      .call(buf, function (ch) {
        return String.fromCharCode(ch);
      })
      .join("");
    return Buffer.from(binstr, "utf8").toString("base64");
  };

  const printPassword = async ({
    cpf,
    code,
  }: {
    cpf: string;
    code: string;
  }) => {
    await printCenteredText("Order code:", true);
    await printCenteredText(code);

    await printCenteredText("================================");

    await printCenteredText("CPF:", true);
    await printCenteredText(cpf);
  };

and this error is being generated:

Possible Unhandled Promise Rejection (id: 2):
TypeError: Cannot read property 'sendRAWData' of null
TypeError: Cannot read property 'sendRAWData' of null

Possible Unhandled Promise Rejection (id: 2):
TypeError: Cannot read property 'printColumnsText' of null
TypeError: Cannot read property 'printColumnsText' of null

Would using the check with the question mark before calling the method ( SunmiPrinter.sendRAWData(codeBase64) ) work normally?

amcjml commented 9 months ago

Your problem is that SunmiPrinter is null. So, maybe the printer is not detected. Your issue is different of mine.

davimateus1 commented 9 months ago

hello, sorry to bother you again :/

Error:

"react-native": "0.72.4", "@heasy/react-native-sunmi-printer": "^1.7.0"

[stderr] * What went wrong
[stderr] Could not determine the dependencies of task ':heasy_react-native-sunmi-printer:mergeReleaseGeneratedProguardFiles'.
[stderr] > Could not determine the dependencies of null.
[stderr] > Could not create task ':heasy_react-native-sunmi-printer:compileReleaseJavaWithJavac'.
[stderr] > In order to compile Java 9+ source, please set compileSdkVersion to 30 or above
[stderr] * Try:
[stderr] > Run with --stacktrace option to get the stack trace
[stderr] > Run with --info or --debug option to get more log output
[stderr] > Run with --scan to get full insights.89[stderr] * Get more help at https://help.gradle.org
[stderr] BUILD FAILED in 4m 27s

I'm having this error when running the eas build -p android --profile preview command, how can I solve it? I've tried a few things I've researched but none of them work.

amcjml commented 9 months ago

No problem. Try compileSdkVersion 30 or 31 in your android/build.gradle file.

Here is my command I run to build release version:

cd android
/gradlew clean 
./gradlew bundleRelease --stacktrace 
./gradlew app:assembleRelease --stacktrace

PS: change/ to \ on Windows

davimateus1 commented 9 months ago

The android folder you say is the library itself located within the node modules, correct? Do I navigate to it and run these commands?

amcjml commented 9 months ago

I mean the android folder of your project. Just change compileSdkVersion to 30 or 31 in android/build.gradle then run your build command.

Never mind these commands if your build command works after the change.

davimateus1 commented 9 months ago

@amcjml I'm trying to use AIDL but without much success, Android Studio doesn't detect the file, can you tell me how I can solve it?

com.organization.myapp

import com.organization.myapp.ExtPrinterService does not exists

amcjml commented 9 months ago

Without using AIDL, did you test your app in a real K2 device with inbuit printer?

davimateus1 commented 9 months ago

This library doesn't work well for me, I used it but when I was going to create the android app it always gave an error, I tried several times and couldn't, do you have any step-by-step implementation or something like that? I was only able to get the printer to work using react-native-print :/

amcjml commented 9 months ago

Sorry @davimateus1, I don't understand what are you doing. When you have the first error, did you build your app and test it on a real K2 device with inbuit printer?