react-nativenfcscanning

Error with React Native NFC manager Initialise


Hello I have the following code:

import React, { useEffect } from 'react';
import { View, Text, Button, Alert } from 'react-native';
import { NfcTech } from 'react-native-nfc-manager';
import NfcManager from 'react-native-nfc-manager';

const NFCWriterCom = () => {
  useEffect(() => {
    async function initNfc() {
      try {
        await NfcManager.start();
        console.log('NFC Manager initialized successfully.');

        // Check if NFC Manager is initialized before configuring P2P mode
        if (!NfcManager.isInitialized) {
          console.warn('NFC Manager not initialized.');
          Alert.alert('Error', 'NFC Manager not initialized.');
          return;
        }

        // Configure NFC for P2P mode (Android Beam)
        await configureNfcForP2PMode();

        // Cleanup when the component is unmounted
        return () => {
          NfcManager.stop();
        };
      } catch (ex) {
        console.warn('Error initializing NFC Manager:', ex);
      }
    }

    initNfc();
  }, []);

  async function configureNfcForP2PMode() {
    try {
      // Request NFC P2P technology
      await NfcManager.requestTechnology(NfcTech.Ndef);

      console.log('NFC P2P mode configured.');
    } catch (ex) {
      console.warn('Error configuring NFC for P2P mode:', ex);
      Alert.alert('Error', 'Failed to configure NFC for P2P mode.');
    }
  }

  async function initiateSharing() {
    try {
      // Check if NFC Manager is initialized
      if (!NfcManager.isInitialized) {
        console.warn('NFC Manager not initialized.');
        Alert.alert('Error', 'NFC Manager not initialized.');
        return;
      }
  
      // Check if NFC technology is available
      if (!NfcManager.ndef) {
        console.warn('NFC technology not available.');
        Alert.alert('Error', 'NFC technology not available.');
        return;
      }
  
      // Start NFC P2P sharing with the AFM number
      const afmNumber = '18109603490';
      const ndefMessage = [
        NfcTech.Ndef,
        NfcManager.ndef.textRecord(afmNumber),
      ];
      await NfcManager.setNdefPushMessage(ndefMessage);
  
      console.log('Sharing initiated with AFM number:', afmNumber);
    } catch (ex) {
      console.warn('Error initiating sharing:', ex);
      Alert.alert('Error', 'Failed to initiate sharing.');
    }
  }
  
  return (
    <View>
      <Text> NFC Writer (Phone as Tag) </Text>
      <Button title="Initiate Sharing" onPress={initiateSharing} />
    </View>
  );
};

export default NFCWriterCom;

that im using NFC libraries to scan with my nfc another nfc phone and pass it a specific number but when i try to run it and press the button to start sharing , it keep saying me that nfc manager is not initialised. I've tried with many libraries but still didnt make it work. Anyone got any idea with which library or how this can work?


Solution

  • Android Beam was removed and deprecated in Android 10.

    Hence you cannot use it on your Android 13 test phone.