无法在未调用looper.prepare()的线程[create\u react\u context,5,main]内创建处理程序

px9o7tmv  于 2021-06-26  发布在  Java
关注(0)|答案(0)|浏览(169)

如何在react native java模块的方法中真正调用处理程序?我试图在react原生java模块中使用处理程序,但它抛出了一个错误
printdata通过其nativemodule从react native调用。
本机网桥似乎工作得很好,但只有在包含处理程序时才会出错
这是我的模块课`

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");
        }

    }
}

`

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题