NFC技术(使用Android Beam技术传输文本)

【NFC技术(使用Android Beam技术传输文本)】天下之事常成于困约,而败于奢靡。这篇文章主要讲述NFC技术:使用Android Beam技术传输文本相关的知识,希望能为你提供帮助。

NFC技术(使用Android Beam技术传输文本)

文章图片

NFC技术(使用Android Beam技术传输文本)

文章图片

1 //实现两部android手机文本传输 2 //4.0以上 3 //方法:将两部安卓手机背对背,一部手机输入文本,点击屏幕发送,另一部接收显示 4 5 public class MainActivity extends Activity implements 6CreateNdefMessageCallback, OnNdefPushCompleteCallback { 7 8private NfcAdapter mnfcAdapter; 9private EditText mBeamText; 10private PendingIntent mPendingIntent; 11 12@Override 13protected void onCreate(Bundle savedInstanceState) { 14super.onCreate(savedInstanceState); 15setContentView(R.layout.fragment_main); 16mBeamText = (EditText) findViewById(R.id.editText1); 17mnfcAdapter = mnfcAdapter.getDefaultAdapter(this); 18mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, 19getClass()), 0); 20 21// 指定要传输文本的回调 22mnfcAdapter.setNdefPushMessageCallback(this, this); 23// 传输完成调用 24mnfcAdapter.setOnNdefPushCompleteCallback(this, this); 25} 26 27@Override 28protected void onResume() { 29// TODO Auto-generated method stub 30super.onResume(); 31if (mnfcAdapter != null) { 32mnfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, 33null); 34} 35} 36 37@Override 38protected void onPause() { 39// TODO Auto-generated method stub 40super.onPause(); 41if (mnfcAdapter != null) { 42mnfcAdapter.disableForegroundDispatch(this); 43} 44} 45 46// 显示接收到的数据 47void processIntent(Intent intent) { 48 49Parcelable[] rawMsgs = intent 50.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); 51NdefMessage message = (NdefMessage) rawMsgs[0]; 52String text = TextRecord.parse(message.getRecords()[0]).getText(); 53Toast.makeText(this, text, 0).show(); 54} 55 56@Override 57protected void onNewIntent(Intent intent) { 58// TODO Auto-generated method stub 59// super.onNewIntent(intent); 60processIntent(intent); 61} 62 63@Override 64public void onNdefPushComplete(NfcEvent event) { 65// TODO Auto-generated method stub 66Log.i("-----> > ", "完成"); 67} 68 69@Override 70public NdefMessage createNdefMessage(NfcEvent event) { 71// TODO Auto-generated method stub 72 73String text = mBeamText.getText().toString().trim(); 74if ("".equals(text)) { 75text = "默认文本"; 76// 当手机靠近另一部手机,使它自动打开计算器 77// NdefMessage ndefMessage = new NdefMessage( 78// new NdefRecord[] { NdefRecord 79// .createApplicationRecord("com.android.calculator2") }); 80// 81 82NdefMessage ndefMessage = new NdefMessage( 83new NdefRecord[] { creatTextRecord(text) }); 84return ndefMessage; 85} 86 87return null; 88} 89 90public NdefRecord creatTextRecord(String text) { 91 92byte[] langBytes = Locale.CHINA.getLanguage().getBytes( 93Charset.forName("US-ASCII")); 94Charset utfEncoding = Charset.forName("UTF-8"); 95byte[] textBytes = text.getBytes(utfEncoding); 96int utfBit = 0; 97char status = (char) (utfBit + langBytes.length); 98byte[] data = https://www.songbingjia.com/android/new byte[1 + langBytes.length + textBytes.length]; 99data[0] = (byte) status; 100System.arraycopy(langBytes, 0, data, 1, langBytes.length); 101System.arraycopy(textBytes, 0, data, 1 + langBytes.length, 102textBytes.length); 103 104NdefRecord ndefRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, 105NdefRecord.RTD_TEXT, new byte[0], data); 106return ndefRecord; 107 108} 109 110 }

 

    推荐阅读