Coder Social home page Coder Social logo

nfcforms's Introduction

NFCForms

Easily read and write NFC tags in your Xamarin.Forms application. XNFCForms is an example app that you can download to see how it works.

##Installation

Install from Nuget NFCForms ( http://www.nuget.org/packages/NFCForms/ )

###Android 1: Add the NFC capability in the manifest. 2: In MainActivity.cs add

public NfcAdapter NFCdevice;
public NfcForms x;

3: In the OnCreate method, before LoadApplication(), add

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;

4: after OnCreate() add

    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);
		}

Here we are asking Android to notify us when an NFC tag is available. We can choose wich kind of tags we want to be notified setting the parameeter of the IntentFilter. In this case we will receive tags supporting NDEF or MifareClassic. More info here ( http://developer.android.com/guide/topics/connectivity/nfc/nfc.html ).

5: If we want our app to be lauched when a compatible tag is scanned add

[Activity (Label = "NFCForms.Droid", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
[IntentFilter (new[]{NfcAdapter.ActionTechDiscovered})]
[MetaData (NfcAdapter.ActionTechDiscovered, Resource="@xml/nfc")]

and an XML file where you set the kind of NFC tag you want to open your app (there is one in the example in Resources/xml/nfc.xml). This code is the equivalent of adding these lines

<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>

in the manifest file. Again, more info here ( http://developer.android.com/guide/topics/connectivity/nfc/nfc.html )

###Windows Phone

1: Add NFC capability to the manifest

2: In MainPage.xaml.cs add

public NfcForms x;

3: before LoadApplication() add

Xamarin.Forms.DependencyService.Register<INfcForms, NfcForms>();
x = Xamarin.Forms.DependencyService.Get<INfcForms>() as NfcForms;

On WindowsPhone only NDEF tags are supported so there is no need to specify technologies.

##Usage ###Read Tag We call the DependecyService to get the NFC Reader and we register to the NewTag event:

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

then when a compatible tag is scanned the event is fired and we receive an NfcFormsTag. In NfcFormsTag.NdefMessage we have the NDEF message. Using NDEF Library ( http://github.com/mopius/ndef-nfc ) we can read the content.

###Write Tag we use

device.WriteTag(msg);

where msg is an NDEFMessage that, again, you can create using NDEF Library. Please note that this method is assuming the tag to be in place when it is called.

##Credits

Written by Alessandro Pozone - www.POZ1.com thanks to the guys behind NDEF Library, without their project this would have been much more limited.

nfcforms's People

Contributors

marcoscobena avatar neoncube2 avatar poz1 avatar stephannielsen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nfcforms's Issues

I can't scan NFC by asus nexus 7

I use the NFCForms in my project. I try to Installer the app for two phones. one is sony Z5, the other is nexus 7. I can use fine in sony Z5, but I can't scan in nexus 7.
nexus 7 doesn't happen in OnPause(), Why?
I try to find NFCtools, I can scan MifareClassic card in sony Z5 and nexus 7.

No se puede leer NFCV, ayuda

eh logrado hacer la conexion
NFCdevice.EnableForegroundDispatch
(
this,
PendingIntent.GetActivity(this, 0, intent, 0),
new[] { new IntentFilter(NfcAdapter.ActionTechDiscovered) },
new String[][] {new string[] {
NFCTechs.NfcV,
NFCTechs.NdefFormatable
}
}
);

pero en el message llega vacio y tampoco me puedo acceder a las características.

Crash in OnNewIntent when restarting the app.

Has to do with #23

Platform:
Xamarin Forms 3.4.0.1009999
Xamarin Android Targeting Android 8.1 API 27 Minimum API 21

What happens:
App crashes on restart.

Steps to reproduce.

Create A blank PCL project.
Add NFCForms
Build App
Open app on device.
Go back to device home screen by pressing the home key of your device.
Reopen the app from the app drawer.
Exception occurs.

So im writing this as an issue even though i know the solution.
It has to do with the check on line 92 in this file

For some reason it thinks droidTag has a value on a regular restart of the app.
However if you move the check to your MainActivity it sees it as null as expected.
MainActivity

protected override void OnNewIntent(Intent intent)
{
            base.OnNewIntent(intent);
            //Check that normally occurs in line 92
            var TestIntent = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
            if (TestIntent != null)
            {
                x.OnNewIntent(this, intent);
            }


}

For now this fixes it however its not the prettiest of solutions.

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

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>

error when installing for windows phone

Could not install package 'NFCForms 2.3.0'. You are trying to install this package into a project that targets 'WindowsPhoneApp,Version=v8.1', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

Can't install package on portable project

I was trying to use this library but I get this error
cattura

Could not install package 'NFCForms 2.3.0'. You are trying to install this package into a project that targets '.NETPortable,Version=v4.5,Profile=Profile259', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

Sample app not working on Android 4.4, 5.0

Hi,

I've built the sample app and tested on two Android devices: 4.4 and 5.0.
It seems that the OnNewIntent(Intent intent) in MainActivity.cs is not getting called when I attach a card. I've enabled NFC and I could hear the NFC beep sound after attaching.
But it is not always like this. I remember sometimes it could pop up a list of information when I attach a card with an NFC chip.

No error message is thrown.

Thanks.

NfcForms.IsAvailable NullReferenceException on Android-device without NFC

Hi,

I'm getting an System.NullReferenceException: Object reference not set to an instance of an object.
when checking NfcForms.IsAvailable on devices where NFC-hardware isn't available.

For example Samsung Trend GT-S7580 (Android 4.2.2)

Would be nice if false was returned both when NFC is disabled and when device don't have the hardware.

/Richard

problem on download from Nuget

Hi, when I download your nuget package, In PLC project I can't add Poz1.NFCForms.Abstract.
I must add it manually (.dll)
It's correct?

RootFrame_NavigationFailed() reached when running on Windows 10.

When I build and deploy the app to a Windows 10 device, it goes to RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e) in App.xaml.cs and the IDE changes to debugger mode.
The exception message of e is

Could not load file or assembly 'NdefLibrary, Version=3.0.3.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

After clicking continue, it says Your application is in break mode. The exception is TargetInvocationException was unhandled.

Send User Defined values Between devices in NFC with Xamarin.Forms

We need to pass the values (IMEI No,etc..,) between the Android devices from Xamarin.Forms app with custom renderer approach.

In Existing code, we used Android beam to transfer the data/values between devices through app. But now, we are not able to send data/values between devices in Xamarin.Forms.

In this component , the URL is passed between the devices but we need to know how send the user defined data between devices?

Xamarin Android working Sample Code

Kindly find the screenshot here

Send data from one phone to another using this component

Hello,

I'm a bit lost here. Everywhere in this component we are talking about reading and writing tags. This is not what I am trying to do. I want to be able to share data (just an identifier) from one phone to another using nfc. I want to be able to push (for instance) a button, and then let the user know to hold his phone against another phone to transfer the data (in this case, an identifier to a document open in our app) and use the received data on the other phone to show the same document.

Is there anywhere i can see an example of how this works? Or is this not possible at all using this component?

Getting NFC tags real Id, not the byte array

Is there a way to get a Tags Id as a string?
In the object "NfcFormsTag" i am getting the Id, but its a byte[]..
I would want the real UID from the tag. A 20 digit number.

This was our latest diskussion, if u wanna know the implementation, but i think u remember it :)
#10

Tag Error: No NDEF message found o NDEF not supported

Hi,

I'm getting TAG Error. Can you please help on this error?
Please find the details of the error.

Tag Error: No NDEF message found o NDEF not supported

at Poz1.NFCForms.Droid.NfcForms.ReadNdef (Android.Nfc.Tech.Ndef ndef) [0x00014] in :0
at Poz1.NFCForms.Droid.NfcForms.OnNewIntent (System.Object sender, Android.Content.Intent e) [0x0007c] in :0
at NFCApp.Droid.MainActivity.OnNewIntent (Android.Content.Intent intent) [0x00007] in :0
at Android.App.Activity.n_OnNewIntent_Landroid_content_Intent_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_intent) [0x00011] in <296d0ee83a1a4bffb8ba0a81b8bcee24>:0
at (wrapper dynamic-method) System.Object:4e767881-8b5b-417e-a598-da63a31751d0 (intptr,intptr,intptr)

Thanks in Advance.

NfcForms OnNewIntent EmptyTag

I try the code example in README, when i try to read an empty Tag, x.OnNewIntent(this, intent); throw this exception System.Exception: Tag Error: No NDEF message found o NDEF not supported.

My application have to react to an empty tag (to write it).

NFC NTAG213

Hi Alessandro

Can I use you software to read and write to a specific block of memory on an NFC NTAG213 fob?

The Fob is classified as Mifare Ultralite.

If I can what do I put in the Intent?

Thanks

Cant scan NFC tag

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

Could not install package NFCForms 2.3.0

Install-Package : Could not install package 'NFCForms 2.3.0'. You are trying to install this package into a project that targets '.NETPortable,Version=v4.5,Profile=Profile111', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

Corrupt Package

I am trying to install the latest version (2.3.1) but I am getting the error

Unable to read package from path 'NFCForms.2.3.1\NFCForms.2.3.1.nupkg
File contains corrupted data.

Any ideas?

hello

Hello, first of all i want to thank you for the great contribution, it's a great project.
I have only one issue, when i try to run the project i get this error :

Error 1 error: cannot find symbol
mono.android.Runtime.notifyTimeZoneChanged ();
symbol: method notifyTimeZoneChanged()
location: class Runtime
C:\Users\Alex\Desktop\NFCForms-master\Example\NFCFormsSample\NFCFormsSample.Droid\obj\Debug\android\src\mono\android\app\Application.java 23 24 NFCFormsSample.Droid

Any idee what is wrong ? I am using VS 2013 SP on the Droid version.

Thank you

NewTag event Delegates

Hi Alessandro, I'm subscribing to the NewTag event in the Page constructor, so, every time
I open that page a new subscription is done, and the event it's fired X times. No matter if I unsubscribe from the event, the delegate collection is always increasing.

untitled2

untitled

Thanks

Nuget out of date

Are there any plans to update the nuget packages in the near future, we are trying to read the serial numbers of unwritten tags and failing currently. I notice that there have been quite a few commits on this subject since the last official release.

Thank you for a great library.

nfc.xml is missing

Hi,
in the step 5 you said about the XML file avaiable in the example (Resources/xml/nfc.xml).
I haven't found this file.
thks

Not working Latest Android version like (Android 7+)

Hi,

NFCForms works well upto Android version 6 but it's not working in Android Version 7+.
Please sort out this issue ASAP

Not working device detail:
Samsung Galaxy S7 (Android version 7.0)

Thanks in Advance!!

MissingMethodException

Hi,

I am trying to build an app for scanning NFC tags, using this packet.
However when i try to build i get an error on this line of code:

x = Xamarin.Forms.DependencyService.Get<INfcForms>() as NfcForms;

Error:

System.MissingMethodException
Default constructor not found for type Poz1.NFCForms.Droid.NfcForms

Do you have an idea what might cause this?

Can't install package on portable project

I was trying to use this library in my project but I get the following when installing:

Could not install package 'NFCForms 2.3.0'. You are trying to install this package into a project that targets '.NETPortable,Version=v4.5,Profile=Profile111', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

Tried in VS for Windows and Mac

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.