BreadcrumbsStartseite / Development with Android Beam and NFC Peer-2-Peer
Development with Android Beam and NFC Peer-2-Peer
by NFC Admin, Mittwoch, 28 Dezember 2011
NFC offers three different operating modes:
- Card Emulation Mode
- Reader Writer Mode
- Peer-2-Peer Mode
Reader Writer Mode is already available in Android since the first Gingerbread release in December 2010. Card Emulation Mode, which is actually required for Google Wallet, is not yet available for public. Peer-2-Peer Mode as defined in ISO 18092 is now finding its way into Android devices, also known as „Android Beam“.
Developer that are used to Peer-2-Peer communication from other platforms like J2me (using the JSR257), we be confronted with a different software architecture for this feature. On Android there is nothing like a Target or an Initiator Mode – at least not for the developer. Under to hood, you will find the classes like P2pInitiator.java or P2pTarget.java which are part of the package /frameworks/base/core/jav/com/android/internal/nfc. Also the P2p protocol which is called LLCP (Logical Link Layer Protocol) is not accessible to the developer. And of course the NDEF and RTD implementation is part of the core operating system. The base classes can be found in
/framework/base/core/java/android/nfc. (Browsing the Android Code/GIT repo is not possible anymore so please check out the repo and have a look at the source offline). NFC P2P sending and receiving should be as easy as possible for a developer and is available since API level 14 (since 4.0, Ice Cream Sandwich).
Receiving an NDEF Message (= P2p Target Mode)
In order to receive an NDEF through a NFC P2p connection, the activity has to listen to an Intent, which his defined in the AndroidManifest.xml. The according part looks like the following:
<intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="application/com.example.android.beam" /> </intent-filter>
In order to process the NDEF Message in your application, you need to handle the incomming Intent in the onNewIntent() method.
Sending an NDEF Message (= P2p Initiator Mode)
For sending an NDEF Messages, your Activity needs to implement to Interfaces:
NfcAdapter.CreateNdefMessageCallback.createNdefMessage(NfcEvent event) NfcAdapter.OnNdefPushCompleteCallbackon.NdefPushComplete(NfcEvent event)
CreateNdefMessageCallBack causes a Callback to be invoked when another NFC device capable of NDEF push (Android Beam) is within range. Using setNdefPushMessageCallback() you can instantly create an NdefMessage at the moment that another device is within range for NFC and push it over (pushing is done automatically). Using this callback allows you to create a message with data that might vary based on the content currently visible to the user. Alternatively, you can call setNdefPushMessage() if the NdefMessage always contains the same data.
OnNdefPushCompleteCallbackon causes a Callback as soon as sending of the NDEF Message is completed. You should implement this method in order to let the user know, when the transfer is complete and thus the user can put his NFC device away.
In order to receive the Callback, you have to register them, which usually is done in the onCreate() method.
// Register callback to set NDEF message mNfcAdapter.setNdefPushMessageCallback(this, this); // Register callback to listen for message-sent success mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
If you want to make sure, that on the target devices, the correct application is triggered to process the NDEF record, you can set the Android Application Record (AAR).The AAR overrides the tag dispatch system. You can add it back in to guarantee that this activity starts when receiving a beamed message. For now, this code uses the tag dispatch system. Using the AAR can be done adding the following NDEF Record to the NDEF Message:
With this know-how and the example code from Google it should be easy for you to develop your own P2P Application using Android Beam.
- NFC Phones
- NFC Stickers