Working NFC Plugin method needed - DONATION FOR SOLUTION


#1

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.


#2

Hey , first of all What do you want to do , transfer data between device & tag or exchange data between devices . Let me know ,what is your exact expectation from NFC


#3

Thanks for the reply,

I would like to transfer an array (with only written information; let’s say “petnames”) to another device. After the reciever phone gets the array it should print it out (both in console and on a list - you can simply call this function X).

In use: button sends petnames and gets the other phones petnames and with the successful nfc encounter, the phone alerts (with a sound) the user and prints a list out.

That is all i need.
Thank you in advance.


#4

First of all you don’t use nfc.write to communicate between devices , We use write mostly for tags .You should use Android beam, so use this.nfc.share([record]) & create record of mime type ‘application/json’ . For commumication , sender should use this.nfc.share([record]) & the reciever should be ndef listener or mimeTypeListener .


#5

Would you be able to provide an example?
How do you set html and ts up to transfer and get an array?

Thanks in advance.


#6

refer to this thread …
Understand it you will get it . I prefer you learn it thats better in long run. If you dont get it I ill provide you partial solution.