如何select NT3H2111 nfc芯片上的扇区
How to select sector on NT3H2111 nfc chip
我正在使用 android phone 向 NT3H2111 nfc 芯片的 EEPROM 读写一些数据。
更改扇区的方法似乎有效,但仍然如此,当我尝试读取内容时扇区没有更改,它仍在扇区零上。
@Override
protected void onNewIntent(Intent intent) {
try {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
ntagHandler = new NtagHandler(tag);
ntagHandler.connect();
if(mode == AppMode.READ) {
Ve95_DataModelHandler.readSectionNames(ve95DataModel, ntagHandler);
populateView();
} else if(mode == AppMode.WRITE) {
populateDataModel();
Ve95_DataModelHandler.writeSectionNames(ve95DataModel, ntagHandler);
}
ntagHandler.close();
ImageView img = findViewById(R.id.imageViewNFCConnect);
if(img != null) {
img.setVisibility(View.INVISIBLE);
mode = AppMode.READ;
}
} catch(IOException e) {
Toast.makeText(this, "Caught exception: " + e.toString(), Toast.LENGTH_LONG).show();
}
}
...
public boolean sectorSelect(int sector) throws IOException {
byte[] cmd_sel1 = { (byte)0xC2, (byte)0xFF };
byte[] cmd_sel2 = { (byte)sector, (byte)0x00, (byte)0x00, (byte)0x00 };
byte[] result1 = nfca.transceive(cmd_sel1);
if (result1 == null) {
throw new TagLostException();
} else if ((result1.length == 1) && ((result1[0] & 0x00A) == 0x000)) {
return false;
} else {
try {
byte[] result2 = nfca.transceive(cmd_sel2);
if (result2 == null) {
throw new TagLostException();
} else if ((result2.length == 1) && ((result2[0] & 0x00A) == 0x000)) {
// NACK response according to DigitalProtocol
return false;
} else {
return true;
}
} catch (Exception e) {
// passive ACK
Log.d(TAG, "sectorSelect caught exception, but succeeded anyway");
return true;
}
}
}
...
/*
* Read section names from EEPROM and put them into the data model
*/
public static boolean readSectionNames(@NonNull Ve95_DataModel ve95DataModel,
@NonNull NtagHandler ntagHandler)
{
try {
Log.d(TAG, "Reading section names");
boolean retVal = ntagHandler.sectorSelect((byte) 1);
Log.d(TAG, String.format("sectorSelect returned %b", retVal));
byte[] data = ntagHandler.fastRead((byte) BASE_ADDRESS_SECTION_NAMES, (byte) 0x2C);
Log.d(TAG, Utils.bytesToHex(data));
byte[] subArray;
int size = 16;
for(int i=0; i < 20; i++) {
subArray = Arrays.copyOfRange(data, i * size, (i+1) * size);
String name = new String(subArray, StandardCharsets.UTF_8);
ve95DataModel.setSectionName(i, name);
Log.d(TAG, String.format("section %d name %s", i, name));
}
return true;
} catch (IOException e) {
return false;
}
}
我得到了这个结果,但是从数据中我可以看到该扇区仍然是零,并且没有更改为一号扇区。
D/Ve95_DataModelHandler: 读段名
D/: sectorSelect 捕捉到异常,但无论如何都成功了
D/Ve95_DataModelHandler:扇区选择返回真值
D/Ve95_DataModelHandler: 04,B2,87,CA,D4,64,80,00,44,00,00,00,00....
有人知道如何更改扇区吗?
此致
亨里克
自己解决了。安装了 1K 芯片而不是 2K 芯片。难怪它不起作用:)
我正在使用 android phone 向 NT3H2111 nfc 芯片的 EEPROM 读写一些数据。
更改扇区的方法似乎有效,但仍然如此,当我尝试读取内容时扇区没有更改,它仍在扇区零上。
@Override
protected void onNewIntent(Intent intent) {
try {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
ntagHandler = new NtagHandler(tag);
ntagHandler.connect();
if(mode == AppMode.READ) {
Ve95_DataModelHandler.readSectionNames(ve95DataModel, ntagHandler);
populateView();
} else if(mode == AppMode.WRITE) {
populateDataModel();
Ve95_DataModelHandler.writeSectionNames(ve95DataModel, ntagHandler);
}
ntagHandler.close();
ImageView img = findViewById(R.id.imageViewNFCConnect);
if(img != null) {
img.setVisibility(View.INVISIBLE);
mode = AppMode.READ;
}
} catch(IOException e) {
Toast.makeText(this, "Caught exception: " + e.toString(), Toast.LENGTH_LONG).show();
}
}
...
public boolean sectorSelect(int sector) throws IOException {
byte[] cmd_sel1 = { (byte)0xC2, (byte)0xFF };
byte[] cmd_sel2 = { (byte)sector, (byte)0x00, (byte)0x00, (byte)0x00 };
byte[] result1 = nfca.transceive(cmd_sel1);
if (result1 == null) {
throw new TagLostException();
} else if ((result1.length == 1) && ((result1[0] & 0x00A) == 0x000)) {
return false;
} else {
try {
byte[] result2 = nfca.transceive(cmd_sel2);
if (result2 == null) {
throw new TagLostException();
} else if ((result2.length == 1) && ((result2[0] & 0x00A) == 0x000)) {
// NACK response according to DigitalProtocol
return false;
} else {
return true;
}
} catch (Exception e) {
// passive ACK
Log.d(TAG, "sectorSelect caught exception, but succeeded anyway");
return true;
}
}
}
...
/*
* Read section names from EEPROM and put them into the data model
*/
public static boolean readSectionNames(@NonNull Ve95_DataModel ve95DataModel,
@NonNull NtagHandler ntagHandler)
{
try {
Log.d(TAG, "Reading section names");
boolean retVal = ntagHandler.sectorSelect((byte) 1);
Log.d(TAG, String.format("sectorSelect returned %b", retVal));
byte[] data = ntagHandler.fastRead((byte) BASE_ADDRESS_SECTION_NAMES, (byte) 0x2C);
Log.d(TAG, Utils.bytesToHex(data));
byte[] subArray;
int size = 16;
for(int i=0; i < 20; i++) {
subArray = Arrays.copyOfRange(data, i * size, (i+1) * size);
String name = new String(subArray, StandardCharsets.UTF_8);
ve95DataModel.setSectionName(i, name);
Log.d(TAG, String.format("section %d name %s", i, name));
}
return true;
} catch (IOException e) {
return false;
}
}
我得到了这个结果,但是从数据中我可以看到该扇区仍然是零,并且没有更改为一号扇区。
D/Ve95_DataModelHandler: 读段名
D/: sectorSelect 捕捉到异常,但无论如何都成功了
D/Ve95_DataModelHandler:扇区选择返回真值
D/Ve95_DataModelHandler: 04,B2,87,CA,D4,64,80,00,44,00,00,00,00....
有人知道如何更改扇区吗?
此致
亨里克
自己解决了。安装了 1K 芯片而不是 2K 芯片。难怪它不起作用:)