-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Welcome to the NfA wiki!
Include the necessary dependencies to your pom project.
The maven artifacts are available on Oss Sonatype
The mandatory projects are :
- nfa-core
- nfa-api
- nfa-records
- nfa-writers
- nfa-parsers
Here is the dependencies you could add to your pom
<dependency>
<groupId>com.github.nfcforandroid</groupId>
<artifactId>nfa-api</artifactId>
<version>0.7.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.nfcforandroid</groupId>
<artifactId>nfa-records</artifactId>
<version>0.7.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.nfcforandroid</groupId>
<artifactId>nfa-writers</artifactId>
<version>0.7.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.nfcforandroid</groupId>
<artifactId>nfa-parsers</artifactId>
<version>0.7.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.nfcforandroid</groupId>
<artifactId>nfa-filters</artifactId>
<version>0.7.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.nfcforandroid</groupId>
<artifactId>nfa-core</artifactId>
<version>0.7.0-SNAPSHOT</version>
</dependency>For the apklib dependency, you will have to integrate the project in your ide and add the following dependencies to your pom. Download the zip "nfa-apklib" available in the download section
<dependency>
<groupId>com.github.nfcforandroid</groupId>
<artifactId>nfa-apklib</artifactId>
<version>${project.version}</version>
<type>apklib</type>
</dependency>
<dependency>
<groupId>com.github.nfcforandroid</groupId>
<artifactId>nfa-apklib</artifactId>
<version>${project.version}</version>
<type>jar</type>
<scope>provided</scope>
</dependency>You also need to update your android project to have a dependency to nfa-apklib project. Android Library
You need to download the jar you need to the libraries of your project. The jar could be found All in One
Download the zip "nfa-apklib" available here ApkLib, add it to your workspace and update your android project to have a dependency to nfa-apklib project. Android Library
As NfA is a lightweight library, this library take the choice to not based it's main integration arround a Super Class so all is orchestered arround a NFA_MANAGER. This object is a central singleton object that you will use in your application. This object will helps you with lots of helpers.
First of all, you have to register your activity with the NFA_MANAGER. As NFC appeared with Gingerbread, you will find two version of NFA_MANAGER, the first is available for GINGERBREAD and above, the second is available for ICE CREAM SANDWICH and above. The difference between the 2 version are localised around the way you will push informations to push and the way you will manage the NFA_MANAGER with your lifecycle.
-
In the version Gingerbread, you will use deprecated (since API14) methods and you will have to manage on your own the life cycle of your activity with the NFA_MANAGER
-
In the version Ice Cream Sandwich version, you won't have to manage the life cycle anymore !
import static com.github.nfcforandroid.v14.NfaFactory.*;
/**
* @author jefBinomed Nfa activity
*/
public class NfaActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
// We register our activity to the NFA Manager
// We give to the manager the orginal
NFA_MANAGER.register(this);
}
...That's all !
API 9 : Life Cycle managment
You have to manage life cycle in order to optimize memory footprint
@Override
protected void onPause() {
NFA_MANAGER.pause(this);
super.onPause();
}
@Override
protected void onResume() {
NFA_MANAGER.resume(this);
super.onResume();
}
@Override
protected void onStop() {
NFA_MANAGER.stop(this);
super.onStop();
} /**
* Use this method when you want to register your activity to nfc intents and when you want to read the first intent launch at the start of your activity and when you want to use Beam mode (SNEP)
*
* Recommand for read / beam nfc activity
*
* @param activity
* @param recieveConfiguration
* @param filters
*/
<Record extends INfaRecord> void register(Activity activity, NfaRecieveBean<Record> recieveConfiguration, INfaBeam<Record> beamWriter, INfaIntentFilter... filters);
/**
* Use this method when you want to register your activity to nfc intents and when you want to read the first intent launch at the start of your activity
*
* Recommand for read nfc activity
*
* @param activity
* @param recieveConfiguration
* @param filters
*/
<Record extends INfaRecord> void register(Activity activity, NfaRecieveBean<Record> recieveConfiguration, INfaIntentFilter... filters);
/**
* Use this method when you want to register your activity to nfc intents by specifying the filters and when you want to use beam mode (SNEP)
*
* Recommand for write / beam nfc activity
*
* @param activity
* @param filters
*/
<Record extends INfaRecord> void register(Activity activity, INfaBeam<Record> beamWriter, INfaIntentFilter... filters);
/**
* Use this method when you want to register your activity to nfc intents by specifying the filters
*
* Recommand for write nfc activity
*
* @param activity
* @param filters
*/
void register(Activity activity, INfaIntentFilter... filters);
/**
* Use this method when you want to use beam mode (SNEP)
*
* Recommand for beam nfc activity
*
* @param activity
* @param filters
*/
<Record extends INfaRecord> void register(Activity activity, INfaBeam<Record> beamWriter);If you want to read you have two way for registering your activity
/**
* Use this method when you want to register your activity to nfc intents and when you want to read the first intent launch at the start of your activity and when you want to use Beam mode (SNEP)
*
* Recommand for read / beam nfc activity
*
* @param activity
* @param recieveConfiguration
* @param filters
*/
<Record extends INfaRecord> void register(Activity activity, NfaRecieveBean<Record> recieveConfiguration, INfaBeam<Record> beamWriter, INfaIntentFilter... filters);
/**
* Use this method when you want to register your activity to nfc intents and when you want to read the first intent launch at the start of your activity
*
* Recommand for read nfc activity
*
* @param activity
* @param recieveConfiguration
* @param filters
*/
<Record extends INfaRecord> void register(Activity activity, NfaRecieveBean<Record> recieveConfiguration, INfaIntentFilter... filters);- First you have to setting up a NfaRecieveBean that will helps you to define the callback method for recieving the wanted record.
The Record wildcard represent a subtype of INfaRecord. If you want to retrieve severals records types, you could declare a NfaRecieveBean. Else you will define the wanted type.
- Secondly, you have to pass the list of intent filters corresponding to your tag in order to let the framework detect your tag.
import static com.github.nfcforandroid.api.beans.NfaRecieveBean.*;
import static com.github.nfcforandroid.filters.factory.NfaFiltersFactory.*;
import static com.github.nfcforandroid.parser.factory.NfaParserFactory.*;
import static com.github.nfcforandroid.v14.NfaFactory.*;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read);
// We register our activity to the NFA Manager
// We give to the manager the orginal
NFA_MANAGER.register(this //
, recieveBeanConfigure() //
.activity(this) //
.intent(getIntent()) //
.intentRecieveRecord(this) //
.parser(NDEF_PARSER) //
.build() //
, NDEF_FILTER //
, TEXT_FILTER //
, externalFilters().textExternalNdefFilter(NfaSampleCst.TYPE_EXTERNAL) //
);
}In this example, I will recieve all types of Records.
- The method activity(this) will indicate which is the activity used for the intent. It is manadatory because it will helps the frameworks to link the INfaIntentFilters to the parsers !
- The method intent(getIntent) will indicates that I will treat directly the message if the activiies is open after an intent filter
- The method intentRecieveRecord(this) sepcify the callback implementation where the transformed record arrived in. In our case, the Activitiy implements the interface INfaIntentRecieveRecord
- The method parser(NDEF_PASRER) specify the wanted parser to convert the android ndef record to an INnfaRecord
- We gives after the differents FILTER we want to use or detect during the activity life.
When one of your filter is fired to your activities, you arrived in the method Activity.onNewIntent(Intent intent). You only have to delegate the parsing to the NFA_MANAGER.
@Override
protected void onNewIntent(Intent intent) {
/*
* Manadatory
*/
NFA_MANAGER.manageIntent(recieveBeanConfigure() //
.activity(this) //
.intent(intent) //
.intentRecieveRecord(this) //
.parser(NDEF_PARSER) //
.build());
}As for the registration, we have to put similar informations (minus the filters).
As said before, to read the data, we have to implement a callback interface : INfaIntentRecieveRecord. This interface provides you a single method that have as parameter the parse record. You just have to specify when you want to read a data, an implementation of this interface with the expected type and you will recieve your data in the correct format :
/**
* @author jefBinomed Read activity
*/
public class NfaReadActivity extends SherlockFragmentActivity implements INfaIntentRecieveRecord<INfaRecord> {
/**
*
* Nfa methods
**/
@Override
public void recieveRecord(INfaRecord record) {
tag_content_img.setVisibility(View.GONE);
if (record instanceof TextRecord) {
String message = ((TextRecord) record).getText();
tag_content.setText(getString(R.string.tag_content) + message);
} else if (record instanceof TextExternalRecord) {
String message = ((TextExternalRecord) record).getMessage();
tag_content.setText(getString(R.string.tag_content) + message);
} else if (record instanceof UriRecord) {
String message = ((UriRecord) record).getUri().toString();
tag_content.setText(getString(R.string.tag_content) + message);
} else if (record instanceof SmartPosterRecord) {
SmartPosterRecord spRecord = ((SmartPosterRecord) record);
String uri = spRecord.getUri().getUri().toString();
String title = spRecord.getTitle() != null ? spRecord.getTitle().getText() : null;
tag_content.setText(getString(R.string.tag_content) + (title != null ? title + " : " : "") + uri);
} else if (record instanceof MimeTypeRecord) {
tag_content.setVisibility(View.GONE);
tag_content_img.setVisibility(View.VISIBLE);
MimeTypeRecord mimeTypeRecord = ((MimeTypeRecord) record);
ByteArrayInputStream is = new ByteArrayInputStream(mimeTypeRecord.getData());
tag_content_img.setImageBitmap(BitmapFactory.decodeStream(is));
try {
is.close();
} catch (IOException e) {
// TODO
}
} else {
tag_content.setText(R.string.tag_content);
}For this example, I use the most generic case, but you could be more precise
/**
* @author jefBinomed Read activity
*/
public class NfaReadActivity extends SherlockFragmentActivity implements INfaIntentRecieveRecord<TextRecord> {
/**
*
* Nfa methods
**/
@Override
public void recieveRecord(TextRecord record) {
String message = record.getText();
}The only constraint you have is that the paser have to be the same type as the INfaIntentRecieveRecord !
If you want to write you have one way for registering your activity
/**
* Use this method when you want to register your activity to nfc intents by specifying the filters and when you want to use beam mode (SNEP)
*
* Recommand for write / beam nfc activity
*
* @param activity
* @param filters
*/
<Record extends INfaRecord> void register(Activity activity, INfaBeam<Record> beamWriter, INfaIntentFilter... filters);
/**
* Use this method when you want to register your activity to nfc intents by specifying the filters
*
* Recommand for write nfc activity
*
* @param activity
* @param filters
*/
void register(Activity activity, INfaIntentFilter... filters);
- You have to pass the list of intent filters corresponding to your tags in order to let the framework detect your tag.
import static com.github.nfcforandroid.filters.factory.NfaFiltersFactory.*;
import static com.github.nfcforandroid.v14.NfaFactory.*;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NFA_MANAGER.register(this //
, NDEF_FILTER //
);
}In this example, I will write on all type of Ndef message.
When one of your filter is fired to your activities, you arrived in the method Activity.onNewIntent(Intent intent). You will at this moment ask to the NFA_MANAGER to write the datas on your tag.
@Override
protected void onNewIntent(Intent intent) {
/*
* Manadatory
*/
NFA_MANAGER.writeTag(getApplicationContext(), //
intent, // the intent with the information of the tag to write
this, // the callback method
true, // addAndroidApplicationRecord
NfaWriteBean.writeBeanConfigure() //
.writer(TEXT_WRITER) //
.record(NfaRecordFactory.wellKnowTypeFactory().textRecordInstance("Text to write"))
.build());
}We have severals informations here :
- The intent : the intent containing the the information of the tag to write
- The callback interface : an implementation of INfaIntentWrite. It is just a callback interface
- A boolean which indicates if you will include to the tag the AndroidApplicationRecord of your application or not
- As many writers as you want records. The parameters accept a NfaWriteBean. For these bean you have to specify the record (data) and the writer (way to write it).
An alternive way exist for configuring a NfaWriteBean
TEXT_WRITER.init(NfaRecordFactory.wellKnowTypeFactory().textRecordInstance("Text to write"));
NfaWriteBean.writeBeanConfigure() //
.writer(TEXT_WRITER) //
.build());
}If you use the second way to initialise your INfaWriter, you have to be conscious that severals INfaWriter are singleton instance !
If you want to write you have one way for registering your activity
/**
* Use this method when you want to use beam mode (SNEP)
*
* Recommand for beam nfc activity
*
* @param activity
* @param filters
*/
<Record extends INfaRecord> void register(Activity activity, INfaBeam<Record> beamWriter);
- You just have to specify how you want to give information through beam
import static com.github.nfcforandroid.v14.NfaFactory.*;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NFA_MANAGER.register(this // Activity
, this // INfaBeam<Record>
);
}In this example, The activity implement the INfaBeam interface
When you implement INfaBeam, you have to implement 3 methods which gives all the information needed for the beam transfert.
/**
*
* NfA methods
**/
@SuppressWarnings("unchecked")
@Override
public List<NfaWriteBean<INfaRecord>> getWriters() {
return Arrays.asList(NfaWriteBean.writeBeanConfigure() //
.writer(TEXT_WRITER) //
.record(NfaRecordFactory.wellKnowTypeFactory().textRecordInstance("Text to write")) //
.build());
}
@Override
public void beamCallBack() {
// Feedback, we only passed here when the transfert was complete;
}
@Override
public boolean addAndroidApplicationRecord() {
return true;
}- The first method request a list of NfaBeanWriters which represents the list of records exanges during the beam.
- The second method allow you to know that your beam message was well transfered
- The third method is used to known if we have to include the AndroidApplicationRecord of your application in the beam message
With the api 9, the getWriters() method is called at the start of the activity.
With the api 14, the getWriters() method is called each times, you initiate a beam action !
If you want more example, you could look at the nfa-samples project.