Giter Site home page Giter Site logo

android-nfc-lib's Introduction

Android NFC Library

What you'll need when using this library :

Android ver
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
	public class MyActivity{
	    private PendingIntent pendingIntent;
    	private IntentFilter[] mIntentFilters;
   		private String[][] mTechLists;
    	private NfcAdapter mNfcAdapter;

  	 	protected void onCreate(Bundle savedInstanceState) {
        	super.onCreate(savedInstanceState);
        	setContentView(R.layout.activity_main);
        	mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        	pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        	mIntentFilters = new IntentFilter[]{new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED)};
        	mTechLists = new String[][]{new String[]{Ndef.class.getName()},
                    new String[]{NdefFormatable.class.getName()}};
                        	}
		public void onResume(){
			super.onResume();
    		if (mNfcAdapter != null) {
       			mNfcAdapter.enableForegroundDispatch(this, pendingIntent, mIntentFilters, mTechLists);
    		}
    	}
    	public void onPause(){
    		super.onPause();
    		if (mNfcAdapter != null)
        	{
            	mNfcAdapter.disableForegroundDispatch(this);
        	}
        }
	}
  • Or extend from NfcActivity:
	public class MyActivity extends NfcActivity{    	
    	protected void onCreate(Bundle savedInstanceState){
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    	}
    }
  • In both cases, add the following to your AndroidManifest.xml file :
	 <uses-permission android:name="android.permission.NFC" />

Start Reading

  • Paste this in the activity if you're extending our class :
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
    	for (String message : getNfcMessages()){
       		Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
    	}
	}
  • Otherwise :
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        SparseArray<String> res = new NfcReadUtilityImpl().readFromTagWithSparseArray(intent);
        for (int i =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 :
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        for (String message : new NfcReadUtilityImpl().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:
    @Override
    public void callbackWithReturnValue(Boolean result) {
        String message = result ? "Success" : "Failed!";
        Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProgressUpdate(Boolean... booleans) {
        Toast.makeText(this, booleans[0] ? "We started writing" : "We could not write!",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onError(Exception e) {
        Toast.makeText(this,e.getMessage(),Toast.LENGTH_SHORT).show();
    }
  • Create a field with an AsyncOperationCallback in the following way :
	AsyncOperationCallback mAsyncOperationCallback = new AsyncOperationCallback() {

        @Override
        public boolean performWrite(NfcWriteUtility writeUtility) throws ReadOnlyTagException, InsufficientCapacityException, TagNotPresentException, FormatException {
            return writeUtility.writeEmailToTagFromIntent("[email protected]","Subject","Message",getIntent());
        }
    };
  • Override the onNewIntent(Intent) method in the following way :
	@Override
	protected void onNewIntent(Intent intent) {
	    super.onNewIntent(intent);
	    new WriteEmailNfcAsync(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.

android-nfc-lib's People

Contributors

bartvandeweerdt avatar daneov avatar vangeb 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  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  avatar  avatar  avatar  avatar

android-nfc-lib's Issues

First char in message

The message is received without the first character.
For example:
I sent -> "Beam me up, Android!" -> I receive -> "eam me up, Android!"

problem in an application with fragment

hi,
when i use your library in my program i have to set onNewIntent in MainActivity and when i want to write tag i should access this from fragment...... i solved some of small problem by providing middle class and transfer some data between MainActivity and fragment .... but i encounter with this :
public boolean performWrite(NfcWriteUtility writeUtility) throws ReadOnlyTagException,InsufficientCapacityException, TagNotPresentException, FormatException { return writeUtility.writeTelToTagFromIntent(text,LogHolder.fragment_1_activity.getIntent()); }
and as you can see i provide MainActivity's activity by that middle class(LogHolder) but i get error :
Encountered an error ! be.appfoundry.nfclibrary.exceptions.TagNotPresentException: Intent does not contain a tag

i stock here i can't do more ..... help me plz :))

Side effects on Google Pay possible?

Hi, first thanks for this library. I was able to implement reading/writing NFC tags in very short time with it ๐Ÿ˜

Lately I had trouble using my smartphone on a payment terminal, where I wanted to pay with Google Pay. Could it be that Google Pay doesn't work when an app is on background which uses android-nfc-lib? My smartphone didn't react on the payment terminal, I tried multiple times. After I uninstalled the app the payment was successful.

I'm using the "extend NfcActivity" way. You may want to have a look at my sources: MainActivity.java, AndroidManifest.xml.

I do not have too much of a clue of the technical side here (that's why I'm using this library). Maybe you have an explanation for this?

I have a problem with @Override

@OverRide
public void callbackWithReturnValue(Boolean result) {
String message = result ? "Success" : "Failed!";
Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
}

@Override
public void onProgressUpdate(Boolean... booleans) {
    Toast.makeText(this, booleans[0] ? "We started writing" : "We could not write!",Toast.LENGTH_SHORT).show();
}

@Override
public void onError(Exception e) {
    Toast.makeText(this,e.getMessage(),Toast.LENGTH_SHORT).show();
}

In this code @OverRide doesnt work (my IDE doesnt let me do it)

Erase Tag to factory default

Can you tell me if it is possible to erase tag using that lib? I mean also not to write for example empty String.

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.