Hi!
Can someone help me with this one?
This has been boggling my mind for a few weeks now.
NFC working example needed:
What I need is my app to be able to send an array of information via NFC.
This is what I used, yet it doesn’t seem to work - i tried a few things out, with no luck yet. This is also another forum member’s solution from last year.
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
// plugins
import { NFC, Ndef } from '@ionic-native/nfc';
import { Subscription } from 'rxjs/Rx'
@IonicPage()
@Component({
selector: 'page-nfc',
templateUrl: 'nfc.html',
})
export class NfcPage {
readingTag: boolean = false;
writingTag: boolean = false;
isWriting: boolean = false;
ndefMsg: string = '';
subscriptions: Array<Subscription> = new Array<Subscription>();
constructor(
public navCtrl: NavController,
public navParams: NavParams,
public nfc: NFC,
public ndef: Ndef) {
this.subscriptions.push(this.nfc.addNdefListener()
.subscribe(data => {
if (this.readingTag) {
let payload = data.tag.ndefMessage[0].payload;
let tagContent = this.nfc.bytesToString(payload).substring(3);
this.readingTag = false;
console.log("tag data", tagContent);
}
else if (this.writingTag) {
if (!this.isWriting) {
this.isWriting = true;
this.nfc.write([this.ndefMsg])
.then(() => {
this.writingTag = false;
this.isWriting = false;
console.log("written");
})
.catch(err => {
this.writingTag = false;
this.isWriting = false;
});
}
}
},
err => {
})
);
}
ionViewWillLeave() {
this.subscriptions.forEach(sub => {
sub.unsubscribe();
});
}
readTag() {
this.readingTag = true;
}
writeTag(writeText: string) {
this.writingTag = true;
this.ndefMsg = this.ndef.textRecord(writeText);
}
}
It would be extremely helpful if you’d help. Donation will be sent and arranged to the best solution.
Thanks in advance.