Cosc 4/5730
Android and Blackberry
Near Field Communications (NFC)
• Near field communication (NFC) is a set of standards
for smartphones and similar devices to establish radio
communication with each other by touching them
together or bringing them into close proximity, usually
no more than an inch or so.
• Present and anticipated applications include
contactless transactions, data exchange, and simplified
setup of more complex communications such as Wi-Fi.
• Communication is also possible between an NFC device
and an unpowered NFC chip, called a "tag".
NFC (2)
• NFC standards cover communications protocols
and data exchange formats, and are based on
existing radio-frequency identification (RFID)
standards including ISO/IEC 14443 and FeliCa.
• The standards include ISO/IEC 18092 and those
defined by the NFC Forum, which was founded in
2004 by Nokia, Philips and Sony, and now has 150
members. The Forum also promotes NFC and
certifies device compliance
NFC and phones
• Starting with Android 2.3, the platform
includes an NFC stack and framework API that
allows you to read/write to NDEF (NFC Forum
Data Exchange Format) tags.
– For Android smartphones, that means the
requirement is to be running at least Android 2.3
and have a NFC chip on the phone.
• Many newer phones/tablets have it now. Looks
settings-> wireless More…
What do with NFC
• The number of applications that could use NFC is
limited by only by the developers.
• The first major apps are things like Google wallet,
payments systems, and store cards.
• Otherwise, contact exchange and that sort things.
– Maybe adding friends in facebook, google+, etc
– File/Music/Data exchange between devices
• But remember the devices have be really close.
– Less then 4cm ( under 2 inches)
– “Tap” your phone/tablet on a the wifi access point
and it will send the configurations to the device.
What do with NFC (2)
• Think QR without a camera.
– Would allow phones to easily respond and react
to objects around them. Imagine a world where
you can touch a phone to a poster, a piece of
furniture, a tag, a keychain, a business card,
anything, and expect an application to respond.
– is a place you can get
stickers with nfc chips in them and customized
them to your “tag”, url, data, etc.
• There are two major uses cases when working
with NDEF data and Android:
– Reading NDEF data from an NFC tag
– Beaming NDEF messages from one device to
another with Android Beam™
• Note:
– None of this will work in the android emulators.
You need devices with NFC turned on.
Reading NDEF data from an NFC tag
• I don’t have any tags, so I’m unable to test any
of the code.
– The stickyNotes (android) example uses them.
– The app appears to work, but I can’t test it.
– Both reads and writes (I think)
• Uses 4.0 APIs which does not work on 4.2
• There is a android nfc in the demo API. It will
read any tag and you can send it fake to see
how it works.
• There are some problems. See the reference for fixes.
Beam feature
• If you want to send a file to another android
device you can use their beam feature.
• nfcDemo3 does this.
– Allows you to select any file and then “beam” to
another device. This app only needs to be
installed one device, the receiving device doesn’t
need this app.
Beam feature (2)
• First get the nfc adapter
NfcAdapter =
• The address of the file must be in the form of
Uri[], which is easy using a “chooser”
• Via an intent.
• To send the file:
adapter.setBeamPushUris(file, this);
Sending “messages”
• To send a nfc message, it needs to be in a
NdefMessage format.
• This is done via a call back when the user
“uses the nfc option” on the screen.
– This is controlled by the OS, not the user (in 4.2+ anyway).
• In the activity, you implement the
CreateNdefMessageCallback and
• In on create get the adapter
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
• Register callback to set NDEF message
• And override the method:
public NdefMessage createNdefMessage(NfcEvent
• Register the callback in Oncreate()
– Register callback to listen for message-sent success
mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
• Override the
public void onNdefPushComplete(NfcEvent arg0) {
• A handler is needed to send messages to the activity when
this callback occurs, because it happens from a binder
thread, example code:
Receiving NFC messages
• Register the type of message you app will receive,
because you activity will be stored when receiving it.
– In AndroidManifest.xml file.
android:name="android.nfc.action.NDEF_DISCOVERED" />
android:name="android.intent.category.DEFAULT" />
Receiving NFC messages (2)
• Override the onNewIntent(Intent intent) method,
which will receive the intent with the nfc message.
• Pull the message of the intent with:
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
• only one message sent during the beam
NdefMessage msg = (NdefMessage) rawMsgs[0];
• record 0 contains the MIME type, record 1 is the AAR,
if present
• Likely need to convert to a string, since returns byte[]
Code examples
• stickyNotes
• Tested, but not sure if it works should read/write tags
• nfcDemo
– Will send nfc message between to devices
• Needs to be install on both devices.
• nfcDemo3
– Will send a file to another nfc enable device via
android Beam.
• Only needs to be installed on sending device.
– with a video
explanation as well.

similar documents