Skip to content

Repository files navigation

Android NFC Library

What you'll need when using this library :

Androidver
API>=16

Quick start

In order to get a quick demo running you could perform the following steps :

  • Add this to the repositories block :
	repositories {
maven{
url "http://maven.appfoundry.be"
}
}
  • Go to your project's build.gradle file, and change the dependencies block to match the following line of code there :
 compile 'be.appfoundry:nfc-lib:1.1'

Now go to the created activity, and either

  • Implement FGD yourself
publicclassMyActivity{
privatePendingIntentpendingIntent;
privateIntentFilter[] mIntentFilters;
privateString[][] mTechLists;
privateNfcAdaptermNfcAdapter;
protectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
pendingIntent = PendingIntent.getActivity(this, 0, newIntent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
mIntentFilters = newIntentFilter[]{newIntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED)};
mTechLists = newString[][]{newString[]{Ndef.class.getName()},
newString[]{NdefFormatable.class.getName()}};
}
publicvoidonResume(){
super.onResume();
if (mNfcAdapter != null) {
mNfcAdapter.enableForegroundDispatch(this, pendingIntent, mIntentFilters, mTechLists);
}
}
publicvoidonPause(){
super.onPause();
if (mNfcAdapter != null)
{
mNfcAdapter.disableForegroundDispatch(this);
}
}
}
  • Or extend from NfcActivity:
publicclassMyActivityextendsNfcActivity{ protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
  • In both cases, add the following to your AndroidManifest.xml file :
 <uses-permissionandroid:name="android.permission.NFC" />

Start Reading

  • Paste this in the activity if you're extending our class :
@OverrideprotectedvoidonNewIntent(Intentintent) {
super.onNewIntent(intent);
for (Stringmessage : getNfcMessages()){
Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
}
}
  • Otherwise :
@OverrideprotectedvoidonNewIntent(Intentintent) {
super.onNewIntent(intent);
SparseArray<String> res = newNfcReadUtilityImpl().readFromTagWithSparseArray(intent);
for (inti =0; i < res.size() ; i++ ) {
Toast.makeText(this, res.valueAt(i), Toast.LENGTH_SHORT).show();
}
}
  • If you like the Map implementation more you might as well use :
@OverrideprotectedvoidonNewIntent(Intentintent) {
super.onNewIntent(intent);
for (Stringmessage : newNfcReadUtilityImpl().readFromTagWithMap(intent).values()) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
  • Now you're able to read the NFC Tags as long as the library supports the data in it when held to your phone!

Write to a tag

  • Let your activity implement AsyncUiCallback:
@OverridepublicvoidcallbackWithReturnValue(Booleanresult) {
Stringmessage = result ? "Success" : "Failed!";
Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonProgressUpdate(Boolean... booleans) {
Toast.makeText(this, booleans[0] ? "We started writing" : "We could not write!",Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonError(Exceptione) {
Toast.makeText(this,e.getMessage(),Toast.LENGTH_SHORT).show();
}
  • Create a field with an AsyncOperationCallback in the following way :
AsyncOperationCallbackmAsyncOperationCallback = newAsyncOperationCallback() {
@OverridepublicbooleanperformWrite(NfcWriteUtilitywriteUtility) throwsReadOnlyTagException, InsufficientCapacityException, TagNotPresentException, FormatException {
returnwriteUtility.writeEmailToTagFromIntent("some@email.tld","Subject","Message",getIntent());
}
};
  • Override the onNewIntent(Intent) method in the following way :
@OverrideprotectedvoidonNewIntent(Intentintent) {
super.onNewIntent(intent);
newWriteEmailNfcAsync(this,mAsyncOperationCallback).executeWriteOperation();
}
  • If you hold a tag against the phone and it is NFC Enabled, your implementation of the methods will be executed.

Android Beam

  • When extending our class, all you have to do in order to enable Android Beam is call the enableBeam() method.

    • This enables Android Beam
    • Provides a default implementation with a standard text
  • If you did not opt for extending, take a look at the official docs, or the source code.

About

NFC library for Android

Resources

Stars

64 stars

Watchers

12 watching

Forks

Releases

Packages

Contributors

Languages