資源簡介
支持Mifare卡(NFCA)和NFCB卡讀寫的demo。
1、 m1卡讀寫數據和修改控制位和秘鑰都可以,增值減值可能是用的不對,沒效果。http://download.csdn.net/detail/qust_lizhijun/9001243 鏈接是控制位的不同對于數據讀寫和控制位、密鑰改寫的影響。
2、二代證就是nfcb模式,然并卵,數據都是加密的,能讀寫也看不懂,所以只寫了個接口使用方式。

代碼片段和文件信息
package?com.straw.nfcdemo;
import?java.io.IOException;
import?java.util.ArrayList;
import?org.apache.http.util.ByteArrayBuffer;
import?android.app.Activity;
import?android.content.Intent;
import?android.nfc.NfcAdapter;
import?android.nfc.Tag;
import?android.nfc.tech.MifareClassic;
import?android.nfc.tech.NfcB;
import?android.os.AsyncTask;
import?android.os.Bundle;
import?android.text.method.ScrollingMovementMethod;
import?android.view.View;
import?android.view.View.onclickListener;
import?android.widget.Button;
import?android.widget.TextView;
import?android.widget.Toast;
public?class?MainActivity?extends?Activity?implements?onclickListener?{
private?TextView?tv;
private?Button?readCardBtn?addBtn?minusBtn?modifyBtn;
private?MifareClassic?mTag;
private?NfcB?nfcbTag;
private?int?flag;
@Override
protected?void?onCreate(Bundle?savedInstanceState)?{
//?TODO?Auto-generated?method?stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initWidget();
onNewIntent(getIntent());
}
@Override
protected?void?onNewIntent(Intent?intent)?{
if?(NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()))?{
tv.setText(“卡ID=“
+?ByteArrayToHexString(intent
.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
Tag?tag?=?intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String[]?techList?=?tag.getTechList();
ArrayList?list?=?new?ArrayList();
for?(String?string?:?techList)?{
list.add(string);
System.out.println(“tech=“?+?string);
}
if?(list.contains(“android.nfc.tech.NfcB“))?{
nfcbTag?=?NfcB.get(tag);
try?{
nfcbTag.connect();
if?(nfcbTag.isConnected())?{
System.out.println(“已連接“);
Toast.makeText(MainActivity.this?“身份證已連接“
Toast.LENGTH_SHORT).show();
new?CommandAsyncTask().execute();
}
//?nfcbTag.close();
}?catch?(IOException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}
}?else?if?(list.contains(“android.nfc.tech.MifareClassic“))?{
mTag?=?MifareClassic.get(tag);
if?(mTag?!=?null)?{
try?{
mTag.connect();
if?(mTag.isConnected())?{
System.out.println(“已連接“);
}?else?{
Toast.makeText(MainActivity.this?“請貼卡后再操作“
Toast.LENGTH_SHORT).show();
}
}?catch?(IOException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}
}
if?(flag?==?0)?{
tv.setText(readTag(mTag));
}?else?if?(flag?==?1)?{
addValue(mTag);
}?else?if?(flag?==?2)?{
minusValue(mTag);
}?else?if?(flag?==?3)?{
modifyControl(mTag);
}
}
}
}
private?String?ByteArrayToHexString(byte[]?inarray)?{?//?converts?byte
//?arrays?to?string
int?i?j?in;
String[]?hex?=?{?“0“?“1“?“2“?“3“?“4“?“5“?“6“?“7“?“8“?“9“?“A“
“B“?“C“?“D“?“E“?“F“?};
String?out?=?““;
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????475??2015-08-17?10:12??NfcDemo\.classpath
?????文件????????843??2015-07-25?09:12??NfcDemo\.project
?????文件????????177??2015-07-25?09:12??NfcDemo\.settings\org.eclipse.jdt.core.prefs
?????文件???????1371??2015-08-17?10:12??NfcDemo\AndroidManifest.xm
?????文件??????51394??2015-07-25?09:12??NfcDemo\ic_launcher-web.png
?????文件????1184450??2015-07-25?09:12??NfcDemo\libs\android-support-v4.jar
?????文件????????781??2015-07-25?09:12??NfcDemo\proguard-project.txt
?????文件????????563??2015-07-25?09:13??NfcDemo\project.properties
?????文件???????7658??2015-07-25?09:12??NfcDemo\res\drawable-hdpi\ic_launcher.png
?????文件???????3777??2015-07-25?09:12??NfcDemo\res\drawable-mdpi\ic_launcher.png
?????文件??????12516??2015-07-25?09:12??NfcDemo\res\drawable-xhdpi\ic_launcher.png
?????文件??????24777??2015-07-25?09:12??NfcDemo\res\drawable-xxhdpi\ic_launcher.png
?????文件???????1710??2015-08-14?10:13??NfcDemo\res\layout\activity_main.xm
?????文件?????????77??2015-07-25?09:12??NfcDemo\res\values\strings.xm
?????文件????????330??2015-08-15?09:55??NfcDemo\res\xm
?????文件??????12015??2015-08-17?10:12??NfcDemo\src\com\straw\nfcdemo\MainActivity.java
?????目錄??????????0??2015-08-17?10:14??NfcDemo\src\com\straw\nfcdemo
?????目錄??????????0??2015-08-17?10:14??NfcDemo\src\com\straw
?????目錄??????????0??2015-08-17?10:14??NfcDemo\res\drawable-hdpi
?????目錄??????????0??2015-07-25?09:12??NfcDemo\res\drawable-ldpi
?????目錄??????????0??2015-08-17?10:14??NfcDemo\res\drawable-mdpi
?????目錄??????????0??2015-08-17?10:14??NfcDemo\res\drawable-xhdpi
?????目錄??????????0??2015-08-17?10:14??NfcDemo\res\drawable-xxhdpi
?????目錄??????????0??2015-08-17?10:14??NfcDemo\res\layout
?????目錄??????????0??2015-08-17?10:14??NfcDemo\res\values
?????目錄??????????0??2015-08-17?10:14??NfcDemo\res\xm
?????目錄??????????0??2015-08-17?10:14??NfcDemo\src\com
?????目錄??????????0??2015-08-17?10:14??NfcDemo\.settings
?????目錄??????????0??2015-07-25?09:12??NfcDemo\assets
?????目錄??????????0??2015-08-17?10:14??NfcDemo\libs
............此處省略6個文件信息
- 上一篇:java開發工程師述職報告
- 下一篇:JAVA程序設計_具有發牌洗牌功能的紙牌游戲
評論
共有 條評論