Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-native-zebra-rfid-barcode-ef-tech

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-zebra-rfid-barcode-ef-tech

This React Native module enables seamless integration with Zebra RFID readers and barcode scanners.

  • 0.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
increased by400%
Maintainers
0
Weekly downloads
 
Created
Source

react-native-zebra-rfid-barcode-ef-tech

This React Native module enables seamless integration with Zebra RFID readers and barcode scanners.

Demo

https://github.com/efernandes-tech/react-native-zebra-rfid-barcode-ef-tech/assets/78204178/a54c4616-76ce-48da-86cb-6ceaab1beeef

Installation

npm install react-native-zebra-rfid-barcode-ef-tech
yarn add react-native-zebra-rfid-barcode-ef-tech

Linking

yarn android

Import

import {
  ZebraEvent,
  ZebraEventEmitter,
  getAllDevices,
  connectToDevice,
  type ZebraResultPayload,
  type ZebraRfidResultPayload,
} from 'react-native-zebra-rfid-barcode-ef-tech';

// ...

Get All RFID Devices

const listDevices = await getAllDevices();
// will return array of devices names like ["RFD+19189414", "RFD+018134912"]

Connect to device

useEffect(() => {
  const deviceConnectEvent = ZebraEventEmitter.addListener(
    ZebraEvent.ON_DEVICE_CONNECTED,
    (e: ZebraResultPayload) => {
      console.log(e.data); // "Connect successfully" || "Connect failed"
    }
  );

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

//...

<TouchableOpacity onPress={() => connectToDevice(deviceName)}>
  <Text>{deviceName}</Text>
</TouchableOpacity>

Barcode Scanner

useEffect(() => {
  const barcodeEvent = ZebraEventEmitter.addListener(
    ZebraEvent.ON_BARCODE,
    (e: ZebraResultPayload) => {
      handleBarcodeEvent(e.data); // string
    }
  );

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

const handleBarcodeEvent = useCallback(
  debounce((newData: string) => {
    setListBarcodes((pre) => [...pre, newData]);
  }, 200),
  []
);

RFID Reader

useEffect(() => {
  const rfidEvent = ZebraEventEmitter.addListener(
    ZebraEvent.ON_RFID,
    (e: ZebraRfidResultPayload) => {
      handleRfidEvent(e.data); // array of string tags
    }
  );

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

const handleRfidEvent = useCallback(
  debounce((newData: string[]) => {
    setListRfid((pre) => [...pre, ...newData]);
  }, 200),
  []
);

Full code example

import React, { useCallback, useEffect, useState } from 'react';
import debounce from 'lodash/debounce';
import {
  FlatList,
  StyleSheet,
  Text,
  TouchableOpacity,
  View,
} from 'react-native';
import {
  ZebraEvent,
  ZebraEventEmitter,
  connectToDevice,
  getAllDevices,
  type ZebraResultPayload,
  type ZebraRfidResultPayload,
} from 'react-native-zebra-rfid-barcode-ef-tech';

export default function App() {
  const [listDevices, setListDevices] = useState<string[]>([]);
  const [listBarcodes, setListBarcodes] = useState<string[]>([]);
  const [listRfid, setListRfid] = useState<string[]>([]);

  useEffect(() => {
    getListRfidDevices();

    const barcodeEvent = ZebraEventEmitter.addListener(
      ZebraEvent.ON_BARCODE,
      (e: ZebraResultPayload) => {
        handleBarcodeEvent(e.data);
      }
    );

    const rfidEvent = ZebraEventEmitter.addListener(
      ZebraEvent.ON_RFID,
      (e: ZebraRfidResultPayload) => {
        handleRfidEvent(e.data);
      }
    );

    const deviceConnectEvent = ZebraEventEmitter.addListener(
      ZebraEvent.ON_DEVICE_CONNECTED,
      (e: ZebraResultPayload) => {
        console.log(e.data); // "Connect successfully" || "Connect failed"
      }
    );

    return () => {
      barcodeEvent.remove();
      rfidEvent.remove();
      deviceConnectEvent.remove();
    };
  }, []);

  const handleRfidEvent = useCallback(
    debounce((newData: string[]) => {
      setListRfid((pre) => [...pre, ...newData]);
    }, 200),
    []
  );

  const handleBarcodeEvent = useCallback(
    debounce((newData: string) => {
      setListBarcodes((pre) => [...pre, newData]);
    }, 200),
    []
  );

  const getListRfidDevices = async () => {
    const listDevices = await getAllDevices();
    setListDevices(listDevices);
  };

  return (
    <View style={styles.container}>
      <View
        style={{
          maxHeight: 200,
        }}
      >
        <Text style={[styles.text, styles.title]}>
          Devices: {listDevices.length}
        </Text>
        <FlatList
          style={{ backgroundColor: '#FEF3C7' }}
          ItemSeparatorComponent={() => <View style={styles.separator} />}
          data={listDevices}
          renderItem={({ item }) => (
            <TouchableOpacity
              onPress={() => connectToDevice(item)}
              style={styles.item}
            >
              <Text style={styles.text}>{item}</Text>
            </TouchableOpacity>
          )}
        />
      </View>

      <View style={styles.partial}>
        <Text style={[styles.text, styles.title]}>
          Barcodes: {listBarcodes.length}
        </Text>
        <FlatList
          style={{ backgroundColor: '#DCFCE7' }}
          ItemSeparatorComponent={() => <View style={styles.separator} />}
          data={listBarcodes}
          renderItem={({ item }) => (
            <View style={styles.item}>
              <Text style={styles.text}>{item}</Text>
            </View>
          )}
        />
      </View>

      <View style={styles.partial}>
        <Text style={[styles.text, styles.title]}>
          RFIDs: {listRfid.length}
        </Text>
        <FlatList
          style={{ backgroundColor: '#E0F2FE', marginBottom: 10 }}
          ItemSeparatorComponent={() => <View style={styles.separator} />}
          data={listRfid}
          renderItem={({ item }) => (
            <View style={styles.item}>
              <Text style={styles.text}>{item}</Text>
            </View>
          )}
        />
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingHorizontal: 16,
    backgroundColor: 'white',
  },
  partial: {
    flex: 1,
  },
  item: {
    height: 50,
    paddingHorizontal: 15,
    justifyContent: 'center',
  },
  separator: {
    height: 1,
    backgroundColor: '#ccc',
  },
  text: {
    color: '#333',
  },
  title: {
    fontWeight: 'bold',
    fontSize: 16,
    marginVertical: 5,
  },
});

License

MIT


Made with create-react-native-library

Keywords

FAQs

Package last updated on 18 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc