HeligPfleigh / react-native-thermal-receipt-printer

A RN library for thermal printer
176 stars 103 forks source link

Can't Connect to NetPrinter iOS #176

Closed thecodecafe closed 6 months ago

thecodecafe commented 6 months ago

When you attempt connecting with the NetPrinter it never connects, you do not get the connected printer's info (See connect code here), you instead get a string returned saying that it's "Connecting to Printer [the host IP address]". Then when I attempt printing I get an error saying the "Can't connect to printer".

What code are you running and what is happening?

NetPrinter.connect(ipAddres, port)

Expected Behavior

I expect the printing functionality to work since the PrinterSDK used in the RNNetPrinter.m file did not fail to connect.

Also, the return type for the connectPrinter method should be an object with the printer's host, port, and name as the type suggests and not a string saying that it's connecting.

Although I'm curious has the NetPrinter ever worked in the past?

How to reproduce

  1. Run the following code NetPrinter.init(). This returns undefined
  2. Then run the following code const res = await NetPrinter.connect(ipAddres, port); returns a string saying Connecting to printer [host IP address].
  3. Then try to print a text with NetPrinter.printText("Hello"). Logs a warning saying Can't connect to printer.

Your Environment

software version
iOS 16
react-native 0.73.6
node 21.5.0
yarn 1.22.19
thecodecafe commented 6 months ago

I've been able to detect what's causing the issue.

The connected_ip attribute on the RNNetPrinter class does not reflect the change; even when you set its value to the host IP address it does not seem to update.

I know zilch about writing Objective-C so I'm not going to be able to send a good PR to fix that, I have however patched the library on my project to exclude that piece of code. I patched the library in the following way:

  1. Install patch-package.
  2. Comment out the code that checks if connected_ip is set before printing.
  3. Run the patch command to patch the library.
  4. Run yarn or npm install to be sure the library is patched.

Note: you will have to monitor on your own end to know when the printer has been connected to by just maintaining a variable you set to true or false since the patch comments out the code that does that for you from within the library.