EddyVerbruggen / nativescript-nfc

:pencil: NativeScript plugin to discover, read, and write NFC tags
MIT License
85 stars 38 forks source link

callback not get called when detect nfc #34

Open kousherAlam opened 5 years ago

kousherAlam commented 5 years ago

callback not getting called, when nfc detected. the device play the sound but nothing is happening ...

` import { Injectable } from "@angular/core"; import { Nfc, NfcNdefData, NfcTagData } from "nativescript-nfc"; import { Subject } from "rxjs";

@Injectable({ providedIn: 'root' }) export class NfcReaderService { nfcData = new Subject();

private nfc: Nfc = new Nfc();

constructor() {
    this.nfc.available().then((avail) => {
        this.nfc.enabled().then((on) => {
            if (on) {
                console.log("NFC Enabled!!!");
                this.readNFC();
                this.onTagDiscoverd();
            } else {
                console.log("NFC Not Enabled");
            }
        });
    });
}

public readNFC() {
    this.nfc.setOnNdefDiscoveredListener((data: NfcNdefData) => {
        console.log("Nfc Data: ", data);
        this.nfcData.next(data);
        if (data.message) {
            for (let m in data.message) {
                let record = data.message[m];
                console.log("Ndef discovered! Message record: " + record.payloadAsString);
            }
        }
    }, {
            stopAfterFirstRead: false,
            scanHint: "Scan a tag, baby!"
        }).then(() => {
            console.log("OnNdefDiscovered listener added");
        });
}

public onTagDiscoverd() {
    this.nfc.setOnTagDiscoveredListener((data: NfcTagData) => {
        console.log("Discovered a tag with ID " + data.id);
    }).then(() => {
        console.log("OnTagDiscovered listener added");
    });
}

}

`

S-Maxime commented 5 years ago

I have actually the same problem, here my gist : https://gist.github.com/Ravaniss/e8b25658995198d95f84dba2d5def57a

It will be good to fix this in priority. ^^

EddyVerbruggen commented 5 years ago

Please share a repo instead of a snippet and I'll take a look. Also, please post steps to reproduce this issue.

S-Maxime commented 5 years ago

@EddyVerbruggen Thank you so much to see you here ! :)

I did a repo just for you <3 https://github.com/Ravaniss/nfcprob

Here is what i have did :

1) create a project with vue init nativescript-vue/vue-cli-template <project-name>

thank you !

EddyVerbruggen commented 5 years ago

@Ravaniss This problem is on Android I guess?

What I'm seeing is when I scan an Nfc tag with your demo (after pressing all buttons) is when a tag was written to, it calls the ndef listener with all data, and if it's blank it calls the other callback.

S-Maxime commented 5 years ago

Yes, it's on Android.

How did you get all data from the thing you scan ? O_O Am i missing something ? Are you with IOS ?

Don't tell me i've missing something ... xD

EddyVerbruggen commented 5 years ago

I'm testing on Android as well. Perhaps it depends on the type of tag you're scanning.. there are so many of them. But I don't see anything out of order..

S-Maxime commented 5 years ago

Here is what i get with another app, and it's exactly these informations i need like, platform, type of tag and disponible technology

https://ibb.co/0ftrYfW

EddyVerbruggen commented 5 years ago

Looks what I'm getting in the ndef listener. Perhaps try only setting that listener (not the tag listener).

S-Maxime commented 5 years ago

What should i do ? Because for me, my code is okay. Can you please give me an example ?

EddyVerbruggen commented 5 years ago

Your code is fine for Android. Like I said, when only setting the ndef listener in your demo my testtags read out correctly.

S-Maxime commented 5 years ago

Like this ? https://gist.github.com/Ravaniss/c0f7343a5b9c6978b1be01dcde5d9b39 I get this :

Tag discovered, but no listener was set via setOnTagDiscoveredListener. Ndef: {"id":[4,16,-114,58,41,94,-127],"techList":["android.nfc.tech.NfcA","android.nfc.tech.MifareUltralight","android.nfc.tech.NdefFormatable"]}

If yes, how can i get the name and the platform ? Like this : https://ibb.co/0ftrYfW

EDIT : Sorry but, the callback is not triggered. What i am missing ? Because, i get data id and techlist, just like you can see on my screenshot from another app, but not like i would like to be.

Pkurto commented 5 years ago

I have the same problem here described. Do you discover the solution?

S-Maxime commented 5 years ago

Nope.

Pkurto commented 5 years ago

I am trying to use this plugin and when I run the demo code to read a tag (using my Samsung S8) I get the following error:

Tag discovered, but no listener was set via setOnTagDiscoveredListener. Ndef: {"id":[4,25,31,-126,-39,91,-128],"techList":["android.nfc.tech.IsoDep","android.nfc.tech.NfcA","android.nfc.tech.NdefFormatable"]}

I added a console.log in the first line of the callback function and it not appears on the logs. It seems the callback function is not called.

Any help?

Pkurto commented 5 years ago

[UPDATE]

If I set a listener using"setOnTagDiscoveredListener" it will be called correctly when I scan a tag. For example:

OnTagDiscovered Listener set
ENTER TAG LISTENER!!!!!!!!!!!!!!!!!!!!!!!!
Tag discovered! 4,25,31,-126,-39,91,-128 android.nfc.tech.IsoDep, android.nfc.tech.NfcA, android.nfc.tech.NdefFormatable

Then I stopped this listener with "setOnTagDiscoveredListener(null)" and tested to set the listener for "setOnNdefDiscoveredListener". Then when I scan the tag again and I had the following output message:

doStartNdefListener Listener set
Tag discovered, but no listener was set via setOnTagDiscoveredListener. 
Ndef: {"id":[4,25,31,-126,-39,91,-128],"techList":["android.nfc.tech.IsoDep","android.nfc.tech.NfcA","android.nfc.tech.NdefFormatable"]}

I am trying to use the setOnNdefDiscoveredListener because it seems to have more information. Am I using this module the wrong away?