Cant scan NFC tag
empaaa opened this issue · 24 comments
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
Hey, which device are you testing on?
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.
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?
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.
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.
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.
Yes, it should. But you're filtering for Classic only. Try adding MIfareUltralight in
new string[] {
NFCTechs.MifareClassic, NFCTechs.MifareUltralight
},
Will try it now and see what happens :)
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.
Nop, still nothing happens.. :(
Is it possible that the card is not Ndef formatted?
Try setting the two filter arrays at null
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?
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..
O then try using:
new string[] {
NFCTechs.MifareClassic, NFCTechs. NfcV
},
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
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)?
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?
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
}
}
OnNewIntent doesn't get called because the intent doesn't mach any filter
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!
U're welcome :)
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.
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
},
}
);
}
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