revtel / react-native-nfc-manager

React Native NFC module for Android & iOS
MIT License
1.36k stars 317 forks source link

Keep reading tags #578

Closed robertcoroianu closed 1 year ago

robertcoroianu commented 1 year ago

Hello, It's there a way to keep scanning the tags not only after a button was press?

robertcoroianu commented 1 year ago

I added a event, for discoverTags and not unregister :)

`  function readNdef() {
    NfcManager.setEventListener(NfcEvents.DiscoverTag, (tag: any) => {
      let tagFound = tag;
      console.log(tagFound);
      // NfcManager.unregisterTagEvent();
    });
    NfcManager.registerTagEvent();
  }`
Productivix commented 1 year ago

hi, can you @robertcoroianu please the complete code example ? thank you !

robertcoroianu commented 1 year ago

hi, can you @robertcoroianu please the complete code example ? thank you !

import {View, StyleSheet, Text} from 'react-native';
import React, {useEffect} from 'react';
import NfcManager, {NfcEvents} from 'react-native-nfc-manager';

NfcManager.start();

function RfidReader(props: {sendAuth: (code: string, type: number) => void}) {
  const changeEndianness = (value: string) => {
    const result = [];
    let len = value.length - 2;
    while (len >= 0) {
      result.push(value.substring(len, 3));
      len -= 2;
    }
    return result.join('');
  };
  function barcodeRecognized(code: any): void {
    code = changeEndianness(code);
    props.sendAuth(code, 2);
  }
  async function readNdef() {
    if (await NfcManager.isSupported()) {
      NfcManager.setEventListener(NfcEvents.DiscoverTag, (tag: any) => {
        let tagFound = tag;
        console.log(tagFound);
        barcodeRecognized(tag.id);
        // NfcManager.unregisterTagEvent();
      });
      NfcManager.registerTagEvent();
    } else if (await NfcManager.isEnabled()) {
      console.log('NFC Not Active');
    }
  }

  // Run once
  useEffect(() => {
    readNdef();
  }, []);
  return ( );
}

export default RfidReader;
Productivix commented 1 year ago

yes ! thank you : it runs very well !