12、NFC技术:读写NFC标签中的Uri数据
2014-07-20 17:29
537 查看
[b]功能实现,如下代码所示:[/b]
读写NFC标签的Uri 主Activity
import cn.read.write.uri.library.UriRecord; import android.app.Activity; import android.content.Intent; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.nfc.tech.Ndef; import android.os.Bundle; import android.view.View; import android.widget.TextView; import android.widget.Toast; /** * 读写NFC标签的Uri * @author dr * */ public class ReadWriteUriMainActivity extends Activity { private TextView mSelectUri; private String mUri; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_read_write_uri_main); mSelectUri = (TextView) findViewById(R.id.textview_uri); } public void onClick_SelectUri(View view) { Intent intent = new Intent(this, UriListActivity.class); startActivityForResult(intent, 1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1 && resultCode == 1) { mUri = data.getStringExtra("uri"); mSelectUri.setText(mUri); } } public void onNewIntent(Intent intent) { if (mUri == null) { Intent myIntent = new Intent(this, ShowNFCTagContentActivity.class); myIntent.putExtras(intent); myIntent.setAction(NfcAdapter.ACTION_NDEF_DISCOVERED); startActivity(myIntent); } else { // write nfc Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); NdefMessage ndefMessage = new NdefMessage( new NdefRecord[] { createUriRecord(mUri) }); if (writeTag(ndefMessage, tag)) { mUri = null; mSelectUri.setText(""); } } } public NdefRecord createUriRecord(String uriStr) { byte prefix = 0; for (Byte b : UriRecord.URI_PREFIX_MAP.keySet()) { String prefixStr = UriRecord.URI_PREFIX_MAP.get(b).toLowerCase(); if ("".equals(prefixStr)) continue; if (uriStr.toLowerCase().startsWith(prefixStr)) { prefix = b; uriStr = uriStr.substring(prefixStr.length()); break; } } byte[] data = new byte[1 + uriStr.length()]; data[0] = prefix; System.arraycopy(uriStr.getBytes(), 0, data, 1, uriStr.length()); NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], data); return record; } boolean writeTag(NdefMessage message, Tag tag) { int size = message.toByteArray().length; try { Ndef ndef = Ndef.get(tag); if (ndef != null) { ndef.connect(); if (!ndef.isWritable()) { return false; } if (ndef.getMaxSize() < size) { return false; } ndef.writeNdefMessage(message); Toast.makeText(this, "ok", Toast.LENGTH_LONG).show(); return true; } } catch (Exception e) { // TODO: handle exception } return false; } }
读写NFC标签的Uri 主xml
<?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" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick_SelectUri" android:text="选择Uri" /> <TextView android:id="@+id/textview_uri" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="16sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="请将NFC标签靠近手机背面读取或写入Uri" android:textColor="#F00" android:textSize="16sp" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/read_nfc_tag" /> </LinearLayout>
显示URI列表Activity
import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; /** * 显示URI列表 * @author dr * */ public class UriListActivity extends ListActivity implements OnItemClickListener { private String uris[] = new String[] { "http://www.google.com", "http://www.apple.com", "http://developer.apple.com", "http://www.126.com", "ftp://192.168.17.160", "https://192.168.17.120", "smb://192.168.17.100" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, uris); setListAdapter(arrayAdapter); getListView().setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(); intent.putExtra("uri", uris[position]); setResult(1, intent); finish(); } }
显示NFC标签内容Activity
import android.app.Activity; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.nfc.tech.Ndef; import android.os.Bundle; import android.os.Parcelable; import android.widget.TextView; import cn.read.write.uri.library.UriRecord; /** * 显示NFC标签内容 * @author dr * */ public class ShowNFCTagContentActivity extends Activity { private TextView mTagContent; private Tag mDetectedTag; private String mTagText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show_nfctag_content); mTagContent = (TextView) findViewById(R.id.textview_tag_content); mDetectedTag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG); Ndef ndef = Ndef.get(mDetectedTag); mTagText = ndef.getType() + "\n max size:" + ndef.getMaxSize() + " bytes\n\n"; readNFCTag(); mTagContent.setText(mTagText); } private void readNFCTag() { if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) { Parcelable[] rawMsgs = getIntent().getParcelableArrayExtra( NfcAdapter.EXTRA_NDEF_MESSAGES); NdefMessage ndefMessage = null; int contentSize = 0; if (rawMsgs != null) { if (rawMsgs.length > 0) { ndefMessage = (NdefMessage) rawMsgs[0]; contentSize = ndefMessage.toByteArray().length; } else { return; } } try { NdefRecord ndefRecord = ndefMessage.getRecords()[0]; UriRecord uriRecord = UriRecord.parse(ndefRecord); mTagText += uriRecord.getUri().toString() + "\n\nUri\n" + contentSize + " bytes"; } catch (Exception e) { // TODO: handle exception } } } }
显示NFC标签内容xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textview_tag_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="6dp" android:textSize="16sp" /> </LinearLayout>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.eoe.read.write.uri" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.NFC" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".ReadWriteUriMainActivity" android:label="读写NFC标签的Uri" android:launchMode="singleTask" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" /> <data android:scheme="https" /> <data android:scheme="ftp" /> </intent-filter> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> </intent-filter> </activity> <activity android:name="cn.eoe.read.write.uri.ShowNFCTagContentActivity" android:label="显示NFC标签内容" /> <activity android:name="cn.eoe.read.write.uri.UriListActivity" android:label="选择Uri" /> </application> </manifest>
[b]DEMO下载地址:http://download.csdn.net/detail/androidsj/7680247[/b]
相关文章推荐
- Android硬件开发之——读写NFC标签中URI数据
- 10、NFC技术:读写NFC标签中的文本数据
- NFC标签初始化、NFC标签读写数据功能、NFC标签前台调度系统
- 13、NFC技术:读写非NDEF格式的数据
- Android 使用NFC通过标签协议栈读写标签数据
- Android基础——NFC标签初始化设置、NFC标签读写数据和获取ID、NFC标签前台调度系统
- Android设备读写NFC标签
- 11、NFC技术:NDEF Uri格式解析
- Android平台上读写NFC标签 原理
- Android开发——NFC标签读写
- Android NFC 标签 读写
- Android NFC 使用MifareUltralight读写数据
- 数据挖掘概念与技术12--数据立方体的计算和多路数组聚集详解
- 管理 XML 数据: 标签 URI
- Android 使用手机NFC的读取NFC标签数据的方法
- 详解Android平台上读写NFC标签
- Android&nbsp;NFC开发读写NDEF数据实例
- Avro技术应用_7. 将 Avro 数据读写到一个 Parquet 文件中 -- 待完善
- Android应用实例之----MifareUltralight格式的nfc标签读写
- Android平台上读写NFC标签