0

我正在尝试从 react-native 调用本机方法。但它抛出错误。在下面找到详细信息。基本上我正在尝试使用兄弟 sdk 从 react native 打印。它的工作模拟器很好,但很少有真正的设备工作正常。但不适用于三星 a70 设备。请帮我解决这个问题。提前致谢

const PrintPreviewLabel = (props) => { 

  const saveAsImage = useCallback((viewShotRef, index) => {
    captureRef(viewShotRef, {
      quality: 1,
      width: 350,
      height: 350,
      format: 'jpg',
    }).then(
      uri => {
        const splitted = uri.split('/');
        RNPrinterModule.printImageUsingWifi(
          ipAddress,
          splitted[splitted.length - 1],
          printFrom == 'search' ? parseInt(noCopyValue[index] || 1) :   printData[index]?.numberOfCopies,
          (message: { error: string, success: string}) => {
            setShowLoader(false);
            if (message && message.error) {
              setErrorModal(true);
            }
          },
        );
      },
      error => {
        console.log('Image saved failed', error);
      },
    );

  }, [ipAddress, noCopyValue, printData, printFrom]);
  
  return (<View>some rendering</View>);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

下面的代码是本机模块代码

    @ReactMethod
    void printImageUsingWifi(String ipAddress, String path, Integer copies, Callback callback) {
        try {
            Context context = getReactApplicationContext();
            Channel channel = Channel.newWifiChannel(ipAddress);
            WritableMap map = new WritableNativeMap();

            PrinterDriverGenerateResult result = PrinterDriverGenerator.openChannel(channel);
            if (result.getError().getCode() != OpenChannelError.ErrorCode.NoError) {
                Log.e("Printer", "Error - Open Channel: " + result.getError().getCode());
                map.putString("error", String.valueOf(result.getError().getCode()));
                callback.invoke(map);
                return;
            }

            File file = new File(context.getCacheDir().toString(), path);

            PrinterDriver printerDriver = result.getDriver();
            QLPrintSettings printSettings = new QLPrintSettings(PrinterModel.QL_820NWB);

            printSettings.setLabelSize(QLPrintSettings.LabelSize.RollW62RB);
            printSettings.setAutoCut(true);
            printSettings.setWorkPath(context.getCacheDir().toString());
//            printSettings.setHalftone(PrintImageSettings.Halftone.Threshold);
//            printSettings.setHalftoneThreshold(255);
            printSettings.setNumCopies(copies);

            PrintError printError = printerDriver.printImage(file.toString(), printSettings);

            if (printError.getCode() != PrintError.ErrorCode.NoError) {
                Log.d("Printer", "Error - Print Image: " + printError.getCode());
                map.putString("error", String.valueOf(printError.getCode()));
                callback.invoke(map);
            }
            else {
                Log.d("Printer", "Success - Print Image");
                map.putString("success", "true");
                callback.invoke(map);
            }

            printerDriver.closeChannel();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

错误是

Fatal Exception: java.lang.RuntimeException
Could not invoke RNPrinterModule.printImageUsingWifi
com.facebook.react.bridge.JavaMethodWrapper.invoke (JavaMethodWrapper.java:383)
com.facebook.react.bridge.JavaModuleWrapper.invoke (JavaModuleWrapper.java:151)
com.facebook.react.bridge.queue.NativeRunnable.run (NativeRunnable.java)
android.os.Handler.handleCallback (Handler.java:938)
android.os.Handler.dispatchMessage (Handler.java:99)
com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage (MessageQueueThreadHandler.java:27)
android.os.Looper.loop (Looper.java:246)
com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run (MessageQueueThreadImpl.java:226)
java.lang.Thread.run (Thread.java:923)


Caused by java.lang.reflect.InvocationTargetException
java.lang.reflect.Method.invoke (Method.java)
com.facebook.react.bridge.JavaMethodWrapper.invoke (JavaMethodWrapper.java:372)
com.facebook.react.bridge.JavaModuleWrapper.invoke (JavaModuleWrapper.java:151)
com.facebook.react.bridge.queue.NativeRunnable.run (NativeRunnable.java)
android.os.Handler.handleCallback (Handler.java:938)
android.os.Handler.dispatchMessage (Handler.java:99)
com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage (MessageQueueThreadHandler.java:27)
android.os.Looper.loop (Looper.java:246)
com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run (MessageQueueThreadImpl.java:226)
java.lang.Thread.run (Thread.java:923)


Caused by java.lang.NoClassDefFoundError
Failed resolution of: Lkotlin/jvm/internal/Intrinsics;
com.brother.sdk.lmprinter.GeneralPrintSettings.<init> (Unknown Source:2)
com.brother.sdk.lmprinter.V3PrinterService.setPrintSettings (V3PrinterService.java:277)
com.brother.sdk.lmprinter.V3PrinterService.setPrintSettingsAndCallPrintImage (V3PrinterService.java:70)
com.brother.sdk.lmprinter.V3PrinterService.printImage (V3PrinterService.java:44)
com.brother.sdk.lmprinter.PrinterDriver.printImage (PrinterDriver.java:29)
vn.telio.ims.RNPrinterModule.printImageUsingWifi (RNPrinterModule.java:83)
java.lang.reflect.Method.invoke (Method.java)
com.facebook.react.bridge.JavaMethodWrapper.invoke (JavaMethodWrapper.java:372)
com.facebook.react.bridge.JavaModuleWrapper.invoke (JavaModuleWrapper.java:151)
com.facebook.react.bridge.queue.NativeRunnable.run (NativeRunnable.java)
android.os.Handler.handleCallback (Handler.java:938)
android.os.Handler.dispatchMessage (Handler.java:99)
com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage (MessageQueueThreadHandler.java:27)
android.os.Looper.loop (Looper.java:246)
com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run (MessageQueueThreadImpl.java:226)
java.lang.Thread.run (Thread.java:923)


Caused by java.lang.ClassNotFoundException
kotlin.jvm.internal.Intrinsics
com.brother.sdk.lmprinter.GeneralPrintSettings.<init> (Unknown Source:2)
com.brother.sdk.lmprinter.V3PrinterService.setPrintSettings (V3PrinterService.java:277)
com.brother.sdk.lmprinter.V3PrinterService.setPrintSettingsAndCallPrintImage (V3PrinterService.java:70)
com.brother.sdk.lmprinter.V3PrinterService.printImage (V3PrinterService.java:44)
com.brother.sdk.lmprinter.PrinterDriver.printImage (PrinterDriver.java:29)
vn.telio.ims.RNPrinterModule.printImageUsingWifi (RNPrinterModule.java:83)
java.lang.reflect.Method.invoke (Method.java)
com.facebook.react.bridge.JavaMethodWrapper.invoke (JavaMethodWrapper.java:372)
com.facebook.react.bridge.JavaModuleWrapper.invoke (JavaModuleWrapper.java:151)
com.facebook.react.bridge.queue.NativeRunnable.run (NativeRunnable.java)
android.os.Handler.handleCallback (Handler.java:938)
android.os.Handler.dispatchMessage (Handler.java:99)
com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage (MessageQueueThreadHandler.java:27)
android.os.Looper.loop (Looper.java:246)
com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run (MessageQueueThreadImpl.java:226)
java.lang.Thread.run (Thread.java:923)

4

1 回答 1

0

如果问题仅导致 android 11 及更高版本。然后,这可能是由于行为变化:面向 Android 11的应用程序。

只有在我们的 AndroidManifest.xml 中添加以下代码后,某些功能(例如:语音识别、TTS 等...)才能在 android 11 设备上运行

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
        tools:node="remove" tools:ignore="QueryAllPackagesPermission" />

也在android:exported="true"里面<activity>

于 2022-02-17T06:08:32.327 回答