chariotsolutions / phonegap-nfc

PhoneGap NFC Plugin
MIT License
706 stars 555 forks source link

NFC Service detects tag before addNdefFormatableListener #385

Closed pierresh closed 4 years ago

pierresh commented 4 years ago

Hello,

My app was working correctly with Phonegap-nfc 1.0.3, and now I am updating it to Cordova Android 8.1.0.

I successfully installed Phonegap-nfc 1.0.4, but the background service detects the NFC before my app, so a window appears in front of my app, although I did not change anything in my code... Does anyone know how t solve this? Thanks!

Below is my ionic-info

Ionic:

   ionic (Ionic CLI)  : 4.12.0
   Ionic Framework    : ionic-angular 3.9.8
   @ionic/app-scripts : 3.2.3

Cordova:

   cordova (Cordova CLI) : 8.1.2 (cordova-lib@8.1.1)
   Cordova Platforms     : android 8.1.0, browser 5.0.3, ios 4.5.5
   Cordova Plugins       : cordova-plugin-ionic-keyboard 2.1.2, cordova-plugin-ionic-webview 1.2.1, (and 24 other plugins)

System:

   Android SDK Tools : 25.2.5 (/usr/local/Cellar/android-sdk/24.1.2/)
   ios-deploy        : 1.9.2
   NodeJS            : v10.16.0 (/usr/local/bin/node)
   npm               : 6.9.0
   OS                : macOS Mojave
   Xcode             : Xcode 11.2.1 Build version 11B500

below is my code:

scanNFC(){
    this.nfc_subscription = this.nfc.addNdefListener(() => {
        this.isScanning = true;
    }, (err) => {
        if (err == "NFC_DISABLED") {
            this.dialogCtrl.stdAlert('ALERT', 'NFC_IS_NOT_ACTIVATED', 'CLOSE');
        } else {
            this.dialogCtrl.errorAlert(500, JSON.stringify(err));
        }
    }).subscribe((event) => {
        let tag = event.tag;

        this.nfc_subscription.unsubscribe();
        this.isScanning = false;

        if (tag.ndefMessage == undefined) {
            this.dialogCtrl.stdAlert('ALERT', 'NFC_IS_EMPTY', 'CLOSE');
        } else {
            let ndefMessage = tag.ndefMessage;

            if (this.nfc.bytesToString(ndefMessage[0].payload).substring(3) == this.code_to_scan) { 
                this.scanningTag = 0;
            } else {
                this.dialogCtrl.stdAlert('ALERT', 'NFC_TAG_DO_NOT_MATCH', 'CLOSE');
            }
        }
    });
}
pierresh commented 4 years ago

Actually, I uninstalled and reinstalled Cordova Android 8.1.0 and now it is working well, sorry for that...