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

Net Printer init of null #175

Open
GalaxyTea1 opened this issue Apr 5, 2024 · 5 comments
Open

Net Printer init of null #175

GalaxyTea1 opened this issue Apr 5, 2024 · 5 comments
Labels
question Further information is requested

Comments

@GalaxyTea1
Copy link

I am using Expo and trying to connect to a POS machine on the same LAN network as my device (I have successfully done so using a PC). I don't understand what's happening here, I get err [TypeError: Cannot read property 'init' of null.
I would appreciate some help!
My code:

import { View, Text, TouchableOpacity } from "react-native";
import {
  USBPrinter,
  NetPrinter,
  BLEPrinter,
} from "react-native-thermal-receipt-printer";

const PrinterComponent = () => {
  const [printers, setPrinters] = useState([]);
  const [currentPrinter, setCurrentPrinter] = useState();

  useEffect(() => {
    NetPrinter.init()
      .then(() => {
        setPrinters({ host: "192.168.1.12", port: 9100 });
      })
      .catch((error) => console.warn(error));
  }, []);

  const connectPrinter = (host, port) => {
    NetPrinter.connectPrinter(host, port).then(
      (printer) => setCurrentPrinter(printer),
      (error) => console.warn(error),
    );
  };

  const printTextTest = () => {
    if (currentPrinter) {
      NetPrinter.printText("<C>sample text</C>\n");
    }
  };

  return (
    <View>
      {printers.map((printer, index) => (
        <TouchableOpacity
          key={index}
          onPress={() => connectPrinter(printer.host, printer.port)}
        >
          <Text>{`device_name: ${printer.device_name}, host: ${printer.host}, port: ${printer.port}`}</Text>
        </TouchableOpacity>
      ))}
      <TouchableOpacity onPress={printTextTest}>
        <Text>Print Text</Text>
      </TouchableOpacity>
    </View>
  );
};

export default PrinterComponent;
@GalaxyTea1 GalaxyTea1 added the question Further information is requested label Apr 5, 2024
@thecodecafe
Copy link

Hey @GalaxyTea1 if this is an issue on iOS ensure pod-install ran successfully.

Also if you're using Expo then you might need to dig further on how to install libraries that introduce native code correctly.

@GalaxyTea1
Copy link
Author

Hey @GalaxyTea1 if this is an issue on iOS ensure pod-install ran successfully.

Also if you're using Expo then you might need to dig further on how to install libraries that introduce native code correctly.

I use android with Expo run dev, maybe lib does not support it.

@thecodecafe
Copy link

Most likely the case @GalaxyTea1, to test the hypothesis, try booting a fresh react native project with react-native cli and see if the lib works as expected. If it does then we will have to jump ship.

@petermoreira18
Copy link

petermoreira18 commented May 14, 2024

ould appreciate s

Hi @GalaxyTea1 , I had the same issue and I'm also using expo run dev.
Currently it's working but there is something that i cannot explain yet.

Notes:

  • I had to create a development build with "eas".
  • The error " [TypeError: Cannot read property 'init' of null." was not related to the "NetPrinter" module but to the "RNNetPrinter" native module wich was Null.
  • Working with expo-doctor, updating packages and making sure that the build tools, compile sdk and target sdk and kotlin versions were correct helped.
  • Have the correct permissions to access LAN
  • I had to use NetPrinterEventEmitter.addListener to search the Network printer and only connect if the device was found. The NetPrinter.getDeviceList only triggered the events to search the network.
  • I still could not make it work on a production version (it crashes the App)

@GalaxyTea1
Copy link
Author

It works with Expo dev, I haven't tried it with the product version. But I was advised to use react native instead of expo, so I changed @petermoreira18

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

No branches or pull requests

3 participants