Android near Field Communication---NFC Basics (v)

Source: Internet
Author: User

Transfer from http://blog.csdn.net/think_soft/article/details/8190463

Android App Recorder (Android application record---AAR)

The Android application record (AAR) introduced in Android4.0 (API level 14) provides a strong certainty when scanning to an NFC tag to launch the application. AAR has the package name of the application embedded inside 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 launches the application based on the package name inside the AAR. If the app is not on the current device, Google Play is launched to download the app.

AAR is useful if you want to prevent other applications from filtering the same intent and potentially dealing with specific NFC tags that you deploy. AAR is supported only at the application level, because the package name is constrained and cannot be filtered at the activity level intent. If you want to process intent at the activity level, use the intent filter.

If an AAR is included in the NFC tag, the NFC tag dispatch system is dispatched in the following manner:

1. In general, try using the intent filter to start an activity. If the activity that matches the intent is also matched with AAR, the activity is activated.

2. If the activity with intent formation does not match AAR, or if there are multiple activity that can handle the intent, or if there is no activity that can handle the intent, then the application specified by AAR is launched.

3. If there is no application associated with the AAR, then Google Play will be launched to the group based on the AAR application.

Note: You can use the foreground dispatch system to rewrite the AAR and intent dispatch system when the NFC tag is found. It allows for priority use of activity at the foreground. In this way, the activity must be in the foreground to rewrite the AAR and intent dispatch system.

If you still want to filter the scanned NFC tag that does not contain AAR, you can usually declare the intent filter. This is useful if your application is interested in other NFC tags that do not contain AAR. For example, you might want to ensure that your application handles the dedicated NFC tags you deploy, as well as ordinary NFC tags deployed by third parties. Remember that AAR is only specified after Android4.0, so when you deploy an NFC tag, you probably want to use a device that broadly supports AAR and MIME type/uri. Also, when you deploy an NFC tag, how to write your NFC tag so that most devices (Android devices and other devices) support it. This can be done by defining a relatively unique MIME type or URI, making the application easier to differentiate.

Android provides a simple Api:createapplicationrecord () to create AAR. All you need to do is embed the AAR in your ndefmessage. Unless AAR is the only record in Ndefmessage, do not use the first record of Ndefmessage. This is because the Android system examines Ndefmessage's first record to determine the MIME type or URI of the NFC tag, which is used to create the intent object for the corresponding application. The following code demonstrates how to create an AAR:

Ndefmessage msg = new Ndefmessage (

New ndefrecord[] {

...,

Ndefrecord.createapplicationrecord ("Com.example.android.beam")}

Send ndef messages to other devices.

Android beam allows for simple peer data exchange between two Android devices, applications that want to send data to another device must be in the foreground, and the device that receives the data must not be locked. When the transmitting device is close enough to the receiving device, the transmitting device will display the "Touch to Beam" UI. The user can then choose whether to transmit the message to the receiving device.

Note: The ndef push in the foreground can be used in API level 10, which provides features similar to Android beam. These APIs are obsolete, but they are still available on some older devices. For more information please see Enableforegroundndefpush ().

You can enable Android Beam for your application by calling any of the following two methods:

1. Setndefpushmessage (): This method sets the received Ndefmessage object as a message to beam. When two devices are close enough, the message is automatically sent.

2. Setndefpushmessagecallback (): Receives a callback containing the Createndefmessage () method, which is called when the device is within the range of the emitted data. Callbacks will let you create ndef messages only when you need them.

An activity can only push one ndef message at a time, so if both methods are used at the same time, the Setndefpushmessagecallback () method takes precedence over the Setndefpushmessage () method. To use Android Beam, you typically must meet the following criteria:

1. The activity of transmitting data must be at the foreground. The screen of both devices must not be locked;

2. The data to be emitted must be encapsulated into a Ndefmessage object;

3. The NFC device receiving the transmitting data must support the COM.ANDROID.NPP ndef push protocol or the SNEP Protocol of the NFC Organization (Simple ndef Exchange protocol). COM.ANDROID.NPP protocol is required on devices with API Level9 (Android2.3) to API level (Android3.2). On API level (Android4.0) and later devices, both COM.ANDROID.NPP and SNEP are required.

Note: If the activity in the foreground is enabled for Android Beam, then the standard intent dispatch system will be disabled. However, if the activity also has foreground scheduling enabled, it can still scan the NFC tag that matches the intent filter in the foreground dispatch system.

Enable Android Beam:

1. Create a Ndefmessage object containing Ndefrecord that is ready to be pushed to another device.

2. Call the Setndefpushmessage () method with the Ndefmessage type parameter, or in the activity's OnCreate () method to pass an object that implements the Nfcadapter.createndefmessagecallback interface by calling the Setndefpushmessagecallback method. Both methods require at least one activity to be enabled for Android beam, as well as an optional other active activity list.

Normally, if your activity pushes the same NDEF message at any time, then when two devices are within the range of communication, use Setndefpushmessage (). Use the Setndefpushmessagecallback method when your application wants to focus on the current content of the application and want to push Ndef messages based on the user's behavior in your application.

The following sample code demonstrates how to call the Nfcadapter.createndefmessagecallback method in the activity's OnCreate () method (see Androidbeamdemo for the complete example). There is also a way to help create MIME records in this example:

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.getdefaultadapter (this);

if (Mnfcadapter = = null) {

Toast.maketext (This, "NFC was 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\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

* Receives a push with an AAR in it, the application specified in the AAR

* is guaranteed to run. The AAR overrides the tag dispatch system.

* Can add it back in to guarantee

* Activity starts when receiving a beamed message. For now, the This Code

* uses the tag dispatch system.

*/

, Ndefrecord.createapplicationrecord ("Com.example.android.beam")

});

return msg;

}

@Override

public void Onresume () {

Super.onresume ();

Check 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 the AAR. If the app doesn't exist, Google play launches the download program. Therefore, if AAR is used, the following intent filters, for Android4.0 devices, are not technically necessary:

<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 under the following conditions:

1. Scan to NFC tag;

2. An AAR or ndef message that receives a Com.example.android.beam type contains an application/vnd.com.example.android.beam type of MIME record for Android Beam of the time.

Even though AAR ensures that an application is started or downloaded, it is recommended to use the intent filter because it allows you to choose to start the activity in the application instead of always starting the main activity of the application package specified in the AAR. AAR does not have the granularity of activity levels. There are also some Android devices that do not support AAR, and you should also embed the identity information in the first ndef record of the NDEF message, along with the corresponding filter.

Android near Field Communication---NFC Basics (v) (RPM)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.