您的位置:首页 > 移动开发 > Android开发

android nfc P2P模式

2015-11-30 09:31 399 查看
demo下载地址http://download.csdn.net/detail/u012303938/9311167(打开手机自带的NFC 与BEAM,两个手机相碰即可传递数据)

主要代码如下:
package com.example.nfcp2p;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Locale;

import android.support.v7.app.ActionBarActivity;
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.NfcEvent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity implements CreateNdefMessageCallback{
NfcAdapter nfcAdapter;
PendingIntent intent;
TextView tv_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_text=(TextView) findViewById(R.id.tv_text);
nfcAdapter=NfcAdapter.getDefaultAdapter(this);
nfcAdapter.setNdefPushMessageCallback(this, this, this);
intent=PendingIntent.getActivity(this, 0, new Intent(this,getClass()), 0);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
super.onNewIntent(intent);
setIntent(intent);
if(intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)){
Parcelable[] parcelables=intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
String str=readRtdText(parcelables);
tv_text.setText(str);
}
}

public String readRtdText(Parcelable[] parcelables){
NdefMessage [] ndefMessages=new NdefMessage[parcelables.length];
String text="";
for(int i=0;i<parcelables.length;i++){
ndefMessages[i]=(NdefMessage) parcelables[i];

NdefRecord[]ndefRecords=ndefMessages[i].getRecords();
for(int j=0;j<ndefRecords.length;j++){
if(ndefRecords[j].getTnf()==NdefRecord.TNF_WELL_KNOWN){
if(Arrays.equals(ndefRecords[j].getType(), NdefRecord.RTD_TEXT)){
byte[] payload=ndefRecords[j].getPayload();
byte status=payload[0];
String utftype=(status&0200)==0?"UTF-8":"UTF-16";
int length=status&0077;
//RTD_TEXT由{UTF状态(0为UTF8,1<<7为UTF16)+语言编码长度}(只占一个byte)+{语言编码}+{内容}
String localdata=new String(payload, 1, length, Charset.forName("UTF-8"));
String textdata=new String(payload, 1+length, payload.length
-(1+length), Charset.forName("UTF-8"));

text+="语言"+localdata+"内容"+textdata;
}else{
text="不是RTD_TEXT";
}
}else{
text+="不是TNF_WELL_KNOWN+"+ndefRecords[j].getTnf();
}
}
}
return text;
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
nfcAdapter.enableForegroundDispatch(this, intent, null, null);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
nfcAdapter.disableForegroundDispatch(this);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public NdefMessage createNdefMessage(NfcEvent event) {
// TODO Auto-generated method stub
NdefMessage message=getRtdText("手机号:15275415668", true, false);
return message;
}

public NdefMessage getRtdText(String text,boolean utfEncode,boolean AAR){
Locale locale=new Locale("en", "US-ASCII");
byte []localedatas=locale.getLanguage().getBytes(Charset.forName("UTF-8"));
Charset charset=utfEncode?Charset.forName("UTF-8"):Charset.forName("UTF-16");
int utfbit=utfEncode?0:1<<7;
char stauschar=(char)(utfbit+localedatas.length);
byte [] textdatas=text.getBytes(charset);
byte[] payload=new byte[1+localedatas.length+textdatas.length];
payload[0]=(byte)stauschar;
System.arraycopy(localedatas, 0, payload, 1, localedatas.length);
System.arraycopy(textdatas, 0, payload, localedatas.length+1,
payload.length-(localedatas.length+1));
NdefRecord ndefRecord=new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
NdefRecord.RTD_TEXT, new byte[0], payload);
if(AAR){
NdefMessage ndefMessage=new NdefMessage(new NdefRecord[]{ndefRecord,
NdefRecord.createApplicationRecord("com.example.nfcp2p")});
return ndefMessage;
}else{
NdefMessage ndefMessage=new NdefMessage(new NdefRecord[]{ndefRecord});
return ndefMessage;
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: