Cosc 4/5730 Android and Blackberry Near Field Communications (NFC) 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. • http://www.tagstand.com/pages/about-nfc – http://www.tagstand.com/ is a place you can get stickers with nfc chips in them and customized them to your “tag”, url, data, etc. Basics: • 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 Code • 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 = adapter=NfcAdapter.getDefaultAdapter(this); • 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 onNdefPushCompleteCallback. CreateNdefMessageCallback • In on create get the adapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this); • Register callback to set NDEF message mNfcAdapter.setNdefPushMessageCallback(this, this); • And override the method: public NdefMessage createNdefMessage(NfcEvent event) OnNdefPushCompleteCallback • 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: 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> <action android:name="android.nfc.action.NDEF_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="application/edu.cs4730.nfcdemo.beam" /> </intent-filter> 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( NfcAdapter.EXTRA_NDEF_MESSAGES); • 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 msg.getRecords()[0].getPayload() • 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. References • • http://en.wikipedia.org/wiki/Near_field_communication Android – http://developer.android.com/guide/topics/connectivity/nfc/index.html – http://developer.android.com/resources/samples/NFCDemo/index.html – http://stackoverflow.com/questions/5078649/android-nfc-sample-demo-reads-only-fakeinformation-from-the-tag – http://www.jessechen.net/blog/how-to-nfc-on-the-android-platform/ – https://github.com/commonsguy/cwomnibus/blob/master/NFC/FileBeam/src/com/commonsware/android/filebeam/MainActivity. java – http://stackoverflow.com/questions/8648149/bi-directional-android-beam?rq=1 – http://developer.android.com/reference/android/nfc/NfcAdapter.html#setNdefPushMessage %28android.nfc.NdefMessage,%20android.app.Activity,%20android.app.Activity...%29 – http://developer.android.com/guide/topics/connectivity/nfc/nfc.html – http://stackoverflow.com/questions/10265928/writing-data-into-nfc-tag-in-android-tutorial – http://code.google.com/p/ndef-tools-for-android/downloads/list – http://www.jessechen.net/blog/how-to-nfc-on-the-android-platform/ with a video explanation as well. – http://stackoverflow.com/questions/5762234/nfc-tutorial-for-android-other-than-api-demo