我对Android NFC协议有疑问:我有一台装有Mifare Classic的设备,尺寸为4K。我尝试使用KeyB写入块27但是我总是得到IOException:Trasceive Failed。我尝试用writeBlock(int blockIndex,byte [] bArray)方法编写。 授权为True,设备已连接。相同的KeyB用于读取扇区27,并且该操作正常工作。 使用相同的代码(使用KeyA),我可以在扇区1中编写。
在下面的代码中,我尝试写入扇区27的第一个块(块108,其中108是块的绝对索引)。
我尝试将代码放入MainThread或第二个Thread中,但没有任何改变。 任何想法???
...
MyNfc.mAdapter = NfcAdapter.getDefaultAdapter(this);
MyNfc.mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// Setup an intent filter for all MIME based dispatches
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
try {
ndef.addDataType("*/*");
} catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
MyNfc.mFilters = new IntentFilter[] { ndef, };
// Setup a tech list for all NfcF tags
MyNfc.mTechLists = new String[][] { new String[] { MifareClassic.class.getName() } };
……
try {
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
MifareClassic mfc = MifareClassic.get(tagFromIntent);
MyNfc.MifareID = tagFromIntent.getId();
MyNfc.keyB = GYMCalcoloAlg.GYMCalcoloAlgorithm(MyNfc.MifareID, MyNfc.keyB);
mfc.connect();
mfc.setTimeout(10000);
boolean auth = false;
auth = mfc.authenticateSectorWithKeyB(27,MyNfc.keyB);
if (auth) {
String text = "Hello, World!";
byte[] value = text.getBytes();
byte[] toWrite = new byte[MifareClassic.BLOCK_SIZE];
for (int i=0; i<MifareClassic.BLOCK_SIZE; i++) {
if (i < value.length)
toWrite[i] = value[i];
else
toWrite[i] = 0;
}
mfc.writeBlock(108, toWrite); <-- THIS OPERATION FAILED: IOException: Transceive Failed.
}
}
catch(Exception ex){
ex.getMessage();
}