HeligPfleigh / react-native-thermal-receipt-printer

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

Error when connect printer on android, ios connect normal #77

Closed hoanganhnh2009 closed 2 years ago

hoanganhnh2009 commented 3 years ago

239675292_4840639742616912_2127379729715382026_n

hoanganhnh2009 commented 3 years ago

NetPrinter.connectPrinter( selectedPrinter?.host || '', selectedPrinter?.port || 9100, );

hoanganhnh2009 commented 2 years ago

Can you help me?

nguyen-vo commented 2 years ago

your host is empty. That might cause the error. check seletedPrinter before passing it

hoanganhnh2009 commented 2 years ago

your host is empty. That might cause the error. check seletedPrinter before passing it

Tks but i used useState init image

nguyen-vo commented 2 years ago

I would suggest checking the selectedPrinter object. Perhaps, you can console.log it to see whether you have it before you use it. The error said you are passing a null object to the function. I would first remove the selectedPrinter?.host || "", and try it with the actual IP address " 192.168.43.100". See if it works. Also I would not use selectedPrinter?.host. Rather I would check whether I actually have host and port before passing it. If I don't, I wont call the connectPrinter function

hoanganhnh2009 commented 2 years ago

I would suggest checking the selectedPrinter object. Perhaps, you can console.log it to see whether you have it before you use it. The error said you are passing a null object to the function. I would first remove the selectedPrinter?.host || "", and try it with the actual IP address " 192.168.43.100". See if it works. Also I would not use selectedPrinter?.host. Rather I would check whether I actually have host and port before passing it. If I don't, I wont call the connectPrinter function

I see what you mean, I did console log and make sure host has value

nguyen-vo commented 2 years ago

Could you share your code where you use the NetPrinter module

hoanganhnh2009 commented 2 years ago
import React, { useState } from 'react';
import { StyleSheet, View, Button } from 'react-native';
import { NetPrinter } from 'react-native-thermal-receipt-printer';
import Encoder from 'esc-pos-encoder';

// - (void)sendHex:(NSString*)hex;

import { NativeModules } from 'react-native';
const RNNetPrinter = NativeModules.RNNetPrinter;
import { Buffer } from 'buffer';

export default function App() {
  const [selectedPrinter] = useState({
    device_name: 'Net Printer',
    host: '192.168.43.100',
    port: 9100,
    printerType: 'net',
  });

  const handleConnectSelectedPrinter = () => {
    if (!selectedPrinter) return;
    const connect = async () => {
      try {
        let status = await NetPrinter.connectPrinter('192.168.43.100', 9100);
        console.log('connect -> status', status);
      } catch (err) {
        console.warn(err);
      }
    };
    connect();
  };

  const handlePrint = async () => {
    const encoder = new Encoder();
    let _encoder = encoder
      .initialize()
      .align('center')
      .line('CAM ON QUY KHACH, HEN GAP LAI')
      .line('Website: www.vt.vn')
      // qr code
      .qrcode('https://nielsleenheer.com')
      .encode();

    let base64String = Buffer.from(_encoder).toString('base64'); // android
    RNNetPrinter.printRawData(base64String, function(error) {
      return console.warn(error);
    });
  };

  return (
    <View style={styles.container}>
      <Button
        disabled={!selectedPrinter?.device_name}
        title="Connect"
        onPress={handleConnectSelectedPrinter}
      />
      <Button
        disabled={!selectedPrinter?.device_name}
        title="Print sample"
        onPress={handlePrint}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    padding: 16,
  },
  section: {
    flex: 1,
  },
  rowDirection: {
    flexDirection: 'row',
  },
});
hoanganhnh2009 commented 2 years ago

Could you share your code where you use the NetPrinter module

This is my code

nguyen-vo commented 2 years ago

I will change these line

Since you already import NetPrinter from “react-native-thermal-receipt-printer”, you don't need those 2 line of code.

Secondly, you have not init the NetPrinter object. I will add this

useEffect(()=>{ NetPrinter.init() },[])

hoanganhnh2009 commented 2 years ago

I will change these line

  • import { NativeModules } from 'react-native'; const RNNetPrinter = NativeModules.RNNetPrinter;

Since you already import NetPrinter from “react-native-thermal-receipt-printer”, you don't need those 2 line of code.

Secondly, you have not init the NetPrinter object. I will add this

useEffect(()=>{ NetPrinter.init() },[])

  1. I import NativeModules.RNNetPrinter to use method native printRawData
  2. I added useEffect(()=>{ NetPrinter.init() },[]) but error still occurs
nguyen-vo commented 2 years ago

Okay, since you are using printRaw with RNNetPrinter, initialize with NetPrinter does no solve the problem. The problem is in the Native side the RNNetPrinter has not been initialize yet. So in the useEffect try RNNetPrinter.init(). I am suggesting that you look in the src/index.ts to see how they use the init function

hoanganhnh2009 commented 2 years ago

I was able to print. Thankyou very much