Introducing Android BeamAndroid Beam, introduced in Android 4.0 (API level 14), provides a simple API for an application to transmit data between two devices using NFC, simply by placing them back-to-back. For example, the native contacts, browser, and YouTube applications use Android Beam to share the currently viewed contact, web page, and video, respectively, with other devices.To beam messages, your application must be in the foreground and the device receiving the data must not be locked.Android Beam is initiated by tapping two NFC-enabled Android devices together. Users are presented with a “touch to beam” UI, at which point they can choose to “beam” the foreground application to the other device.Android Beam uses NFC to push NDEF messages between devices when they are physically placed together.By enabling Android Beam within your application, you can define the payload of the beamed message. If you don’t customize the message, the default action for your application will be to launch it on the target device. If your application isn’t installed on the target device, the Google Play Store will launch and display your application’s details page.To define the message your application beams, you need to request the NFC permission in the manifest:The process to define your own custom payload is described as follows:1. Create an NdefMessage object that contains an NdefRecord that contains your message payload.2. Assign your Ndef Message to the NFC Adapter as your Android Beam payload.
3. Configure your application to listen for incoming Android Beam messages.
Creating Android Beam Messages
To create a new Ndef Message, create a new NdefMessage object that contains at least one NdefRecord containing the payload you want to beam to your application on the target device.
When creating a new Ndef Record, you must specify the type of record it represents, a MIME type, an ID, and a payload. There are several common types of Ndef Record that can be used to transmit data using Android Beam; note that they should always be the first record added to each beamed message.
Using the NdefRecord.TNF_MlME_MEDlA type, you can transmit an absolute URI:
NdefRecord uriRecord = new NdefRecord(
NdefRecord.TNF_ABS0LUTE_URI,
"http://blog.radioactiveyak.com".getBytes(Charset.forName("US-ASCII")), new byte[0], new byte[0]);
This is the most common Ndef Record transmitted using Android Beam because the received Intent will be of the same form as any Intent used to start an Activity. The Intent Filter used to decide which NFC messages a particular Activity should receive can use the scheme, host, and path Prefix attributes.
If you need to transmit messages that contain information that can’t be easily interpreted as a URI, the NdefRecord.TNF_MiME_MEDiA type supports the creation of an application-specific MIME type and the inclusion of associated payload data:
byte[] mimeType = "application/com.paad.nfcbeamu.getBytes(Charset.forName("US-ASCII")); byte[] tagId = new byte[0];
byte[] payload = "Not a URIu.getBytes(Charset.forName("US-ASCII"));
NdefRecord mimeRecord = new NdefRecord(
NdefRecord.TNF_MIME_MEDIA,
mimeType,
tagId,
payload);
A more complete examination of the available NDEF record types and how to use them can be found in the Android Developer Guide (http://developer.android.com/guide/topics/nfc/ nfc.html#creating-
đang được dịch, vui lòng đợi..
