First time NFC read doesn't work


#1

Hi everyone!

I have to read a NFC tag with ionic 2, the problem is the first attempt doesn’t read then if I read again is working fine,

This is my code:


@IonicPage()
@Component({
  selector: 'page-dispositivo',
  templateUrl: 'dispositivo.html',
  providers: [NFC, Ndef, Diagnostic, Vibration]
})
export class DispositivoPage {

  subscriptions: Array<Subscription> = new Array<Subscription>();

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private auth: AuthService,
    public loading: LoadingController,
    public alertCtrl: AlertController,
    public toastCtrl: ToastController,
    private nfc: NFC,
    private ndef: Ndef,
    public diagnostic: Diagnostic,
    public platform: Platform,
    public vibration: Vibration
  ) {

    platform.ready().then(() => {
      this.initNFC();
    });
  }

  initNFC() {
    this.nfc.enabled()
      .then(() => {
        this.addListenNFC();
      }).catch(err => {
        let alert = this.alertCtrl.create({
          subTitle: 'Active la conexión NFC para para leer artículos',
          buttons: [
            {
              text: 'Activar más tarde',
              handler: () => {
                this.goToHome();
              }
            },
            {
              text: 'Ir a Ajustes para activar',
              handler: () => {
                this.nfc.showSettings().then(rs => {
                  this.goToHome();
                });
              }
            }]
        });
        alert.present();
      });
  }

  addListenNFC() {
    this.subscriptions.push(this.nfc.addTagDiscoveredListener().subscribe(nfcData => {
      this.processResponse(nfcData);
    }));
  }


I don’t know what is happening, somebody can help me?


First time NFC read doesn’t work
Working NFC Plugin method needed - DONATION FOR SOLUTION
#2

Me too. @Carlosvel60 did you found a work around?

P.


#3

I know this is bit late but , the solution is much simpler than the problem ,
first use only single subscription dont use the array.

subscriptions:Subscription = new Subscription();
ndeflistner:any;

later in code;
this.ndeflistner = this.nfc.addMimeTypeListener(this.tpe);

u may use addndefListner() too

then subscrible to it

this.subscriptions = this.ndeflistner.subscribe(data=>{
//your code
});


#4

Thank you for the help.

I didn’t understand what is the class “Subscription”. Where is it defined?

This is my code:

import { Component } from '@angular/core';
import { Platform, AlertController } from 'ionic-angular';
import { NFC } from '@ionic-native/nfc';
import { SplashScreen } from '@ionic-native/splash-screen';
import { InAppBrowser } from '@ionic-native/in-app-browser';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
    
    tagId: string = "??"
    timeout: any
    scrivibile: string
    proteggibile: string
    maxSize: number
    readOnly: string

    constructor(private nfc: NFC, private platform: Platform,
                private alertCtrl: AlertController, private splashScreen: SplashScreen,
                private iab: InAppBrowser) {
        platform.ready().then(() => {
            console.log('Platform ready: checking for nfc enabled...')
            this.timeout = setInterval(() => {
                this.timeoutExpired();
            }, 1000*0.5);
            console.log('Timeout started');
            nfc.enabled().then((flag) => {
                clearInterval(this.timeout);
                console.log('nfc enabled risponde: ' + flag);
                    nfc.addNdefListener().subscribe(
                        (data: Event) => this.nfcOkNdef(data),
                        (err)  => console.log(err)
                    );
                splashScreen.hide();
            });
        });

    }

    timeoutExpired() {
        this.splashScreen.hide();
        clearInterval(this.timeout);
        let noNfc = this.alertCtrl.create({
            title: 'NFC not enabled',
            message: 'App will expired; do you want NFC setup to open?',
            buttons: [
                {text: 'Yes', handler: () => { this.nfc.showSettings(); this.platform.exitApp(); } },
                {text: 'No', handler: () => { this.platform.exitApp() } }
            ]
        })
        noNfc.present();
    }

    .....some other functions.....

}

Any help will be appreciated.


#5

use import {Subscription} from ‘rxjs/Subscription’
You need to learn the angular basics to really understand all the things .
Writing nfc service is not a cake walk .

I will give you an advice , if your using nfc tags then you can use write and ndef listner combo,
if you want to transfer data between two android devices , then android beam ,
functions to use for android beam is nfc.share and listner at other end.


#6

Thanks a lot for helping…
I tried, but it’s the same: the first scan does not trigger the subscription. Then all run OK.
By the way, I need only the ID of the NFC. I read also the data programmed in, but this is no important. And I don’t write the nfc, anyway.

Pietro


#7

I have implemented it , use single subscription and attached that listner to subscription , and are you reading tags of android phone