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

Android: Scan NFC tag id and just print it when button is clicked #13

Closed
yacine-karim opened this issue Dec 13, 2016 · 2 comments
Closed

Comments

@yacine-karim
Copy link

Hello,
thank you for this package,
im trying this sample for an Android application
im just trying to get the stored id from the NFC card and print it on screen
so that i can use later ...
when i scan a card nothing happens, and i don't know how to customize the code too scan the card when the button is clicked.
I followed excactly the instructions :
Thank you

  1. i added the NFC capability in the manifest AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="NFC_NDEF.NFC_NDEF" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="16" />
    <uses-permission android:name="android.permission.NFC" />
    <uses-sdk android:targetSdkVersion="15" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />
<application android:label="NFC_NDEF"></application>
</manifest>
  1. In MainActivity.cs i added
public NfcAdapter NFCdevice;
public NfcForms x;
  1. In the OnCreate method, before LoadApplication(), i added
NfcManager NfcManager = (NfcManager)Android.App.Application.Context.GetSystemService(Context.NfcService);       
NFCdevice = NfcManager.DefaultAdapter;

Xamarin.Forms.DependencyService.Register<INfcForms,NfcForms>();
x = Xamarin.Forms.DependencyService.Get<INfcForms>() as NfcForms;
  1. After OnCreate() i added
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 i don't want my app to be lauched when a compatible tag is scanned so i didn't add these lines 5 and 6 :

[Activity (Label = "NFCForms.Droid", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
[IntentFilter (new[]{NfcAdapter.ActionTechDiscovered})]
[MetaData (NfcAdapter.ActionTechDiscovered, Resource="@xml/nfc")]
 <activity>
...
<intent-filter>
    <action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>

<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
    android:resource="@xml/nfc_tech_filter" />
...
</activity>
@poz1
Copy link
Owner

poz1 commented Mar 13, 2017

Hello, unfortunately because of NFC works, you can't scan in a precise moment, you've to start the sensor and wait for it to scan.

You can do this subscribing to this event:


INfcForms device = DependencyService.Get<INfcForms>();
device.NewTag += HandleNewTag;

@yacine-karim
Copy link
Author

Thank you so much this really helped me, i finished my application

@poz1 poz1 closed this as completed Mar 23, 2018
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

2 participants