Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cant scan NFC tag #10

Closed
empaaa opened this issue Sep 21, 2016 · 24 comments
Closed

Cant scan NFC tag #10

empaaa opened this issue Sep 21, 2016 · 24 comments

Comments

@empaaa
Copy link

empaaa commented Sep 21, 2016

Hi, i've tried to implement your nuget and follow your installation guide for Android.

To sum it up ive done this in MainActivity:

NfcManager NfcManager = (NfcManager)Android.App.Application.Context.GetSystemService(NfcService);
            NFCdevice = NfcManager.DefaultAdapter;

            Xamarin.Forms.DependencyService.Register<INfcForms, NfcForms>();
            x = Xamarin.Forms.DependencyService.Get<INfcForms>() as NfcForms;
protected override void OnResume()
        {
            base.OnResume();
            if (NFCdevice != null)
            {
                var intent = new Intent(this, GetType()).AddFlags(ActivityFlags.SingleTop);
                NFCdevice.EnableForegroundDispatch
                (
                    this,
                    PendingIntent.GetActivity(this, 0, intent, 0),
                    new[] { new IntentFilter(NfcAdapter.ActionTechDiscovered) },
                    new String[][] {new string[] {
                            NFCTechs.Ndef,
                        },
                        new string[] {
                            NFCTechs.MifareClassic,
                        },
                    }
                );
            }
        }

        protected override void OnPause()
        {
            base.OnPause();
            NFCdevice.DisableForegroundDispatch(this);
        }

        protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
            x.OnNewIntent(this, intent);
        }

And in the MainMenuPage.xaml.cs in my shared project:

INfcForms device = DependencyService.Get<INfcForms>();
            if (device != null)
            {
                device.NewTag += HandleNewTag;
            }
public void HandleNewTag(object sender, NfcFormsTag e)
        {
            var notification = new Notifications();
            notification.Show("NFC id", "ID: " + e.Id, 3, ToastNotificationType.Info);
        }

But when i let my phone scan a NFC tag, nothing happens at all. Of course ive turn on the permission.nfc in my manifest.

/E

@poz1
Copy link
Owner

poz1 commented Sep 21, 2016

Hey, which device are you testing on?

@empaaa
Copy link
Author

empaaa commented Sep 21, 2016

Samsung Galaxy s6 Edge, and Samsung Galaxy a3

Skickat från min Samsung Galaxy-smartphone.

-------- Originalmeddelande --------
Från: Alessandro Pozone notifications@github.com
Datum: 2016-09-21 23:01 (GMT+01:00)
Till: poz1/NFCForms NFCForms@noreply.github.com
Kopia: empaaa empa_ortberg@hotmail.com, Author author@noreply.github.com
Rubrik: Re: [poz1/NFCForms] Cant scan NFC tag (#10)

Hey, which device are you testing on?

You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com//issues/10#issuecomment-248741559, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJLoKXaLnc2FTZE5GGdEpAbk4nVWstL6ks5qsZr0gaJpZM4KCwUP.

@poz1
Copy link
Owner

poz1 commented Sep 21, 2016

Unfortunately seems that your devices do not support Mifare cards ( http://www.shopnfc.it/en/content/7-nfc-device-compatibility ), do you have others NFC Tags to test?

@empaaa
Copy link
Author

empaaa commented Sep 21, 2016

I havet actully tried it on our test devisen that is a Samsung xcover. And same resultat there...

Skickat från min Samsung Galaxy-smartphone.

-------- Originalmeddelande --------
Från: Alessandro Pozone notifications@github.com
Datum: 2016-09-21 23:52 (GMT+01:00)
Till: poz1/NFCForms NFCForms@noreply.github.com
Kopia: empaaa empa_ortberg@hotmail.com, Author author@noreply.github.com
Rubrik: Re: [poz1/NFCForms] Cant scan NFC tag (#10)

Unfortunately seems that your devices do not support Mifare cards ( http://www.shopnfc.it/en/content/7-nfc-device-compatibility ), do you have others NFC Tags to test?

You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com//issues/10#issuecomment-248753945, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJLoKU2XEoEfOGD8iTyz1bn8dtG2qCk7ks5qsacggaJpZM4KCwUP.

@empaaa
Copy link
Author

empaaa commented Sep 21, 2016

A question. When i use NFC Reader from Play store. i can scan nfc tags with all my devices.. why cant the NFCForms do it?

Skickat från min Samsung Galaxy-smartphone.

-------- Originalmeddelande --------
Från: Alessandro Pozone notifications@github.com
Datum: 2016-09-21 23:52 (GMT+01:00)
Till: poz1/NFCForms NFCForms@noreply.github.com
Kopia: empaaa empa_ortberg@hotmail.com, Author author@noreply.github.com
Rubrik: Re: [poz1/NFCForms] Cant scan NFC tag (#10)

Unfortunately seems that your devices do not support Mifare cards ( http://www.shopnfc.it/en/content/7-nfc-device-compatibility ), do you have others NFC Tags to test?

You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com//issues/10#issuecomment-248753945, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJLoKU2XEoEfOGD8iTyz1bn8dtG2qCk7ks5qsacggaJpZM4KCwUP.

@empaaa
Copy link
Author

empaaa commented Sep 22, 2016

The typer of tag is Mifare ultralight and is working on all The devices im using. Does your code work for this type of tag?

Skickat från min Samsung Galaxy-smartphone.

-------- Originalmeddelande --------
Från: Alessandro Pozone notifications@github.com
Datum: 2016-09-21 23:52 (GMT+01:00)
Till: poz1/NFCForms NFCForms@noreply.github.com
Kopia: empaaa empa_ortberg@hotmail.com, Author author@noreply.github.com
Rubrik: Re: [poz1/NFCForms] Cant scan NFC tag (#10)

Unfortunately seems that your devices do not support Mifare cards ( http://www.shopnfc.it/en/content/7-nfc-device-compatibility ), do you have others NFC Tags to test?

You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com//issues/10#issuecomment-248753945, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJLoKU2XEoEfOGD8iTyz1bn8dtG2qCk7ks5qsacggaJpZM4KCwUP.

@poz1
Copy link
Owner

poz1 commented Sep 22, 2016

Yes, it should. But you're filtering for Classic only. Try adding MIfareUltralight in

new string[] {
NFCTechs.MifareClassic, NFCTechs.MifareUltralight
},

@empaaa
Copy link
Author

empaaa commented Sep 22, 2016

Will try it now and see what happens :)

@empaaa
Copy link
Author

empaaa commented Sep 22, 2016

My main goal is to read the tag and receive the Id of the NFC tag. Nothing more, dont need anything else to happen, no opening of intents and so on.

@empaaa
Copy link
Author

empaaa commented Sep 22, 2016

Nop, still nothing happens.. :(

@poz1
Copy link
Owner

poz1 commented Sep 22, 2016

Is it possible that the card is not Ndef formatted?
Try setting the two filter arrays at null

@empaaa
Copy link
Author

empaaa commented Sep 22, 2016

protected override void OnResume()
        {
            base.OnResume();
            if (NFCdevice != null)
            {
                var intent = new Intent(this, GetType()).AddFlags(ActivityFlags.SingleTop);
                NFCdevice.EnableForegroundDispatch
                (
                    this,
                    PendingIntent.GetActivity(this, 0, intent, 0),
                    new[] { new IntentFilter(NfcAdapter.ActionTechDiscovered) },
                    null
                    //new String[][] 
                    //{
                    //    //new string[] {
                    //    //    NFCTechs.Ndef,
                    //    //},

                    //    //new string[] {
                    //    //    NFCTechs.MifareClassic,NFCTechs.MifareUltralight
                    //    //},
                    //}
                );
            }
        }

This is what ive change in the code, is this what you meant by setting filters to null?

@empaaa
Copy link
Author

empaaa commented Sep 22, 2016

When im using Google Play Store app "NFC Reader" and i scan a nfc tag, it says following in the phone:
Technologies: NfcV, NdefFormatable.

Dont know if this changes anything of your thoughts..

@poz1
Copy link
Owner

poz1 commented Sep 22, 2016

O then try using:

new string[] {
NFCTechs.MifareClassic, NFCTechs. NfcV
},

@empaaa
Copy link
Author

empaaa commented Sep 22, 2016

protected override void OnResume()
        {
            base.OnResume();
            if (NFCdevice != null)
            {
                var intent = new Intent(this, GetType()).AddFlags(ActivityFlags.SingleTop);
                NFCdevice.EnableForegroundDispatch
                (
                    this,
                    PendingIntent.GetActivity(this, 0, intent, 0),
                    new[] { new IntentFilter(NfcAdapter.ActionTechDiscovered) },
                    new String[][]
                    {
                        new string[] {
                            NFCTechs.Ndef, NFCTechs. NfcV
                        },

                        new string[] {
                            NFCTechs.MifareClassic,NFCTechs.MifareUltralight
                        },
                    }
                );
            }
        }

Is this what you mean, i guess i still need to have "NFCTechs.MifareUltralight" in the second array?
You wrote "NFCTechs.MifareClassic, NFCTechs. NfcV" in the same array, they should be in diffrent arrays shouldnt they?

This still didnt work though, either of those solutions

@empaaa
Copy link
Author

empaaa commented Sep 22, 2016

Funny thing, When i have the "NFC Reader" app installed, and running my app, then i scan a NFC tag, it opens "NFC Reader" as it is registered as a NFC reader app, and mine isnt. Is this only because i just now cant get the code right, or do i need to do any settings to mark the app as a NFC reader app (more than the permission in manifest)?

@empaaa
Copy link
Author

empaaa commented Sep 22, 2016

protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
            x.OnNewIntent(this, intent);
        }

This method doesnt seem to be called at any time at all. Doesnt this method apply the NFC logic to the new entered view in Xamarin.Forms?

@poz1
Copy link
Owner

poz1 commented Sep 22, 2016

Yep, it shouldn't open other apps if yours it's properly configured :)

SO basically, each of the inside arrays is considered independently and the activity is considered a match is any single inside array matches the tag that was discovered. This provides AND and OR semantics for filtering desired techs.

It's my fault I thought it was OR and then AND :)

Try with:
new String[][]
{
new string[] {
NFCTechs. NfcV
}
}

@poz1
Copy link
Owner

poz1 commented Sep 22, 2016

OnNewIntent doesn't get called because the intent doesn't mach any filter

@empaaa
Copy link
Author

empaaa commented Sep 22, 2016

Houston, we got a connection.
Im receiving it now. Great work! Still some minor issues with it not responding every time, but i will look into that myself, and if i cant figure it out, i will reach out for you :)

Thanks ALOT for the help!

@poz1
Copy link
Owner

poz1 commented Sep 22, 2016

U're welcome :)

@poz1 poz1 closed this as completed Sep 22, 2016
@empaaa
Copy link
Author

empaaa commented Sep 22, 2016

Do you have any code that turns that Tag Id byte array to a string?
Ive getting a array with numbers, but the id of the tag should be: 16142028408482000659
Im trying to use some regular array to string methods, but they just convert it into some random character string...
I need the real Id of the Tag.

@yacine-karim
Copy link

yacine-karim commented Dec 13, 2016

Hello
@empaaa how can get the id from the string array
from these lines :

        if (NFCdevice != null)
        {
            var intent = new Intent(this, GetType()).AddFlags(ActivityFlags.SingleTop);
            NFCdevice.EnableForegroundDispatch
            (
                this,
                PendingIntent.GetActivity(this, 0, intent, 0),
                new[] { new IntentFilter(NfcAdapter.ActionTechDiscovered) },
                new String[][]
                {
                    new string[] {
                        NFCTechs.Ndef, NFCTechs. NfcV
                    },

                    new string[] {
                        NFCTechs.MifareClassic,NFCTechs.MifareUltralight
                    },
                }
            );
        }

@edwito86
Copy link

edwito86 commented Aug 7, 2019

Yep, it shouldn't open other apps if yours it's properly configured :)

SO basically, each of the inside arrays is considered independently and the activity is considered a match is any single inside array matches the tag that was discovered. This provides AND and OR semantics for filtering desired techs.

It's my fault I thought it was OR and then AND :)

Try with:
new String[][]
{
new string[] {
NFCTechs. NfcV
}
}

Esta es la solución al problema cuando no llama el OnNewIntent.
GRacias

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants