马克.
1.权限,读卡权限
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.NFC"/>
<uses-feature android:name="android.hardware.nfc" />
懒得找大概这些把,网络上大把大把的.
和类申明
<activity
android:name="com.cpu.charge.MainActivity"
android:configChanges="keyboardHidden|orientation"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowBackground="@null"
android:windowSoftInputMode="adjustUnspecified|stateAlwaysHidden" >
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<action android:name="android.nfc.action.TAG_DISCOVERED" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>
2.卡类型,需要在res文件目录下创建xml文件夹,并创建一个xml文件,定义你想要响应的卡片类型.
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.IsoDep</tech>
<tech>android.nfc.tech.NfcA</tech>
<tech>android.nfc.tech.NfcB</tech>
<tech>android.nfc.tech.NfcF</tech>
<tech>android.nfc.tech.NfcV</tech>
<tech>android.nfc.tech.Ndef</tech>
<tech>android.nfc.tech.NdefFormatable</tech>
<tech>android.nfc.tech.MifareClassic</tech>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
</resources>
3.写你的读卡的类:其中,重写protected void onNewIntent(Intent intent) 方法.
然后使用Parcelable parcelable = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Tag tag = (Tag) parcelable;
IsoDep isodep = IsoDep.get(tag);
iso_tag = new Iso7816.Tag(isodep);
iso_tag.connect();
获取响应的tag
再然后写好查询的语句....不同的卡规格不一样,你要得到卡的结构类型然后再去写语句
byte[] SELECT_FILE2 = { (byte) 0x00, (byte) 0xA4,
(byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x3F, (byte) 0x00 };// select
// 根目录MF
byte[] SELECT_FILE_DR2 = { (byte) 0x00, (byte) 0xA4,
(byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0xDF, (byte) 0x04 };// select
// DF
byte[] SELECT_FILE3 = { (byte) 0x00, (byte) 0xA4,
(byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x15, (byte) 0x1E};// select
//公共基本数据文件
byte[] SELECT_FILE_INFO2 = { (byte) 0x00, (byte) 0xB0,
(byte) 0x95, (byte) 0x0A, (byte) 0x00};// select
// info
byte[] SELECT_FILE_INFO3 = { (byte) 0x00, (byte) 0xB0,
(byte) 0x96, (byte) 0x16, (byte) 0x00};// select
然后了就用你的到的tag去获取你想要的东西
Response result_info2 = tag.executeCmd(SELECT_FILE_INFO3);// 查询info
if (result_info.isOkey()) {//判断一下获得是否正确
然后解析就好了 返回的东西和你想要的肯定有差别后面就是截取数据了
final String cardInfo2 = result_info2.toString();
String cardNumber = cardInfo2.substring(30, 64);
String cardNum = "";
for (int i = 0; i < cardNumber.length()/2; i++) {
String str = cardNumber.substring(2*i,2*(i+1));
cardNum = cardNum+str.substring(1,2);
}
好了卡号获取到了
暂时还没有写入的需求....也就懒得看了....读取的差不多就这些步骤,马克一下.