Android Beam的基本理念
Android Beam的基本理念就是两部(只能是两部)NFC设备靠近时(一般是背靠背),通过触摸一部NFC设备的屏幕,将数据推向另外一部NFC设备。在传递数据的过程中,两部NFC设备不能离得太远,否则NFC连接将中断。
Android Beam API
Android SDK提供了如下两个用于传递消息的方法。
NfcAdapter.setNdefPushMessage
NfcAdapter.setNdefPushMessageCallback
public void setNdefPushMessage(NdefMessage message, Activity activity, Activity ... activities);
public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity, Activity ... activities);
public NdefMessage createNdefMessage(NfcEvent event)
Demo
import java.nio.charset.Charset;
import java.util.Locale; import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcAdapter.OnNdefPushCompleteCallback;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast; /**
* 两部android手机,进行传入文本信息。
* @author dr
*
*/
public class AndroidBeamMainActivity extends Activity implements
CreateNdefMessageCallback, OnNdefPushCompleteCallback { private EditText mBeamText; private NfcAdapter mNfcAdapter;
private PendingIntent mPendingIntent; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_android_beam);
mBeamText = (EditText) findViewById(R.id.edittext_beam_text); mNfcAdapter = mNfcAdapter.getDefaultAdapter(this);
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()), 0); mNfcAdapter.setNdefPushMessageCallback(this, this);
mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
} @Override /** 窗口处理完成 */
public void onNdefPushComplete(NfcEvent event) {
Log.d("message", "complete");
} @Override /** 如果另外一台手机,靠近当前这部手机 */
public NdefMessage createNdefMessage(NfcEvent event) {
String text = mBeamText.getText().toString().trim();
if ("".equals(text))
text = "默认文本";
/*
* "com.android.calculator2" 官方原生计算器包。
* 当另外一部手机靠近这部手机时,会启动计算器。
*
* NdefMessage ndefMessage = new NdefMessage( new NdefRecord[] {
* NdefRecord .createApplicationRecord("com.android.calculator2") });
*/
NdefMessage ndefMessage = new NdefMessage(
new NdefRecord[] { createTextRecord(text) }); return ndefMessage;
} @Override
public void onResume() {
super.onResume();
if (mNfcAdapter != null)
mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null,
null);
} @Override
public void onPause() {
super.onPause();
if (mNfcAdapter != null)
mNfcAdapter.disableForegroundDispatch(this);
} @Override
public void onNewIntent(Intent intent) {
// 用于显示接收手机的信息。如果接收手机没有打开,会默认调用系统的黑界面。
processIntent(intent);
} /** 根据文本创建 NdefRecord 这个对象 */
public NdefRecord createTextRecord(String text) {
byte[] langBytes = Locale.CHINA.getLanguage().getBytes(
Charset.forName("US-ASCII"));
Charset utfEncoding = Charset.forName("UTF-8");
byte[] textBytes = text.getBytes(utfEncoding);
int utfBit = 0;
char status = (char) (utfBit + langBytes.length);
byte[] data = new byte[1 + langBytes.length + textBytes.length];
data[0] = (byte) status;
System.arraycopy(langBytes, 0, data, 1, langBytes.length);
System.arraycopy(textBytes, 0, data, 1 + langBytes.length,
textBytes.length);
NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
NdefRecord.RTD_TEXT, new byte[0], data); return record;
} void processIntent(Intent intent) {
Parcelable[] rawMsgs = intent
.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); NdefMessage msg = (NdefMessage) rawMsgs[0];
String text = TextRecord.parse(msg.getRecords()[0]).getText();
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
}
}
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import android.nfc.NdefRecord; public class TextRecord {
private final String mText; private TextRecord(String text) {
mText = text;
} public String getText() {
return mText;
} // 将纯文本内容从NdefRecord对象中解析出来
public static TextRecord parse(NdefRecord record) { if (record.getTnf() != NdefRecord.TNF_WELL_KNOWN)
return null;
if (!Arrays.equals(record.getType(), NdefRecord.RTD_TEXT))
return null; try {
byte[] payload = record.getPayload();
/*
* payload[0] contains the "Status Byte Encodings" field, per the
* NFC Forum "Text Record Type Definition" section 3.2.1.
*
* bit7 is the Text Encoding Field.
*
* if (Bit_7 == 0): The text is encoded in UTF-8 if (Bit_7 == 1):
* The text is encoded in UTF16
*
* Bit_6 is reserved for future use and must be set to zero.
*
* Bits 5 to 0 are the length of the IANA language code.
*/
String textEncoding = ((payload[0] & 0x80) == 0) ? "UTF-8"
: "UTF-16";
int languageCodeLength = payload[0] & 0x3f;
String languageCode = new String(payload, 1, languageCodeLength,
"US-ASCII");
String text = new String(payload, languageCodeLength + 1,
payload.length - languageCodeLength - 1, textEncoding);
return new TextRecord(text);
} catch (UnsupportedEncodingException e) {
// should never happen unless we get a malformed tag.
throw new IllegalArgumentException(e);
}
} }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <EditText
android:id="@+id/edittext_beam_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入要传输的文本"/> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="请将当前设备靠近其他NFC设备,别忘了触摸屏幕哦!"
android:textSize="16sp" /> <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:src="@drawable/read_nfc_tag" /> </LinearLayout>