一、主活动,选择进入不同功能的页面 package com.gc.nfc; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import com.gc.nfc.ui.ReadMUActivity; import com.gc.nfc.ui.ReadTextActivity; import com.gc.nfc.ui.ReadUriActivity; import com.gc.nfc.ui.RunAppActivity; import com.gc.nfc.ui.RunUrlActivity; import com.gc.nfc.ui.WriteMUActivity; import com.gc.nfc.ui.WriteTextActivity; import com.gc.nfc.ui.WriteUriActivity; /** * Created by gc on 2016/12/8. */ public class MainActivity extends AppCompatActivity { private static final String[] strs = new String[]{ "自动运行程序(NDEF格式)", "自动打开网页(NDEF格式)", "读NFC标签中的文本数据(NDEF格式)", "写NFC标签中的文本数据(NDEF格式)", "读NFC标签中的Uri数据(NDEF格式)", "写NFC标签中的Uri数据(NDEF格式)", "读NFC标签非NDEF格式的数据(非NDEF格式)", "写NFC标签非NDEF格式的数据(非NDEF格式)" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView = (ListView) findViewById(R.id.listview); listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strs)); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switchActivity(position); } }); } private void switchActivity(int position) { switch (position) { case 0: //自动运行程序 startActivity(new Intent(this, RunAppActivity.class)); break; case 1: //自动打开网页 startActivity(new Intent(this, RunUrlActivity.class)); break; case 2: //读NFC标签中的文本数据 startActivity(new Intent(this, ReadTextActivity.class)); break; case 3: //写NFC标签中的文本数据 startActivity(new Intent(this, WriteTextActivity.class)); break; case 4: //读NFC标签中的Uri数据 startActivity(new Intent(this, ReadUriActivity.class)); break; case 5: //写NFC标签中的Uri数据 startActivity(new Intent(this, WriteUriActivity.class)); break; case 6: //读NFC标签非NDEF格式的数据 startActivity(new Intent(this, ReadMUActivity.class)); break; case 7: //写NFC标签非NDEF格式的数据 startActivity(new Intent(this, WriteMUActivity.class)); break; default: break; } } }
二、公共活动,初始化扫卡功能,其他功能活动可直接继承
package com.gc.nfc.base;import android.app.PendingIntent;import android.content.Intent;import android.nfc.NfcAdapter;import android.support.v7.app.AppCompatActivity;/** * 1.子类需要在onCreate方法中做Activity初始化。 * 2.子类需要在onNewIntent方法中进行NFC标签相关操作。 * 当launchMode设置为singleTop时,第一次运行调用onCreate方法, * 第二次运行将不会创建新的Activity实例,将调用onNewIntent方法 * 所以我们获取intent传递过来的Tag数据操作放在onNewIntent方法中执行 * 如果在栈中已经有该Activity的实例,就重用该实例(会调用实例的onNewIntent()) * 只要NFC标签靠近就执行 * Created by gc on 2016/12/8. */public class BaseNfcActivity extends AppCompatActivity { private NfcAdapter mNfcAdapter; private PendingIntent mPendingIntent; /** * 启动Activity,界面可见时 */ @Override protected void onStart() { super.onStart(); mNfcAdapter = NfcAdapter.getDefaultAdapter(this); //一旦截获NFC消息,就会通过PendingIntent调用窗口 mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()), 0); } /** * 获得焦点,按钮可以点击 */ @Override public void onResume() { super.onResume(); //设置处理优于所有其他NFC的处理 if (mNfcAdapter != null) mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null); } /** * 暂停Activity,界面获取焦点,按钮可以点击 */ @Override public void onPause() { super.onPause(); //恢复默认状态 if (mNfcAdapter != null) mNfcAdapter.disableForegroundDispatch(this); }}
三、扫卡自动启动程序package com.gc.nfc.ui; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.nfc.tech.Ndef; import android.nfc.tech.NdefFormatable; import android.os.Bundle; import android.util.Log; import android.widget.Toast; import com.gc.nfc.R; import com.gc.nfc.base.BaseNfcActivity; import java.util.ArrayList; import java.util.List; /** * 自动运行程序 * Created by gc on 2016/11/8. */ public class RunAppActivity extends BaseNfcActivity { private String mPackageName = "com.example.administrator.stationdemo";//需要启动的程序的包名 String NFC; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_run_app); } @Override public void onNewIntent(Intent intent) { if (mPackageName == null) return; //1.获取Tag对象 Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); writeNFCTag(detectedTag); } /** * 往标签写数据的方法 * * @param tag */ public void writeNFCTag(Tag tag) { if (tag == null) { return; } NdefMessage ndefMessage = new NdefMessage(new NdefRecord[]{NdefRecord .createApplicationRecord(mPackageName)}); //转换成字节获得大小 Log.e("写入程序的数据2", "====" + ndefMessage); int size = ndefMessage.toByteArray().length; try { //2.判断NFC标签的数据类型(通过Ndef.get方法) Ndef ndef = Ndef.get(tag); Log.e("NFC标签数据类型"+!ndef.isWritable(),"==="+ndef); //判断是否为NDEF标签 if (ndef != null) { ndef.connect(); //判断是否支持可写 if (!ndef.isWritable()) { return; } Log.e("NFC标签数据类型1"+ndef.getMaxSize(),"==="+size); //判断标签的容量是否够用 if (ndef.getMaxSize() < size) { return; } //3.写入数据 ndef.writeNdefMessage(ndefMessage); Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show(); } else { //当我们买回来的NFC标签是没有格式化的,或者没有分区的执行此步 //Ndef格式类 NdefFormatable format = NdefFormatable.get(tag); //判断是否获得了NdefFormatable对象,有一些标签是只读的或者不允许格式化的 if (format != null) { //连接 format.connect(); //格式化并将信息写入标签 format.format(ndefMessage); Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "写入失败", Toast.LENGTH_SHORT).show(); } } } catch (Exception e) { } } }
四、扫卡自动打开网页package com.gc.nfc.ui; import android.content.Intent; import android.net.Uri; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.nfc.tech.Ndef; import android.nfc.tech.NdefFormatable; import android.os.Bundle; import android.widget.Toast; import com.gc.nfc.R; import com.gc.nfc.base.BaseNfcActivity; /** * 自动打开网页 * Created by gc on 2016/11/8. */ public class RunUrlActivity extends BaseNfcActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_run_url); } @Override public void onNewIntent(Intent intent) { //1.获取Tag对象 Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); writeNFCTag(detectedTag); } /** * 往标签写数据的方法 * * @param tag */ public void writeNFCTag(Tag tag) { if (tag == null) { return; } NdefMessage ndefMessage = new NdefMessage(new NdefRecord[]{NdefRecord .createUri(Uri.parse("http://www.baidu.com"))}); //转换成字节获得大小 int size = ndefMessage.toByteArray().length; try { //2.判断NFC标签的数据类型(通过Ndef.get方法) Ndef ndef = Ndef.get(tag); //判断是否为NDEF标签 if (ndef != null) { ndef.connect(); //判断是否支持可写 if (!ndef.isWritable()) { return; } //判断标签的容量是否够用 if (ndef.getMaxSize() < size) { return; } //3.写入数据 ndef.writeNdefMessage(ndefMessage); Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show(); } else { //当我们买回来的NFC标签是没有格式化的,或者没有分区的执行此步 //Ndef格式类 NdefFormatable format = NdefFormatable.get(tag); //判断是否获得了NdefFormatable对象,有一些标签是只读的或者不允许格式化的 if (format != null) { //连接 format.connect(); //格式化并将信息写入标签 format.format(ndefMessage); Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "写入失败", Toast.LENGTH_SHORT).show(); } } } catch (Exception e) { } } }
五、向标签中写入文本数据(NDEF格式)package com.gc.nfc.ui; 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.widget.Toast; import com.gc.nfc.R; import com.gc.nfc.base.BaseNfcActivity; import java.nio.charset.Charset; import java.util.Locale; /** * Created by gc on 2016/12/8. */ public class WriteTextActivity extends BaseNfcActivity { private String mText = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_write_text); } @Override public void onNewIntent(Intent intent) { if (mText == null) return; //获取Tag对象 Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); NdefMessage ndefMessage = new NdefMessage( new NdefRecord[]{createTextRecord(mText)}); boolean result = writeTag(ndefMessage, detectedTag); if (result) { Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "写入失败", Toast.LENGTH_SHORT).show(); } } /** * 创建NDEF文本数据 * * @param text * @return */ public static NdefRecord createTextRecord(String text) { byte[] langBytes = Locale.CHINA.getLanguage().getBytes(Charset.forName("US-ASCII")); Charset utfEncoding = Charset.forName("UTF-8"); //将文本转换为UTF-8格式 byte[] textBytes = text.getBytes(utfEncoding); //设置状态字节编码最高位数为0 int utfBit = 0; //定义状态字节 char status = (char) (utfBit + langBytes.length); byte[] data = new byte[1 + langBytes.length + textBytes.length]; //设置第一个状态字节,先将状态码转换成字节 data[0] = (byte) status; //设置语言编码,使用数组拷贝方法,从0开始拷贝到data中,拷贝到data的1到langBytes.length的位置 System.arraycopy(langBytes, 0, data, 1, langBytes.length); //设置文本字节,使用数组拷贝方法,从0开始拷贝到data中,拷贝到data的1 + langBytes.length //到textBytes.length的位置 System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length); //通过字节传入NdefRecord对象 //NdefRecord.RTD_TEXT:传入类型 读写 NdefRecord ndefRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data); return ndefRecord; } /** * 写数据 * * @param ndefMessage 创建好的NDEF文本数据 * @param tag 标签 * @return */ public static boolean writeTag(NdefMessage ndefMessage, Tag tag) { try { Ndef ndef = Ndef.get(tag); ndef.connect(); ndef.writeNdefMessage(ndefMessage); return true; } catch (Exception e) { } return false; } }
六、读取标签中写入的文本数据(NDEF格式)package com.gc.nfc.ui; 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.Parcelable; import android.os.Bundle; import android.widget.TextView; import com.gc.nfc.R; import com.gc.nfc.base.BaseNfcActivity; import java.util.Arrays; /** * Created by gc on 2016/12/8. */ public class ReadTextActivity extends BaseNfcActivity { private TextView mNfcText; private String mTagText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_read_text); mNfcText = (TextView) findViewById(R.id.tv_nfctext); } @Override public void onNewIntent(Intent intent) { //1.获取Tag对象 Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); //2.获取Ndef的实例 Ndef ndef = Ndef.get(detectedTag); mTagText = ndef.getType() + "\nmaxsize:" + ndef.getMaxSize() + "bytes\n\n"; readNfcTag(intent); mNfcText.setText(mTagText); } /** * 读取NFC标签文本数据 */ private void readNfcTag(Intent intent) { if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) { Parcelable[] rawMsgs = intent.getParcelableArrayExtra( NfcAdapter.EXTRA_NDEF_MESSAGES); NdefMessage msgs[] = null; int contentSize = 0; if (rawMsgs != null) { msgs = new NdefMessage[rawMsgs.length]; for (int i = 0; i < rawMsgs.length; i++) { msgs[i] = (NdefMessage) rawMsgs[i]; contentSize += msgs[i].toByteArray().length; } } try { if (msgs != null) { NdefRecord record = msgs[0].getRecords()[0]; String textRecord = parseTextRecord(record); mTagText += textRecord + "\n\ntext\n" + contentSize + " bytes"; } } catch (Exception e) { } } } /** * 解析NDEF文本数据,从第三个字节开始,后面的文本数据 * * @param ndefRecord * @return */ public static String parseTextRecord(NdefRecord ndefRecord) { /** * 判断数据是否为NDEF格式 */ //判断TNF if (ndefRecord.getTnf() != NdefRecord.TNF_WELL_KNOWN) { return null; } //判断可变的长度的类型 if (!Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) { return null; } try { //获得字节数组,然后进行分析 byte[] payload = ndefRecord.getPayload(); //下面开始NDEF文本数据第一个字节,状态字节 //判断文本是基于UTF-8还是UTF-16的,取第一个字节"位与"上16进制的80,16进制的80也就是最高位是1, //其他位都是0,所以进行"位与"运算后就会保留最高位 String textEncoding = ((payload[0] & 0x80) == 0) ? "UTF-8" : "UTF-16"; //3f最高两位是0,第六位是1,所以进行"位与"运算后获得第六位 int languageCodeLength = payload[0] & 0x3f; //下面开始NDEF文本数据第二个字节,语言编码 //获得语言编码 String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII"); //下面开始NDEF文本数据后面的字节,解析出文本 String textRecord = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding); return textRecord; } catch (Exception e) { throw new IllegalArgumentException(); } } }七、向标签中写入的网址(NDEF格式)package com.gc.nfc.ui; 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.widget.Toast; import com.gc.nfc.R; import com.gc.nfc.base.BaseNfcActivity; import com.gc.nfc.UriPrefix; /** * Created by gc on 2016/12/8. */ public class WriteUriActivity extends BaseNfcActivity { private String mUri = "http://www.baidu.com"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_write_uri); } public void onNewIntent(Intent intent) { Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); NdefMessage ndefMessage = new NdefMessage(new NdefRecord[]{createUriRecord(mUri)}); boolean result = writeTag(ndefMessage, detectedTag); if (result) { Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "写入失败", Toast.LENGTH_SHORT).show(); } } /** * 将Uri转成NdefRecord * @param uriStr * @return */ public static NdefRecord createUriRecord(String uriStr) { byte prefix = 0; for (Byte b : UriPrefix.URI_PREFIX_MAP.keySet()) { String prefixStr = UriPrefix.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; } /** * 写入标签 * @param message * @param tag * @return */ public static 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); return true; } } catch (Exception e) { } return false; } }八、读取标签中写入的网址(NDEF格式)package com.gc.nfc.ui; import android.content.Intent; import android.net.Uri; 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.util.Log; import android.widget.TextView; import com.gc.nfc.R; import com.gc.nfc.base.BaseNfcActivity; import com.gc.nfc.UriPrefix; import java.nio.charset.Charset; import java.util.Arrays; /** * Created by gc on 2016/12/8. */ public class ReadUriActivity extends BaseNfcActivity { private TextView mNfcText; private String mTagText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_read_uri); mNfcText = (TextView) findViewById(R.id.tv_nfctext); } @Override public void onNewIntent(Intent intent) { //获取Tag对象 Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); //获取Ndef的实例 Ndef ndef = Ndef.get(detectedTag); mTagText = ndef.getType() + "\n max size:" + ndef.getMaxSize() + " bytes\n\n"; readNfcTag(intent); mNfcText.setText(mTagText); } /** * 读取NFC标签Uri */ private void readNfcTag(Intent intent) { if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) { Parcelable[] rawMsgs = intent.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]; Log.i("JAVA", ndefRecord.toString()); Uri uri = parse(ndefRecord); Log.i("JAVA", "uri:" + uri.toString()); mTagText += uri.toString() + "\n\nUri\n" + contentSize + " bytes"; } catch (Exception e) { } } } /** * 解析NdefRecord中Uri数据 * * @param record * @return */ public static Uri parse(NdefRecord record) { short tnf = record.getTnf(); if (tnf == NdefRecord.TNF_WELL_KNOWN) { return parseWellKnown(record); } else if (tnf == NdefRecord.TNF_ABSOLUTE_URI) { return parseAbsolute(record); } throw new IllegalArgumentException("Unknown TNF " + tnf); } /** * 处理绝对的Uri * 没有Uri识别码,也就是没有Uri前缀,存储的全部是字符串 * * @param ndefRecord 描述NDEF信息的一个信息段,一个NdefMessage可能包含一个或者多个NdefRecord * @return */ private static Uri parseAbsolute(NdefRecord ndefRecord) { //获取所有的字节数据 byte[] payload = ndefRecord.getPayload(); Uri uri = Uri.parse(new String(payload, Charset.forName("UTF-8"))); return uri; } /** * 处理已知类型的Uri * * @param ndefRecord * @return */ private static Uri parseWellKnown(NdefRecord ndefRecord) { //判断数据是否是Uri类型的 if (!Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_URI)) return null; //获取所有的字节数据 byte[] payload = ndefRecord.getPayload(); String prefix = UriPrefix.URI_PREFIX_MAP.get(payload[0]); byte[] prefixBytes = prefix.getBytes(Charset.forName("UTF-8")); byte[] fullUri = new byte[prefixBytes.length + payload.length - 1]; System.arraycopy(prefixBytes, 0, fullUri, 0, prefixBytes.length); System.arraycopy(payload, 1, fullUri, prefixBytes.length, payload.length - 1); Uri uri = Uri.parse(new String(fullUri, Charset.forName("UTF-8"))); return uri; } }
九、向标签中写入非NDEF格式的数据package com.gc.nfc.ui; import android.content.Intent; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.nfc.tech.MifareUltralight; import android.os.Bundle; import android.util.Log; import android.widget.Toast; import com.gc.nfc.R; import com.gc.nfc.base.BaseNfcActivity; import java.nio.charset.Charset; import javax.crypto.Cipher; /** * Created by gc on 2016/12/8. */ public class WriteMUActivity extends BaseNfcActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_write_mu); } @Override public void onNewIntent(Intent intent) { Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); String[] techList = tag.getTechList(); boolean haveMifareUltralight = false; for (String tech : techList) { if (tech.indexOf("MifareUltralight") >= 0) { Log.e("写入数据","===="+tech.indexOf("MifareUltralight") ); haveMifareUltralight = true; break; } } if (!haveMifareUltralight) { Log.e("写入数据","===="+!haveMifareUltralight); Toast.makeText(this, "不支持MifareUltralight数据格式", Toast.LENGTH_SHORT).show(); return; } writeTag(tag); } public void writeTag(Tag tag) { MifareUltralight ultralight = MifareUltralight.get(tag); try { ultralight.connect(); ultralight.writePage(4, "".getBytes(Charset.forName("GB2312"))); ultralight.writePage(5, "".getBytes(Charset.forName("GB2312"))); ultralight.writePage(6, "".getBytes(Charset.forName("GB2312"))); ultralight.writePage(7, "".getBytes(Charset.forName("GB2312"))); Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) { } finally { try { ultralight.close(); } catch (Exception e) { } } } }十、读取标签中写入的非NDEF格式的数据package com.gc.nfc.ui; import android.content.Intent; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.nfc.tech.MifareUltralight; import android.os.Bundle; import android.widget.Toast; import com.gc.nfc.R; import com.gc.nfc.base.BaseNfcActivity; import java.nio.charset.Charset; /** * Created by gc on 2016/12/8. */ public class ReadMUActivity extends BaseNfcActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_read_mu); } @Override public void onNewIntent(Intent intent) { Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); String[] techList = tag.getTechList(); boolean haveMifareUltralight = false; for (String tech : techList) { if (tech.indexOf("MifareUltralight") >= 0) { haveMifareUltralight = true; break; } } if (!haveMifareUltralight) { Toast.makeText(this, "不支持MifareUltralight数据格式", Toast.LENGTH_SHORT).show(); return; } String data = readTag(tag); if (data != null) Toast.makeText(this, data, Toast.LENGTH_SHORT).show(); } public String readTag(Tag tag) { MifareUltralight ultralight = MifareUltralight.get(tag); try { ultralight.connect(); byte[] data = ultralight.readPages(4); return new String(data, Charset.forName("GB2312")); } catch (Exception e) { } finally { try { ultralight.close(); } catch (Exception e) { } } return null; } }
NFC扫卡各种功能
最新推荐文章于 2024-04-07 13:18:56 发布