RosariaCol
RosariaCol

Reputation: 21

Reading ndef message between smartphone and arduino

I'm creating an NFC mobile app (React-Native) for receiving and sending Ndef messages between an rc532, connected to an Arduino Uno, and the smartphone.

To send data from the smartphone to the Arduino, I used the Android Beam communication mode. I have problems receiving data, or rather reading from the smartphone.

On the Arduino I simulate a card, the mobile app reads the tag but not the content, that is the Ndef message, returns it to me as "undefine" or "null".

The library I used is the following: https://github.com/whitedogg13/react-native-nfc-manager.

This is my data reading code:

readData = async () => {
    NfcManager.start();
    NfcManager.setEventListener(NfcEvents.DiscoverTag, tag => {
        console.log('tag', tag);
        console.log(NfcManager.getCachedNdefMessageAndroid(tag));
        console.log(this.parseText(tag));
        console.log(JSON.stringify(tag.data));
        //NfcManager.unregisterTagEvent().catch(() => 0);
      });
        
    }

What it returns is:

[Thu Jan 21 2021 13:46:10.960]  LOG      Running "projectNFC2" with {"rootTag":1} [Thu Jan 21 2021 13:46:13.182]  LOG      tag {"id": "0000000000000000", "techTypes": ["android.nfc.tech.NfcF"]} 
[Thu Jan 21 2021 13:46:13.215]  LOG      {"_U": 0, "_V": 0, "_W": null, "_X": null} 
[Thu Jan 21 2021 13:46:13.219]  LOG      null 
[Thu Jan 21 2021 13:46:13.221]  LOG      undefined 
[Thu Jan 21 2021 13:46:15.200]  WARN Possible Unhandled Promise Rejection (id: 0): "no tech request available"

Does anyone have any ideas or solutions to my problem?

Upvotes: 2

Views: 726

Answers (1)

vincenzopalazzo
vincenzopalazzo

Reputation: 1645

It is important to read and understands the error, like this

[Thu Jan 21 2021 13:46:15.200] WARN Possible Unhandled Promise Rejection (id: 0): "no tech request available"

It is an exception generated by the library a this line and it look like that you are not setting the type of NFC technology use and the library has the object techRequest undefined.

From the example I can understand that you are missing the NFC configuration, maybe you code need to be

In your situation, y9ou can start the NFC inside the method componentDidMount, an example can be

componentDidMount() {
  NfcManager.start();
}

In addition, you need to set up the NfcTech because the library has under the hood an undefined object, your code could be somethings like that

  readData = async () => {
    let tech = Platform.OS === 'ios' ? NfcTech.MifareIOS : NfcTech.NfcA;
    let resp = await NfcManager.requestTechnology(tech, {
      alertMessage: 'Ready to do some custom Mifare cmd!'
    });
    console.warn(resp);

    // In addition the NFC uid can be found in tag.id
    //let tag = await NfcManager.getTag();
    //console.warn(tag);
    NfcManager.setEventListener(NfcEvents.DiscoverTag, tag => {
        console.log('tag', tag);
        console.log(NfcManager.getCachedNdefMessageAndroid(tag));
        console.log(this.parseText(tag));
        console.log(JSON.stringify(tag.data));
        //NfcManager.unregisterTagEvent().catch(() => 0);
      });

Upvotes: 0

Related Questions