1

How do I really call a handler inside a method in React-Native Java Module ? I am trying to use Handler in React native Java Module but it is throwing error as

The printData is calling from React-native through its NativeModule.

Seems the Native bridge is working perfectly but only getting the error when including the Handler

Here is my module Class `

package com.mosabeeapp;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.printer.sdk.PrinterConstants;
import com.printer.sdk.PrinterInstance;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import java.io.File;

public class CalendarModule extends ReactContextBaseJavaModule {
    public PrinterInstance myPrinter;

    private String devicesName;
    private String devicesAddress;
    private Integer baudrate;
    private Boolean isConnected;

    private Handler mHandler = new Handler() {
        @SuppressLint("ShowToast")
        @Override
        public void handleMessage(Message msg) {

            System.out.println("@@@@@@@@@@@@" + msg.what);
            switch (msg.what) {
                case PrinterConstants.Connect.SUCCESS:
                    isConnected = true;
                    System.out.println("isConnected status:::;" + isConnected);
                    break;
                case PrinterConstants.Connect.FAILED:
                    isConnected = false;
                    System.out.println("Connection failed");
                    break;
                case PrinterConstants.Connect.CLOSED:
                    isConnected = false;
                    System.out.println("Connection closed");
                    break;
                case PrinterConstants.Connect.NODEVICE:
                    System.out.println("There is no device");
                    isConnected = false;
                    break;
                case 0:
                    System.out.println("0");
                    break;
                case -1:
                    System.out.println("-1");

                    break;
                case -2:
                    System.out.println("-2");

                    break;
                case -3:
                    System.out.println("-3");

                    break;
                default:
                    break;
            }
        }
    };


    CalendarModule(ReactApplicationContext context) {
       super(context);
   }

    @Override
    public String getName() {
    return "CalendarModule";
    }


    @ReactMethod
    public void printData(String data) {

        System.out.println("----------buttonPrint--------");
        devicesName = "Serial device";
        devicesAddress = "/dev/ttyMT2";
        String com_baudrate = "115200";
        baudrate = Integer.parseInt(com_baudrate);

        myPrinter = PrinterInstance.getPrinterInstance(new File(devicesAddress), baudrate,0,8, mHandler);

        System.out.println("myPrinter.getCurrentStatus()-" + myPrinter.getCurrentStatus());
        boolean b = myPrinter.openConnection();
        System.out.println("-----------" + b);
        if (b && myPrinter != null) {
            System.out.println("Printer is ready");
        } else {
            System.out.println("Can not made a connection to the device");
        }

    }
}

`

ppegu
  • 362
  • 2
  • 9
  • 22

0 Answers0