Pho*_*nix 2 format android nfc mifare ndef
我想使用以下代码格式化从未使用过的 MIFARE Ultralight 卡:
NdefFormatable formatable = NdefFormatable.get(tag);
if (formatable != null) {
String result = "Afifly";
try {
formatable.connect();
try {
formatable.format(new NdefMessage(new NdefRecord(NdefRecord.TNF_EMPTY, null, null, null)));
} catch (Exception e) {
// let the user know the tag refused to format
System.out.println("error ");//+getStackTrace(e));
result = "Fail 1";
}
} catch (Exception e) {
// let the user know the tag refused to connect
System.out.println("eeeerrror 2"+e);
result = "Fail 2";
} finally {
try {
formatable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return;
}
Run Code Online (Sandbox Code Playgroud)
但在调用方法时总是抛出IOException(没有任何有意义的消息)formatable.format(...)。
我尝试了其他几张卡,结果都相同。但是,这些卡可以使用 NXP TagWriter 等进行格式化。
我已经找到了问题/答案“ Formatting a Mifare Ultralight C to NDEF ”,但这个解决方案对我不起作用。我仍然得到相同的 IOException。
标签的前四页(页 0-3)包含以下字节:
04 F1 E9 94 42 AB 4A 80 23 48 00 00 (清除所有锁定位) 00 00 00 00 (无功能容器)
因此,标签是空的并且未被锁定。
IOException调用空的 MIFARE Ultralight 标签时出现 的最可能原因NdefFormatable.format()是您的设备不支持“格式化”(即初始化为 NFC Forum Type 2 标签)该类型的标签。如果是这样的话,那么你甚至看到该NdefFormatable技术显然就是一个错误。
在这种情况下,您唯一的选择是手动执行格式化过程(有关详细信息,请参阅 NFC 论坛类型 2 标签操作规范)。这也是各种标签写入应用程序(例如 NXP TagWriter)所做的事情。对于 MIFARE Ultralight (MF0ICU1) 标签(不要尝试将其用于较大的标签!),如下所示:
NfcA nfcA = NfcA.get(tag);
if (nfcA != null) {
try {
nfcA.connect();
nfcA.transceive(new byte[] {
(byte)0xA2, // WRITE
(byte)0x03, // page = 3
(byte)0xE1, (byte)0x10, (byte)0x06, (byte)0x00 // capability container (mapping version 1.0, 48 bytes for data available, read/write allowed)
});
nfcA.transceive(new byte[] {
(byte)0xA2, // WRITE
(byte)0x04, // page = 4
(byte)0x03, (byte)0x00, (byte)0xFE, (byte)0x00 // empty NDEF TLV, Terminator TLV
});
} catch (Exception e) {
} finally {
try {
nfcA.close();
} catch (Exception e) {
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4262 次 |
| 最近记录: |