无法读取 null 的属性“ACTION_SOFTSCANTRIGGER”-react-native-datawedge-intents - React-Native Expo

问题描述 投票:0回答:1

我尝试使用 rn-datawedge-intents 库,但收到此错误 - 无法读取 null 的属性“ACTION_SOFTSCANTRIGGER”。我也尝试过使用其他库,但没有任何效果,这是唯一有一些示例的库,但我无法弄清楚。

这是链接:https://www.npmjs.com/package/react-native-datawedge-intents

我这里有两组基于给定示例的代码 两者都有同样的问题。

A - 基于文档的旧代码

import {useEffect} from "react";
import {DeviceEventEmitter} from "react-native";
import DataWedgeIntents from "react-native-datawedge-intents";

export const useBarcodeScanner = () =\> {
useEffect(() =\> {
// Define the action for DataWedge Intents
const action = "com.zebra.dwintents.ACTION";

    // Register a receiver for the barcode scans with the appropriate action
    DataWedgeIntents.registerReceiver(acti
    // Declare a handler for barcode scans
    const scanHandler = (deviceEvent: any) => {on, "");
    
      const barcodeValue = deviceEvent.data; // Assuming 'data' contains the barcode value
      console.log("Scanned barcode:", barcodeValue);
      // Perform further actions with the barcode value here
    };
    
    // Listen for scan events sent from the module
    DeviceEventEmitter.addListener("barcode_scan", scanHandler);
    
    // Cleanup function
    return () => {
      // Remove the listener and unregister receiver to prevent memory leaks
      DeviceEventEmitter.removeAllListeners("barcode_scan");
      DataWedgeIntents.unregisterReceiver();
    };

}, \[\]);

const handleScanButtonPress = () =\> {
// Initiates the scan when the button is pressed
DataWedgeIntents.sendIntent(
DataWedgeIntents.ACTION_SOFTSCANTRIGGER,
DataWedgeIntents.START_SCANNING
).catch((error: any) =\> {
console.error("Failed to initiate scan:", error);
});
};

return {handleScanButtonPress};
};

B - 稍后根据文档使用

import {useState, useEffect} from "react";
import {DeviceEventEmitter} from "react-native";
import DataWedgeIntents from "react-native-datawedge-intents";

interface Scans {
  data: any;
  decoder: any;
  timeAtDecode: any;
}

const useDataWedgeIntegration = () => {
  const [state, setState] = useState({
    lastApiVisible: false,
    lastApiText: "Messages from DataWedge will go here",
    checkBoxesDisabled: true,
    scanButtonVisible: false,
    dwVersionText:
      "Pre 6.3.  Please create and configure profile manually.  See the ReadMe for more details",
    activeProfileText: "Requires DataWedge 6.3+",
    enumeratedScannersText: "Requires DataWedge 6.3+",
  });
  const [sendCommandResult, setCommandResult] = useState("false");
  const [scanBarcode, setScanBarcode] = useState<Scans[]>([]);

  useEffect(() => {
    const deviceEmitterSubscription = DeviceEventEmitter.addListener(
      "datawedge_broadcast_intent",
      broadcastReceiver
    );
    registerBroadcastReceiver();
    determineVersion();

    return () => {
      deviceEmitterSubscription.remove();
    };
  }, []);

  const sendCommand = (extraName: string, extraValue: any) => {
    console.log(
      "Sending Command: " + extraName + ", " + JSON.stringify(extraValue)
    );
    var broadcastExtras: {[key: string]: any} = {};
    broadcastExtras[extraName] = extraValue;
    broadcastExtras["SEND_RESULT"] = sendCommandResult;
    DataWedgeIntents.sendBroadcastWithExtras({
      action: "com.symbol.datawedge.api.ACTION",
      extras: broadcastExtras,
    });
  };

  const determineVersion = () => {
    sendCommand("com.symbol.datawedge.api.GET_VERSION_INFO", "");
  };

  const registerBroadcastReceiver = () => {
    DataWedgeIntents.registerBroadcastReceiver({
      filterActions: [
        "com.zebra.reactnativedemo.ACTION",
        "com.symbol.datawedge.api.RESULT_ACTION",
      ],
      filterCategories: ["android.intent.category.DEFAULT"],
    });
  };

  const broadcastReceiver = (intent: any) => {
    // Broadcast received
    console.log("Received Intent: " + JSON.stringify(intent));
    if (intent.hasOwnProperty("RESULT_INFO")) {
      var commandResult =
        intent.RESULT +
        " (" +
        intent.COMMAND.substring(
          intent.COMMAND.lastIndexOf(".") + 1,
          intent.COMMAND.length
        ) +
        ")"; // + JSON.stringify(intent.RESULT_INFO);
      commandReceived(commandResult.toLowerCase());
    }

    if (
      intent.hasOwnProperty("com.symbol.datawedge.api.RESULT_GET_VERSION_INFO")
    ) {
      // The version has been returned (DW 6.3 or higher). Includes the DW version along with other subsystem versions e.g MX
      var versionInfo =
        intent["com.symbol.datawedge.api.RESULT_GET_VERSION_INFO"];
      console.log("Version Info: " + JSON.stringify(versionInfo));
      var datawedgeVersion = versionInfo["DATAWEDGE"];
      console.log("Datawedge version: " + datawedgeVersion);

      // Fire events sequentially so the application can gracefully degrade the functionality available on earlier DW versions
      if (datawedgeVersion >= "06.3") datawedge63();
      if (datawedgeVersion >= "06.4") datawedge64();
      if (datawedgeVersion >= "06.5") datawedge65();

      setState((state) => ({...state}));
    } else if (
      intent.hasOwnProperty(
        "com.symbol.datawedge.api.RESULT_ENUMERATE_SCANNERS"
      )
    ) {
      // Return from our request to enumerate the available scanners
      var enumeratedScannersObj =
        intent["com.symbol.datawedge.api.RESULT_ENUMERATE_SCANNERS"];
      enumerateScanners(enumeratedScannersObj);
    } else if (
      intent.hasOwnProperty(
        "com.symbol.datawedge.api.RESULT_GET_ACTIVE_PROFILE"
      )
    ) {
      // Return from our request to obtain the active profile
      var activeProfileObj =
        intent["com.symbol.datawedge.api.RESULT_GET_ACTIVE_PROFILE"];
      activeProfile(activeProfileObj);
    } else if (!intent.hasOwnProperty("RESULT_INFO")) {
      // A barcode has been scanned
      barcodeScanned(intent, new Date().toLocaleString());
    }
  };

  const datawedge63 = () => {
    console.log("Datawedge 6.3 APIs are available");
    // Create a profile for our application
    sendCommand(
      "com.symbol.datawedge.api.CREATE_PROFILE",
      "ZebraReactNativeDemo"
    );

    setState((state) => ({
      ...state,
      dwVersionText:
        "6.3.  Please configure profile manually.  See ReadMe for more details.",
    }));

    // Although we created the profile we can only configure it with DW 6.4.
    sendCommand("com.symbol.datawedge.api.GET_ACTIVE_PROFILE", "");

    // Enumerate the available scanners on the device
    sendCommand("com.symbol.datawedge.api.ENUMERATE_SCANNERS", "");

    // Functionality of the scan button is available
    setState((state) => ({...state, scanButtonVisible: true}));
  };

  const datawedge64 = () => {
    console.log("Datawedge 6.4 APIs are available");

    // Documentation states the ability to set a profile config is only available from DW 6.4.
    // For our purposes, this includes setting the decoders and configuring the associated app / output params of the profile.
    setState((state) => ({
      ...state,
      dwVersionText: "6.4.",
      // dwVersionTextStyle: styles.itemText,
      checkBoxesDisabled: false,
    }));

    // Configure the created profile (associated app and keyboard plugin)
    var profileConfig = {
      PROFILE_NAME: "ZebraReactNativeDemo",
      PROFILE_ENABLED: "true",
      CONFIG_MODE: "UPDATE",
      PLUGIN_CONFIG: {
        PLUGIN_NAME: "BARCODE",
        RESET_CONFIG: "true",
        PARAM_LIST: {},
      },
      APP_LIST: [
        {
          PACKAGE_NAME: "com.datawedgereactnative.demo",
          ACTIVITY_LIST: ["*"],
        },
      ],
    };
    sendCommand("com.symbol.datawedge.api.SET_CONFIG", profileConfig);

    // Configure the created profile (intent plugin)
    var profileConfig2 = {
      PROFILE_NAME: "ZebraReactNativeDemo",
      PROFILE_ENABLED: "true",
      CONFIG_MODE: "UPDATE",
      PLUGIN_CONFIG: {
        PLUGIN_NAME: "INTENT",
        RESET_CONFIG: "true",
        PARAM_LIST: {
          intent_output_enabled: "true",
          intent_action: "com.zebra.reactnativedemo.ACTION",
          intent_delivery: "2",
        },
      },
    };
    sendCommand("com.symbol.datawedge.api.SET_CONFIG", profileConfig2);

    // Give some time for the profile to settle then query its value
    setTimeout(() => {
      sendCommand("com.symbol.datawedge.api.GET_ACTIVE_PROFILE", "");
    }, 1000);
  };

  const datawedge65 = () => {
    console.log("Datawedge 6.5 APIs are available");

    setState((state) => ({
      ...state,
      dwVersionText: "6.5 or higher.",
      lastApiVisible: true,
    }));
    setCommandResult("true");
  };

  const commandReceived = (commandText: string) => {
    setState((state) => ({...state, lastApiText: commandText}));
  };

  const enumerateScanners = (enumeratedScanners: any) => {
    var humanReadableScannerList = "";
    for (var i = 0; i < enumeratedScanners.length; i++) {
      console.log(
        "Scanner found: name= " +
          enumeratedScanners[i].SCANNER_NAME +
          ", id=" +
          enumeratedScanners[i].SCANNER_INDEX +
          ", connected=" +
          enumeratedScanners[i].SCANNER_CONNECTION_STATE
      );
      humanReadableScannerList += enumeratedScanners[i].SCANNER_NAME;
      if (i < enumeratedScanners.length - 1) humanReadableScannerList += ", ";
    }
    setState((state) => ({
      ...state,
      enumeratedScannersText: humanReadableScannerList,
    }));
  };

  const activeProfile = (theActiveProfile: string) => {
    setState((state) => ({...state, activeProfileText: theActiveProfile}));
  };

  const barcodeScanned = (scanData: any, timeOfScan: any) => {
    var scannedData = scanData["com.symbol.datawedge.data_string"];
    var scannedType = scanData["com.symbol.datawedge.label_type"];
    console.log("Scan: " + scannedData);
    setScanBarcode([
      {
        data: scannedData,
        decoder: scannedType,
        timeAtDecode: timeOfScan,
      },
    ]);
  };

  const _onPressScanButton = () => {
    sendCommand(
      "com.symbol.datawedge.api.SOFT_SCAN_TRIGGER",
      "TOGGLE_SCANNING"
    );
  };
  return {
    _onPressScanButton,
    scanBarcode,
    barcodeScanned,
  };
};

export default useDataWedgeIntegration;


react-native expo datawedge
1个回答
0
投票

你能在某个时候解决这个问题吗?

© www.soinside.com 2019 - 2024. All rights reserved.