mzakk / chainway-sensors

React Native Module for Barcode Scanner and UHF Reader for Chainway c66
GNU General Public License v3.0
0 stars 0 forks source link

barcode initialization error #3

Closed kamarul-ariffin-github closed 3 weeks ago

kamarul-ariffin-github commented 1 month ago

i already follow your advice on previous issue. now i got this error when i want to initialize the barcode scanner: 10-31 22:08:53.407 15383 15413 E ReactNativeJS: 'Error in barcode initialization', [TypeError: undefined is not a function]

here is my code where this error occur:

if (!isInitialized) {
        if (typeof RNChainwaySensors.initializeBarcodeReader !== "function") {
          console.error("initializeBarcodeReader is not a function");
          return;
        }
        let result = await RNChainwaySensors.initializeBarcodeReader();
        console.log("initialize barcode reader result: ", result);
      }
    } catch (error) {
      console.error("Error in barcode initialization", error);
      console.error("Error details:", {
        name: error.name,
        message: error.message,
        stack: error.stack,
      });
    }
mzakk commented 1 month ago

post all your code in that file. Please use the code quoting button

kamarul-ariffin-github commented 1 month ago

here is my code

import MaterialCommunityIcons from "@expo/vector-icons/MaterialCommunityIcons";
import { Box, Text } from "@gluestack-ui/themed";
import RNChainwaySensors from "chainway-sensors";
import { useNavigation } from "expo-router";
import { useEffect, useState } from "react";
import KeyEvent from "react-native-keyevent";

export default function ScanRfid({ onRfidScanned }: { onRfidScanned: () => void }) {
  const navigation = useNavigation();
  const [lastKeyPressed, setLastKeyPressed] = useState<string>("");
  const [lastKeyCode, setLastKeyCode] = useState<number | null>(null);

  const initializeScanner = async () => {
    try {
      console.log("RNChainwaySensors methods:", Object.keys(RNChainwaySensors));

      if (typeof RNChainwaySensors.isBarcodeReaderInit !== "function") {
        console.error("isBarcodeReaderInit is not a function");
        return;
      }

      const isInitialized = await RNChainwaySensors.isBarcodeReaderInit();
      console.log("Is barcode reader initialized:", isInitialized);

      if (!isInitialized) {
        if (typeof RNChainwaySensors.initializeBarcodeReader !== "function") {
          console.error("initializeBarcodeReader is not a function");
          return;
        }
        let result = await RNChainwaySensors.initializeBarcodeReader();
        console.log("initialize barcode reader result: ", result);
      }
    } catch (error) {
      console.error("Error in barcode initialization", error);
      console.error("Error details:", {
        name: error.name,
        message: error.message,
        stack: error.stack,
      });
    }
  };

  const startScan = async () => {
    try {
      let result = await RNChainwaySensors.startBarcodeScan();
      console.log("start barcode scan result: ", result);
    } catch (error) {
      console.error("Erro", error);
    }
  };

  const stopScan = async () => {
    try {
      let result = await RNChainwaySensors.stopBarcodeScan();
      console.log("stop barcode scan result: ", result);
    } catch (error) {
      console.error("Error", error);
    }
  };

  useEffect(() => {
    navigation.setOptions({ headerShown: false });
    initializeScanner();

    console.log("before listener");
    KeyEvent.onKeyDownListener((keyEvent: any) => {
      console.log("keydown listener");
      if (!keyEvent) return;
      console.log("key down event pressed: ", keyEvent);
      setLastKeyPressed(keyEvent.pressedKey);
      setLastKeyCode(keyEvent.keyCode);

      if (keyEvent.keyCode === 293 || keyEvent.keyCode === 291) {
        console.log("start scan");
        startScan();
      }
    });

    KeyEvent.onKeyUpListener((keyEvent: any) => {
      console.log("keyuplistener");
      if (!keyEvent) return;
      console.log("key up event pressed: ", keyEvent);

      if (keyEvent.keyCode === 293 || keyEvent.keyCode === 291) {
        console.log("stop scan");
        stopScan();
      }
    });

    return () => {
      KeyEvent.removeKeyDownListener();
      KeyEvent.removeKeyUpListener();
    };
  }, [navigation, onRfidScanned]);

  return (
    <Box h={"100%"} w={"100%"} justifyContent="center" alignItems="center" p={"$4"}>
      <MaterialCommunityIcons name="cube-scan" size={100} color="black" />
      <Text size="xl" fontWeight={"bold"}>
        RFID Scanner
      </Text>
      <Text>Please scan RFID tag to start</Text>
      {lastKeyPressed && (
        <Box mt="$4">
          <Text fontWeight="bold">Last Key Pressed: {lastKeyPressed}</Text>
          <Text>Key Code: {lastKeyCode}</Text>
        </Box>
      )}
    </Box>
  );
}
mzakk commented 1 month ago

Ok man, found a misspelled function. Just update the package, i've released a new version.

thanks!

mzakk commented 1 month ago

Let me know if is all working!

kamarul-ariffin-github commented 1 month ago

I have done some testing but still not working for the barcode. it is sucessfully initialized:

11-01 09:34:38.173 24276 24306 I ReactNativeJS: 'Is barcode reader initialized:', false
11-01 09:34:39.310 24276 24306 I ReactNativeJS: 'initialize barcode reader result: ', true

but the startbarcode scan is not returning any data:

const startScan = async () => {
    console.log("start scan before");
    try {
      let result = await RNChainwaySensors.startBarcodeScan();
      console.log("start barcode scan result: ", result);
    } catch (error) {
      console.error("Error", error);
    }
    console.log("start scan after");
  };

  const stopScan = async () => {
    console.log("stop scan before");
    try {
      let result = await RNChainwaySensors.stopBarcodeScan();
      console.log("stop barcode scan result: ", result);
    } catch (error) {
      console.error("Error", error);
    }
    console.log("stop scan after");
  };
kamarul-ariffin-github commented 1 month ago

my latest code

import MaterialCommunityIcons from "@expo/vector-icons/MaterialCommunityIcons";
import { Box, Text } from "@gluestack-ui/themed";
import RNChainwaySensors from "chainway-sensors";
import { useNavigation } from "expo-router";
import { useEffect, useState } from "react";
import KeyEvent from "react-native-keyevent";

export default function ScanRfid({ onRfidScanned }: { onRfidScanned: () => void }) {
  const navigation = useNavigation();
  const [lastKeyPressed, setLastKeyPressed] = useState<string>("");
  const [lastKeyCode, setLastKeyCode] = useState<number | null>(null);

  const initializeScanner = async () => {
    try {
      console.log("RNChainwaySensors methods:", Object.keys(RNChainwaySensors));

      if (typeof RNChainwaySensors.isBarcodeReaderInit !== "function") {
        console.error("isBarcodeReaderInit is not a function");
        return;
      }

      const isInitialized = await RNChainwaySensors.isBarcodeReaderInit();
      console.log("Is barcode reader initialized:", isInitialized);

      if (!isInitialized) {
        if (typeof RNChainwaySensors.initializeBarcodeReader !== "function") {
          console.error("initializeBarcodeReader is not a function");
          return;
        }
        let result = await RNChainwaySensors.initializeBarcodeReader();
        console.log("initialize barcode reader result: ", result);
      }
    } catch (error) {
      console.error("Error in barcode initialization", error);
      console.error("Error details:", {
        name: error.name,
        message: error.message,
        stack: error.stack,
      });
    }
  };

  const startScan = async () => {
    try {
      console.log("===== start scan before");
      let result = await RNChainwaySensors.startBarcodeScan();
      console.log("start barcode scan result: ", result);
    } catch (error) {
      console.error("Error", error);
    }
    console.log("start scan after");
  };

  const stopScan = async () => {
    try {
      console.log("===== stop scan before");
      let result = await RNChainwaySensors.stopBarcodeScan();
      console.log("stop barcode scan result: ", result);
    } catch (error) {
      console.error("Error", error);
    }
    console.log("stop scan after");
  };

  useEffect(() => {
    navigation.setOptions({ headerShown: false });
    initializeScanner();

    console.log("before listener");
    KeyEvent.onKeyDownListener((keyEvent: any) => {
      console.log("keydown listener");
      if (!keyEvent) return;
      console.log("key down event pressed: ", keyEvent);
      setLastKeyPressed(keyEvent.pressedKey);
      setLastKeyCode(keyEvent.keyCode);

      if (keyEvent.keyCode === 293 || keyEvent.keyCode === 291) {
        console.log("start scan");
        startScan();
      }
    });

    KeyEvent.onKeyUpListener((keyEvent: any) => {
      console.log("keyuplistener");
      if (!keyEvent) return;
      console.log("key up event pressed: ", keyEvent);

      if (keyEvent.keyCode === 293 || keyEvent.keyCode === 291) {
        console.log("stop scan");
        stopScan();
      }
    });

    return () => {
      KeyEvent.removeKeyDownListener();
      KeyEvent.removeKeyUpListener();
    };
  }, [navigation, onRfidScanned]);

  return (
    <Box h={"100%"} w={"100%"} justifyContent="center" alignItems="center" p={"$4"}>
      <MaterialCommunityIcons name="cube-scan" size={100} color="black" />
      <Text size="xl" fontWeight={"bold"}>
        RFID Scanner
      </Text>
      <Text>Please scan RFID tag to start</Text>
      {lastKeyPressed && (
        <Box mt="$4">
          <Text fontWeight="bold">Last Key Pressed: {lastKeyPressed}</Text>
          <Text>Key Code: {lastKeyCode}</Text>
        </Box>
      )}
    </Box>
  );
}

the logs

11-01 10:08:45.833 25878 25908 I ReactNativeJS: 'RNChainwaySensors methods:', [ 'initializeUHF',
11-01 10:08:45.833 25878 25908 I ReactNativeJS:   'deinitializeUHF',
11-01 10:08:45.833 25878 25908 I ReactNativeJS:   'powerListener',
11-01 10:08:45.833 25878 25908 I ReactNativeJS:   'tagListener',
11-01 10:08:45.833 25878 25908 I ReactNativeJS:   'initializeUhfReader',
11-01 10:08:45.833 25878 25908 I ReactNativeJS:   'readSingleTag',
11-01 10:08:45.833 25878 25908 I ReactNativeJS:   'startReadingTags',
11-01 10:08:45.833 25878 25908 I ReactNativeJS:   'stopReadingTags',
11-01 10:08:45.833 25878 25908 I ReactNativeJS:   'readPower',
11-01 10:08:45.833 25878 25908 I ReactNativeJS:   'changePower',
11-01 10:08:45.833 25878 25908 I ReactNativeJS:   'deInitializeUhfReader',
11-01 10:08:45.833 25878 25908 I ReactNativeJS:   'clearTags',
11-01 10:08:45.833 25878 25908 I ReactNativeJS:   'initializeBarcodeReader',
11-01 10:08:45.833 25878 25908 I ReactNativeJS:   'deinitBarcodeReader',
11-01 10:08:45.833 25878 25908 I ReactNativeJS:   'isBarcodeReaderInit',
11-01 10:08:45.833 25878 25908 I ReactNativeJS:   'startBarcodeScan',
11-01 10:08:45.833 25878 25908 I ReactNativeJS:   'stopBarcodeScan',
11-01 10:08:45.833 25878 25908 I ReactNativeJS:   'addBarcodeListener',
11-01 10:08:45.833 25878 25908 I ReactNativeJS:   'removeBarcodeListener' ]
11-01 10:08:45.833 25878 25908 I ReactNativeJS: before listener
11-01 10:08:45.834 25878 25908 I ReactNativeJS: findAllSupplierForAnEntity
11-01 10:08:45.845 25878 25908 I ReactNativeJS: 'Is barcode reader initialized:', false
11-01 10:08:46.987 25878 25908 I ReactNativeJS: 'initialize barcode reader result: ', true
11-01 10:08:52.460 25878 25908 I ReactNativeJS: keydown listener
11-01 10:08:52.461 25878 25908 I ReactNativeJS: 'key down event pressed: ', { pressedKey: '\u0000', action: 0, keyCode: 293 }
11-01 10:08:52.515 25878 25908 I ReactNativeJS: start scan
11-01 10:08:52.516 25878 25908 I ReactNativeJS: ===== start scan before
11-01 10:08:52.583 25878 25908 I ReactNativeJS: keyuplistener
11-01 10:08:52.584 25878 25908 I ReactNativeJS: 'key up event pressed: ', { pressedKey: '\u0000', action: 1, keyCode: 293 }
11-01 10:08:52.584 25878 25908 I ReactNativeJS: stop scan
11-01 10:08:52.584 25878 25908 I ReactNativeJS: ===== stop scan before
mzakk commented 1 month ago

check the addBarcodeListener function in the library. When barcode is read, throws a 'BARCODE' event.

do something like this:

    const handleBarcodeScan = (args: any) => {
      const data = args;
     console.log("Barcode data",data)
    };

    RNChainwaySensors.addBarcodeListener(handleBarcodeScan);