react-nativeandroid-intentbarcodebarcode-scannerdatawedge

Urovo DT40 Intent data always null


I have an Urovo dt40 barcode scanner (Android 9) where I have a react-native app. Here I would like to find out the information of a scanned barcode in my app. But here my data is always null.

The scanner settings (intent output) in my device are the following:

This is my code:

const [barcodeContent, setBarcodeContent] = useState(null);
const [modalVisible, setModalVisible] = useState(false);

useEffect(() => {
  const listener = DeviceEventEmitter.addListener(
    "barcode_scan", 
    (intent) => {
      const intentData = JSON.stringify(intent, null, 2); 
      setBarcodeContent(intentData);
      setModalVisible(true);
    }
  );

  DataWedgeIntents.registerBroadcastReceiver({
    filterActions: ["android.intent.ACTION_DECODE_DATA"],
    filterCategories: ["android.intent.category.DEFAULT"],
  });

  return () => {
    // Clean up listener
    listener.remove();
  };
}, []);

return (
  <View style={styles.container}>
    <Button title="Show Last Scan" onPress={() => setModalVisible(true)} />
    <Modal
      visible={modalVisible}
      animationType="slide"
      transparent={true}
      onRequestClose={() => setModalVisible(false)}
    >
      <View style={styles.modalContainer}>
        <View style={styles.modalContent}>
          <Text style={styles.modalText}>Scanned Barcode:</Text>
          <Text style={styles.barcodeText}>{barcodeContent || "No data"}</Text>
          <Button title="Close" onPress={() => setModalVisible(false)} />
        </View>
      </View>
    </Modal>
  </View>
);

The output my modal shows:

{
   "data": null, 
   "labelType": null,
   "source": null
}

Solution

  • I have found this lib which solves my problem: https://github.com/iliapnmrv/react-native-urovo

    The code im using to capture a scanned barcode:

    useEffect(() => {
    
        let eventListener
        if (Urovo) { // used only for type safety
          const eventEmitter = new NativeEventEmitter(Urovo);
          eventListener = eventEmitter.addListener(
            UROVO_EVENTS.ON_SCAN,
            (scan) => {
              props.onScan(scan.value)
    
            }
          );
        }
    
        return () => {
          eventListener?.remove();
        };