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 )
Thanks for helping me or even responding to my problem !