1. A new world is coming…
@fernando_cejas
www.fernandocejas.com
2. Who am I?
• Barcelona GDG Organizer
• NFC, Android and Agile Geek
• Android10.org
• NFC Actions App
• Flojack (@flomio)
• @fernando_cejas
• http://www.fernandocejas.com/
3. Agenda
• What is NFC?
• NFC Features and components
• Common (and not) use cases
• Android and NFC
• Android NFC Development
• Demo code
• Faq
4. What is Near Field Communication?
• Near Field Communication (NFC) is a global standard for
short-range wireless connectivity technology
• Driven by NFC Forum (http://www.nfc-forum.org) since
2004.
• 160 + members, including Google, Intel, Microsoft,
Nokia, NEC, Paypal, At&t, Visa, VTT.
• Promote N-Mark trademark as a universal symbol for
NFC.
5. Near Field Communication
Physical action, virtual reaction
In practice, NFC enables simple and safe
two-way interactions among electronic
devices within a close proximity.
6. NFC Features
• Short range technology
– Connection established when devices or tag close enough (5-10
cm)
– Simple setup (instant configuration)
• 2-way communication
– Not only reading like in contactless smart cards, but data exchange
• Passive tags
– No need for external power
– Low-price
• Security
– Support secure element
– Close proximity use brings some basic security
17. Activity - Reacting to NFC scans
In the .java file for your Activity:
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcEvent;
//...
NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
Boolean nfcEnabled = mNfcAdapter.isEnabled();
//Give priority to the foreground activity to receive NFC scan intents.
mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent,
mReadTagFilters, null);
//Enable Android Beam for peer-to-peer
mNfcAdapter.setNdefPushMessageCallback(this,this);
18. Activity - Formatting to NDEF Format
In the .java file for your Activity:
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
//...
String data = “Hello NFC”;
// create a new NDEF record w/ NDEF message using the app's custom MIME type:
String mimeType = "application/root.gast.playground.nfc"; //unique to your app
byte[] mimeBytes = mimeType.getBytes(Charset.forName("UTF-8"));
byte[] dataBytes = data.getBytes(Charset.forName("UTF-8"));
byte[] id = new byte[0];
NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
mimeBytes, id, dataBytes);
NdefMessage m = new NdefMessage(new NdefRecord[] { record });
19. Activity - Reading NDEF Format
In the .java file for your Activity: import android.nfc.Tag;
//...
Parcelable[] rawMsgs =
intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage[] msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
NdefRecord record = msgs[0].getRecords()[0];
byte[] payload = record.getPayload(); //the info you wanted
20. Activity - Android Beam
In the .java file for your Activity:
//Implement the interface
public class BeamActivity extends Activity implements CreateNdefMessageCallback
// Register callback
mNfcAdapter.setNdefPushMessageCallback(this, this);
public NdefMessage createNdefMessage(NfcEvent event) {
String data = "Hello NFC”;
// create a new NDEF record and containing NDEF message:
String mimeType = "application/com.fernandocejas.example.android.nfc”;
byte[] mimeBytes = mimeType.getBytes(Charset.forName("UTF-8"));
byte[] dataBytes = data.getBytes(Charset.forName("UTF-8"));
byte[] id = new byte[0];
NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
mimeBytes, id, dataBytes);
NdefMessage message = new NdefMessage(new NdefRecord[] { record });
return message;
}