Android 使用NFC通讯坑点记录

private void readNFC(Intent intent) {
SealIdManager manager = new SealIdManager();
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] _tagId = tag.getId();
byte[] resK = new byte[5];
System.arraycopy(_tagId, 0, resK, 0, 4);
resK[4] = 0x00;
String key = HexConverter.toHexString(ComputeKeyA(resK))
.replace(" ", "").toUpperCase();
boolean auth = false;
MifareClassic mifareClassic = MifareClassic.get(tag);
try {
mifareClassic.connect();
byte bytes[] = new byte[16];
//0扇区为m1卡id,不可更改,从0扇区读取(锁号在第0扇区)
auth = mifareClassic.authenticateSectorWithKeyA(0, hexStringToByte(key));//密码验证
if (auth) {
//0块区为m1生厂商信息,不可更改,从1块区读取(锁号在第1扇区)
byte[] data = mifareClassic.readBlock(1);
bytes = Arrays.copyOf(data, 16);
Log.e("读取", manager.GetSealId(bytes));
sendHandle(SCAN_SUCCESSFUL, manager.GetSealId(data));
}
sendHandle(SCAN_ERROR, BaseRfidActivity.this.getText(R.string.rfid_msg_read_fail).toString());
} catch (Exception e) {
sendHandle(SCAN_ERROR, BaseRfidActivity.this.getText(R.string.rfid_msg_read_fail).toString());
}
finally {
try {
mifareClassic.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

    byte[] data = mifareClassic.readBlock(1);这行代码需要注意如果需要进行debug的话,得缓存数据,不能直接调试。数据通讯一次后就会消失,容易报IO错误。

你可能感兴趣的:(Android 使用NFC通讯坑点记录)