Android Beam技术是什么
Android Beam的基本理念就是两部(只能是1对1,不可像蓝牙那样1对多)NFC设备靠近时(一般是背靠背),通过触摸一部NFC设备的屏幕,将数据推向另外一部NFC设备。在传递数据的过程中,两部NFC设备不能离得太远,否则NFC连接将中断。
使用Android Beam技术传输文本数据
目前2015-09-05在android支持的nfc设备上,只要我们提供数据类型就可,不用手动像socket那样手动write(data),由系统完成,
Android SDK提供了如下两个用于传递消息的方法
NfcAdapter.setNdefPushMessage
public void setNdefPushMessage(NdefMessage message, Activity activity, Activity ... activities);
NfcAdapter.setNdefPushMessageCallback ,这是一个回调,直到需要传输数据时才准备数据.
public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity, Activity ... activities);
public NdefMessage createNdefMessage(NfcEvent event)
使用Android Beam技术传输文本示例
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;
/*
* 两个nfc设备通信示例
* 本例中使用第2种方法传递数据,NfcAdapter.setNdefPushMessageCallback
*/
public class AndroidBeamMainActivity extends Activity
implements CreateNdefMessageCallback //传输数据的方法
, OnNdefPushCompleteCallback {//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); //设置当前aty为处理nfc的应用,注意aty为singleTop
mNfcAdapter = mNfcAdapter.getDefaultAdapter(this);
mPendingIntent = PendingIntent.getActivity(this, , new Intent(this,getClass()), ); //数据传输函数回调
mNfcAdapter.setNdefPushMessageCallback(this, this);
//传输完毕回调
mNfcAdapter.setOnNdefPushCompleteCallback(this, this); }
/*
* from OnNdefPushCompleteCallback
* 数据传输完毕的回调.
*/
@Override
public void onNdefPushComplete(NfcEvent event) {
Log.d("message", "complete");
} /*
* 这是CreateNdefMessageCallback的回调,
* 创建一个NdefMessage.这里是把EditText中内容封装成NdefMessage
* NdefMessage中record的类型可以自己随意,如text,uri,application
*
*/
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
String text = mBeamText.getText().toString().trim();
if ("".equals(text))
text = "默认文本"; /*
* 创建一个ApplicationRecord类型的record
* 启动计算器.
*/
/*NdefMessage ndefMessage = new NdefMessage(
new NdefRecord[] { NdefRecord.createApplicationRecord("com.android.calculator2") });*/ /*
* 根据txt创建一个NdefRecord,并把它放入到NdefMessage中.
*/
NdefMessage ndefMessage = new NdefMessage(new NdefRecord[]{createTextRecord(text)}); return ndefMessage;
} /*
*注册当前aty为nfc处理功能
*/
@Override
public void onResume() {
super.onResume();
if (mNfcAdapter != null)
mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null,
null); }
/*
*注销当前aty处理nfc的功能
*/
@Override
public void onPause() {
super.onPause();
if (mNfcAdapter != null)
mNfcAdapter.disableForegroundDispatch(this);
} @Override
public void onNewIntent(Intent intent) {
/*
* 当本aty启动时,有另个nfc设备通过beam与本机配对并传来数据时,
* 本函数接收数据,并用toast提示出来.
* 如果要aty未启动,那么系统调用默认的处理程序.
*/
processIntent(intent);
} /*
* 根据文本创建一个NdefRecord,
* 注意ndef文本格式规范
* 1,这些数据的第1个字节描述了数据的状态,
* 2,然后若干个字节描述文本的语言编码,
* 3,最后剩余字节表示文本数据。
*/
public NdefRecord createTextRecord(String text) {
//语言编码
byte[] langBytes = Locale.CHINA.getLanguage().getBytes(Charset.forName("US-ASCII")); //设置text的字符编码为utf8
Charset utfEncoding = Charset.forName("UTF-8");
byte[] textBytes = text.getBytes(utfEncoding);
int utfBit = ;
char status = (char) (utfBit + langBytes.length);
//申请空间
byte[] data = new byte[ + langBytes.length + textBytes.length];
//第1字节是状态码
data[] = (byte) status;
System.arraycopy(langBytes, , data, , langBytes.length);
System.arraycopy(textBytes, , data, + langBytes.length,textBytes.length);
NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,NdefRecord.RTD_TEXT, new byte[], data); return record;
} void processIntent(Intent intent) { Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); NdefMessage msg = (NdefMessage) rawMsgs[];
String text = TextRecord.parse(msg.getRecords()[]).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;
} 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[] & 0x80) == ) ? "UTF-8"
: "UTF-16";
int languageCodeLength = payload[] & 0x3f;
String languageCode = new String(payload, , languageCodeLength,"US-ASCII");
String text = new String(payload, languageCodeLength + ,
payload.length - languageCodeLength - , textEncoding);
return new TextRecord(text);
} catch (UnsupportedEncodingException e) {
// should never happen unless we get a malformed tag.
throw new IllegalArgumentException(e);
}
}
}