Comments 0

Presentation transcript

Cosc

4/5730

Android and Blackberry

Near Field Communications (NFC)

NFC

•Near field communication (NFC) is a set of standardsfor smartphones and similar devices to establish radiocommunication with each other by touching themtogether or bringing them into close proximity, usuallyno more thanan inch or so.

•Presentand anticipated applications includecontactless transactions, data exchange, and simplifiedsetup of more complex communications such as Wi-Fi.

•Communicationis also possible between an NFC deviceand an unpowered NFC chip, called a "tag".

•Thestandards include ISO/IEC18092and thosedefined by the NFC Forum, which was founded in2004 by Nokia, Philips and Sony, and now has 150members. The Forum also promotes NFC andcertifies device compliance

NFC and phones

•Starting with Android 2.3, the platformincludes an NFC stack and framework API thatallows you to read/write to NDEF (NFC ForumDataExchange Format) tags.

–ForAndroid smartphones, that means therequirement is to be running at least Android 2.3and have a NFC chip on the phone.

•The number of applications that could use NFC islimited 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 infacebook,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 thewifi

access pointand it will send the configurations to the device.

What do withNFC (2)

•Think QR without a camera.

–Would allowphones to easily respond and reactto objects around them. Imagine a world whereyou can touch a phone to a poster, a piece offurniture, a tag, a keychain, a business card,anything, and expect an application to respond.

•http://www.tagstand.com/pages/about-nfc

–http://www.tagstand.com/is a place you can getstickers withnfc

chips in them and customizedthem to your “tag”,url, data, etc.

Basics:

•There are two major uses cases when workingwith NDEF data and Android:

–Reading NDEF data from an NFC tag

–Beaming NDEF messages from one device toanother with Android Beam™

•Note:

–None of this will workin the android emulators.You need devices with NFC turned on.

Reading NDEF data from an NFCtag

•I don’t have any tags, so I’m unable to test anyof the code.

–ThestickyNotes

(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

Code

•There is a androidnfc

in the demo API. It willread any tag and you can send it fake to seehow it works.

•There are some problems. See the reference for fixes.

Beam feature

•If you want to send a file to another androiddevice you can use their beam feature.

•nfcDemo3 does this.

–Allows you to select any file and then “beam” toanother device. This app only needs to beinstalled one device, the receiving device doesn’tneed this app.

Beamfeature (2)

•First get thenfc

adapter

NfcAdapter

=adapter=NfcAdapter.getDefaultAdapter(this);

•The address of the file must be in the form ofUri[], which is easy using a “chooser”

•Via an intent.

•To send the file:

adapter.setBeamPushUris(file, this);

Sending “messages”

•To send anfc

message, it needs to be in aNdefMessage

format.

•This is done via a call back when the user“uses thenfc

option” on the screen.

–This is controlled by the OS, not the user (in 4.2+ anyway).

•In the activity, you implement theCreateNdefMessageCallback

andonNdefPushCompleteCallback.

CreateNdefMessageCallback

•In on create get the adapter

mNfcAdapter

=NfcAdapter.getDefaultAdapter(this);

•Registercallback to set NDEF message

mNfcAdapter.setNdefPushMessageCallback(this,this);

•And override the method:

publicNdefMessage

createNdefMessage(NfcEvent

event)

OnNdefPushCompleteCallback

•Register the callback inOncreate()

–Registercallback to listen for message-sent success

mNfcAdapter.setOnNdefPushCompleteCallback(this, this);

•Override the

publicvoidonNdefPushComplete(NfcEvent

arg0){

•Ahandler is needed to send messages to the activity whenthiscallback occurs, because it happens from a binderthread, example code:

mHandler.obtainMessage(MESSAGE_SENT).sendToTarget();

}

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.

<intent-filter>

<actionandroid:name="android.nfc.action.NDEF_DISCOVERED" />

<categoryandroid:name="android.intent.category.DEFAULT" />

<dataandroid:mimeType="application/edu.cs4730.nfcdemo.beam"/>

</intent-filter>

Receiving NFCmessages (2)

•Override theonNewIntent(Intent intent) method,which will receive the intent with thenfc