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; } } }
相关文章推荐
- Android Fragment中onActivityResult()方法获取不到返回数据或者无响应的解决办法
- Android开发之ViewPager使用详解(一)
- Android 开发之 ---- bootloader (LK)
- Android开发总结笔记 Fragment(下) 1-2-12
- Android SparseArray ArrayMap
- Android studio 提示org.gradle.jvmargs=-Xmx1024m类似错误
- android res加载
- PercentLayout的固定比例
- 【Android】2:控件使用及页面跳转实战
- Android 多个Activity间对象共享
- Android 中style的使用
- 使用RecyclerView实现滚动控件
- 升级CompileSdk和重构主页
- Android测试分析二
- Android CardView的基本使用
- android 实践项目四
- Android之玩转MPAndroidChart让(折线图、柱形图、饼状图、散列图、雷达图)优雅的舞动
- Android程序使用smartupload上传文件的写法
- android测试分析1
- Android去掉标题栏点菜单键程序崩溃