Android Application Record --- AAR)
The Android Application Record (AAR) introduced in Android4.0 (API Level 14) provides strong certainty for starting an application when the NFC tag is scanned. AAR has the package name of the application embedded into the NDEF record. You can add an AAR to any record of your NDEF message, because Android will search for the entire NDEF message for AAR. If it finds an AAR, it starts the application based on the package name in the AAR. If the application is not on the current device, Google Play is started to download the corresponding application.
If you want to prevent other applications from filtering the same Intent and potentially processing the specific NFC tags you deploy, AAR is useful. AAR is only supported at the application level. Because of the package name constraints, it cannot filter Intent at the Activity level. If you want to process Intent at the Activity level, use the Intent filter.
If the NFC tag contains the AAR, the NFC tag scheduling system will schedule the task as follows:
1. Usually, try to use the Intent filter to start an Activity. If the Activity that matches the Intent matches the AAR, the Activity is started.
2. if the Activity in the Intent formation does not match the AAR, or multiple activities can process the Intent, or the Activity cannot process the Intent, start the application specified by AAR.
3. If there is no application corresponding to the AAR, then Google Play will be started to group the application based on the AAR.
Note: You can use the foreground scheduling system to rewrite the AAR and Intent scheduling systems when NFC tags are detected. It allows the foreground Activity to be preferentially used. In this way, the Activity must rewrite the AAR and Intent scheduling systems on the foreground.
If you still want to filter NFC tags that do not contain AAR, you can declare the Intent filter. This method is useful if your application is interested in other NFC tags that do not contain the AAR. For example, you may want to ensure that your application processes the dedicated NFC tags you deploy and common NFC tags deployed by third parties. Remember that AAR is specified after Android4.0. Therefore, when deploying NFC tags, you may want to use devices that support both AAR and MIME-type/URI. In addition, when deploying NFC tags, you also need to write your NFC tags to support most devices (Android devices and other devices. This can be done by defining a relatively unique MIME type or URI that makes it easier for applications to differentiate.
Android provides a simple API for creating AAR: createApplicationRecord (). All you need to do is to embed the AAR into your NdefMessage. Unless AAR is the unique record in NdefMessage, do not use the first record of NdefMessage. This is because the Android system checks the first record of NdefMessage to determine the MIME type or URI of the NFC tag. This information is used to create the Intent object of the corresponding application. The following code creates an AAR:
NdefMessage msg = new NdefMessage (
New NdefRecord [] {
...,
NdefRecord. createApplicationRecord ("com. example. android. beam ")}
Transmit NDEF messages to other devices
Android Beam allows simple peer-to-peer data exchange between two Android devices. Applications that want to send data to another device must be in the foreground and the devices that receive data must not be locked. When the distance between the transmitting device and the receiving device is close enough, the transmitting device displays the UI of "Touch to Beam. Then, you can choose whether to send the message to the receiving device.
Note: In API Level 10, you can use NDEF push at the front end, which provides features similar to Android Beam. These APIs are outdated, but they are still valid on old devices. For more information, see enableForegroundNdefPush ().
You can enable Android Beam for your application by calling either of the following two methods:
1. setNdefPushMessage (): This method sets the received NdefMessage object to Beam as a message. When the two devices are close enough, messages are automatically sent.
2. setNdefPushMessageCallback (): receives a callback containing the createNdefMessage () method. when the device is within the range of data to be sent, this callback method is called. Callback allows you to create NDEF messages only when necessary.
An Activity can only push one NDEF message at a time. Therefore, if both methods are used, the priority of the setNdefPushMessageCallback () method is higher than that of the setNdefPushMessage () method. To use Android Beam, the following conditions must be met:
1. The Activity that transmits data must be in the foreground. The screens of both devices must not be locked;
2. The data to be sent must be encapsulated into an NdefMessage object;
3. The NFC device that receives the transmitted data must support the com. android. npp NDEF push protocol or the SNEP protocol (simple NDEF Exchange Protocol) of the NFC organization ). The com. android. npp Protocol is required for devices from API Level9 (Android2.3) to API Level 13 (Android3.2. On API Level 14 (Android4.0) and later devices, both com. android. npp and SNEP are required.
NOTE: If Android Beam is enabled for the foreground Activity, the standard Intent scheduling system will be disabled. However, if the Activity also enables foreground scheduling, it can still scan NFC tags matching the Intent filter in the foreground scheduling system.
Enable Android Beam:
1. Create an NdefMessage object containing NdefRecord to be pushed to another device.
2. Call the setNdefPushMessage () method with NdefMessage type parameters, or call the setNdefPushMessageCallback method in the onCreate () method of Activity to pass the object that implements the NfcAdapter. CreateNdefMessageCallback interface. Both methods require at least one Activity to enable Android Beam and an optional list of other active activities.
Generally, if your Activity pushes the same NDEF message at any time, you can use setNdefPushMessage () When two devices are in the communication range. When your application needs to focus on the current content of the application and want to push the NDEF message based on the user's behavior in your application, you need to use the setNdefPushMessageCallback method.
The following sample code demonstrates how to call NfcAdapter. CreateNdefMessageCallback in the onCreate () method of the activity (for a complete example, see AndroidBeamDemo ). In this example, you can use the following methods to create MIME records:
Package com. example. android. beam;
Import android. app. Activity;
Import android. content. Intent;
Import android. nfc. NdefMessage;
Import android. nfc. NdefRecord;
Import android. nfc. NfcAdapter;
Import android. nfc. NfcAdapter. CreateNdefMessageCallback;
Import android. nfc. NfcEvent;
Import android. OS. Bundle;
Import android. OS. Parcelable;
Import android. widget. TextView;
Import android. widget. Toast;
Import java. nio. charset. Charset;
Public class Beam extends Activity implements CreateNdefMessageCallback {
NfcAdapter mNfcAdapter;
TextView textView;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TextView textView = (TextView) findViewById (R. id. textView );
// Check for available NFC Adapter
MNfcAdapter = NfcAdapter. getdefaadapter adapter (this );
If (mNfcAdapter = null ){
Toast. makeText (this, "NFC is not available", Toast. LENGTH_LONG). show ();
Finish ();
Return;
}
// Register callback
MNfcAdapter. setNdefPushMessageCallback (this, this );
}
@ Override
Public NdefMessage createNdefMessage (NfcEvent event ){
String text = ("Beam me up, Android! \ N "+
"Beam Time:" + System. currentTimeMillis ());
NdefMessage msg = new NdefMessage (
New NdefRecord [] {createMime (
"Application/vnd.com. example. android. beam", text. getBytes ())
/**
* The Android Application Record (AAR) is commented out. When a device
* Es a push with an AAR in it, the application specified in the AAR
* Is guaranteed to run. The AAR overrides the tag dispatch system.
* You can add it back in to guarantee that this
* Activity starts when refreshing ing a beamed message. For now, this code
* Uses the tag dispatch system.
*/
//, NdefRecord. createApplicationRecord ("com. example. android. beam ")
});
Return msg;
}
@ Override
Public void onResume (){
Super. onResume ();
// Check to see that the Activity started due to an Android Beam
If (NfcAdapter. ACTION_NDEF_DISCOVERED.equals (getIntent (). getAction ())){
ProcessIntent (getIntent ());
}
}
@ Override
Public void onNewIntent (Intent intent ){
// OnResume gets called after this to handle the intent
SetIntent (intent );
}
/**
* Parses the NDEF Message from the intent and prints to the TextView
*/
Void processIntent (Intent intent ){
TextView = (TextView) findViewById (R. id. textView );
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
TextView. setText (new String (msg. getRecords () [0]. getPayload ()));
}
}
Note: The above code has commented out the AAR, And you can delete it. If you enable AAR, the application will always receive the Android Beam message specified in AAR. If the application does not exist, Google Play starts the download program. Therefore, if AAR is used, the following Intent filters are not technically required for devices later than Android4.0:
<Intent-filter>
<Action android: name = "android. nfc. action. NDEF_DISCOVERED"/>
<Category android: name = "android. intent. category. DEFAULT"/>
<Data android: mimeType = "application/vnd.com. example. android. beam"/>
</Intent-filter>
With this Intent filter, the com. example. android. beam Application can be started in the following situations:
1. NFC tags are scanned;
2. When receiving an android beam message containing an application/vnd.com. example. android. beam MIME record of the com. example. NDEF type.
Even if AAR can ensure that an application is started or downloaded, we recommend that you use the Intent filter because it will enable you to start the Activity in the application, instead of starting the main Activity of the application package specified in AAR. AAR has no Activity-level granularity. In addition, some android devices do not support AAR. You should also embed the ID information and corresponding filters in the first ndef record of the ndef message.