Read Ndef data in a NFC Tag

Hi everyone !

First i would like to excuse my english and my technical language because i’m a brand new French dev’ which just started to discover Ionic 2.

I am trying to develop an application to read NFC tag and more precisely to read Ndef data into it.
But it is not working (that’s why i’m here !) :

I use addNdefFormatableListener and addTagDiscoveredListener just like the doc in https://github.com/chariotsolutions/phonegap-nfc#sample-projects

The problem is I can’t read the Ndef object because my application does not want to enter in my success function which is :

tagListenerSuccess(tagEvent:Event) {
    console.log("coucou");
    console.log(TagUtil.readTagFromJson(tagEvent));
    console.log(tagEvent);
} 

Although i can read the tag (which seems to contain only metadata) i can’t reach the ndef.
I get literally nothing on my console concerning ndef message or ndef record.

I am, of course, running my app on a physical smartphone (a Samsung Galaxy S4) linked to my computer to debug and to see the console on chrome.

I saw that in most of the cases, it is because there is no ndef to read but in testing my application with my subway card (not the fast food restaurant ^^) so i’m pretty sure there is data in the ndef. (Well it would be surprising there is not).

Here is my code if you want to look in details :

export class Nfc {
private dataReceived:boolean = false;
private tag:Tag;
private title:string = "nfc";
private data = null;

constructor(private vibration:Vibration,
            public platform:Platform,
            public navCtrl:NavController,
            public navParams:NavParams,
            public zone:NgZone,
            private nfc:NFC,
            private ndef:Ndef) {

    this.title = navParams.get('title') || this.title;
}

ionViewDidLoad() {
    this.platform.ready()
        .then(() => {
            //this.nfc.addTagDiscoveredListener().subscribe((tagEvent:Event) => this.tagListenerSuccess(tagEvent), (error) => console.log('error'));
            //this.nfc.addMimeTypeListener("application/json").subscribe((tagEvent:Event) => this.tagListenerSuccess(tagEvent), (error) => console.log('error'));
            //this.nfc.addNdefListener().subscribe((tagEvent) => this.tagListenerSuccess(tagEvent), (error) => console.log('error'));
        });


}

readNFCNdefListener():void {
    this.vibration.vibrate(100);
    this.nfc.addNdefFormatableListener().subscribe((tagEvent:Event) => this.tagListenerSuccess(tagEvent));
    this.nfc.addMimeTypeListener("text/json").subscribe((tagEvent:Event) => this.tagListenerSuccess(tagEvent));
    this.nfc.addMimeTypeListener("text/plain").subscribe((tagEvent:Event) => this.tagListenerSuccess(tagEvent));
    this.nfc.addMimeTypeListener("application/json").subscribe((tagEvent:Event) => this.tagListenerSuccess(tagEvent));
    this.nfc.addTagDiscoveredListener().subscribe((tagEvent:Event) => this.tagListenerSuccess(tagEvent));

}

tagListenerSuccess(tagEvent:Event) {
    console.log("coucou");
    console.log(TagUtil.readTagFromJson(tagEvent));
    console.log(tagEvent);
}

writeOnNfcTag():void {
    this.nfc.addTagDiscoveredListener().subscribe((tagEvent:Event) => this.tagWriterSuccess(tagEvent));
}

tagWriterSuccess(tagEvent) {
    let message = [
        this.ndef.textRecord("Hello je suis Ulysse 31 venu du futur"),
    ];

    this.nfc.write(message)
        .then((sucess)=> console.log("success"))
        .catch((error) => console.log(error));
}

The writeOnNfcTag and the readNFCNdefListener and related to button in my html file so i can activate them or not whenever i need it.
(I tried to write some data on my card just in case there were no data but my card is not writable of course :sweat_smile:)

Thanks for helping me or even responding to my problem !

1 Like

CCan you try to read your card with another app? Like Samsung NFCreader?

Some cards don’t have Ndef content, just used as identification the NdefId on the application(The most application), if you are trying to get just the Ndef Message, your app dont will response as success.

Try to read with another app. If you can, buy some tag to test and write.

Your implementation is Ok as documentation.

I would suggest looking into using ionic-native’s NFC shim.

Thanks for responding !

I tried with differents card and as you said, there is no ndef data in all my nfc card that i have. ( I didn’t expect that at all aha)

With an other application (NFC TagInfo) which can read ndef data.
I saw that only my old student card have ndef data but the format (Milfare Classic) is not supported by my phone. ( GREAT ! )
I supose that others cards just use the tag id to check servers to store less data on the card.

Unfortunately, I don’t have a room full of servers running in my house so i don’t think that is a possible solution :sweat_smile:

However, my boss lent me a book from a public library and it have a nfc tag with ndef data inside !
If someone told me that one i would test nfc tag on a book, i wouldn’t believe him.

Well i will keep you informed on that. For the moment i’m going to do some test with my new experimental subject !

could you post the event object you receive?

I remember that I had an ndefMessage object on the event which was just a payload array of integers, you might have to parse them to string if you want to read the message. Same the Id of the tag is an array of integers.

Of course !

But first i changed my code to read more clearly what i scanned :

readNFCNdefListener():void {
    this.vibration.vibrate(100);
    this.nfc.addNdefFormatableListener().subscribe((tagEvent) => this.tagListenerSuccess(tagEvent));
    this.nfc.addNdefListener().subscribe((tagEvent) => this.tagListenerSuccess(tagEvent));
    this.nfc.addTagDiscoveredListener().subscribe((tagEvent:Event) => this.tagListenerSuccess(tagEvent));

}

tagListenerSuccess(tagEvent) {
    console.log(tagEvent.type);
    if(Tag.TAG_TYPE == tagEvent.type){console.log("Ceci est un tag : " + tagEvent);}
    else{
        console.log("Ceci est un putain de ndef : " + tagEvent)}
        let data:Ndef = tagEvent;
        console.log(data);

}

The text in the console.log just says “there is a tag” and the second one says “there is a bad word ndef”

There what i get from the book when i read it from my application

And there something i don’t really understand (this is the application i downloaded and print me data from tag and ndef) :

I really don’t know where it gets all that data because my application doesn’t see any Ndef but just Ndef formatable which is not the same thing according to the documentation.

Furthermore, i don’t know why i can’t use method from android.nfc.tech.NdefFormatable in ionic ( well may be besause NdefFormatable does not exist in the plugin but well how i am supposed to manipule the data then ? I don’t really know).

Yes there is plenty of intergers array, and there are ascii characters i think… Parsing is not a problem, getting to thoses arrays is, unfortunately…

in the webconsole, could you show the data of the tag object that is attached to the event object please?

tagEvent.tag maybe you looked, I just didn’t see the content on the screen. Everything you need should be in there.

Here you go :

But i don’t think that’s we are looking for, maybe there no ndef message in that nfc tag too.

I’m saying this because i saw this post on stackOverflow :
http://stackoverflow.com/questions/42535930/ionic-2-use-nfc

And there, the ndef message was quiet different from the tag id array.

But may be the book uses the tag id to contains data ? I don’t know either.

It seems like no one is using ndef messages on their nfc objects.

Again i don’t think that this array is used to fill the data field (may be i’m wrong) when you look at this :

(You get the same on US-ASCII)

And here in HEX :

I can’t see the the link between them…

You are still using addNdefFormatableListener ? Because in the Readme it says:

The ndef-formatable event will not contain an NdefMessage.

Which explains why you don’t have any data.

Maybe I didn’t see it but did you only use the addNdefListener function? If you have both one might overwrite the other? not sure about this.
Also to debug better, maybe use a different function for every listener you add so you can log exactly which event did occur.

Seems tricky :confused: Last advice, maybe use the original functions of the nfc plugin and don’t use the wrapper of ionic native, sometimes there are differences between wrapper and plugin api.

Update:
Just found this in the Readme, rather only use one listener.

Multiple Listeners

Multiple listeners can be registered in JavaScript. e.g. addNdefListener, addTagDiscoveredListener, addMimeTypeListener.

On Android, only the most specific event will fire. If a Mime Media Tag is scanned, only the addMimeTypeListener callback is called and not the callback defined in addNdefListener. You can use the same event handler for multiple listeners.

It’s me again with bad news (again),

Indeed, I was using NdefFormatable because using NdefListener wasn’t giving me results, but i thought it was because of a bad implementation.

It tried your solutions (just adding a ndefListener instead of both and not using the ionic wrapper), but i get the same results as my precedent post.

I think that your solutions work great but it is just the tag which is weird.
Your code or my code just found a NdefFormatable, no sign of a Ndef object. So i think there is none.
However, i’m curious how the NFCReader that i downloaded can find informations ^^

I think it is pointless trying to work with a NFC tag which is not blank.
I will ask my boss to get me one if it is possible (i’m a trainee so… :sweat_smile:)

I will keep this thread updated if there is anything new.
I didn’t find much informations about this subject, maybe there is others people in my case, trying to understand how NFC it works and I hope this threat will be useful.

Thanks again for answering my post ! You helped me understand ionic and NFC so it was quite useful after all !

i’m following the same link, but in my case doesn’t work.

I’m trying on real device (Samsung S7 Edge with Android 7)

Can you share your source code?

Thanks.