Данные не передаются, а код застрял в IOException
Я действительно новичок в разработке Android и Java. Я разработал приложение, которое должно отправлять настройки микроконтроллера через NFC. После этого он должен получить значения АЦП от микро, используя NFC, но мой код застрял в IOException (сообщение об ошибке: передача тега не удалась11). Любая помощь будет более чем полезной. Я вставил свой код ниже.
Спасибо!
//communication and tag methods
private Tag currentTag;
private void resolveIntent(Intent intent) {
String action = intent.getAction();
//check if the tag is ISO15693 and display message
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
text_view.setText("Tag discovered!");
text_val="Tag discovered!";
//Log.i("life cycle", "NfcAdapter.ACTION_TECH_DISCOVERED");
currentTag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
new NfcVReaderTask().execute(currentTag);// read ADC data in background
}
}
//parsing function
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 3];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 3] = hexArray[v >>> 4];
hexChars[j * 3 + 1] = hexArray[v & 0x0F];
hexChars[j * 3 + 2] = ' ';
}
return new String(hexChars);
}
/**
*
* Background task for reading the data. Do not block the UI thread while reading.
*
*/
private class NfcVReaderTask extends AsyncTask<Tag, Void, String> {
@Override
protected void onPostExecute(String result) {
//Log.i("Life cycle", "NFC thread start");
}
@Override
protected String doInBackground(Tag... params) {
Tag tag = params[0];
readTagData(tag);
return null;
}
}
//read data
private void readTagData(Tag tag) {
byte[] id = tag.getId();
boolean techFound = false;
for (String tech : tag.getTechList()) {
// checking for NfcV
if (tech.equals(NfcV.class.getName())) {
techFound = true;
// Get an instance of NfcV for the given tag:
NfcV nfcv_senseTag = NfcV.get(tag);
try {
nfcv_senseTag.connect();
text_val="Tag connected";
}catch (IOException e) {
text_val="Tag connection lost";
return;
}
//read register test
byte[] cmd = new byte[] {
// (byte)0x18, // Always needed, everything after this 18 is sent over the air, response is given in the text box below
(byte)0x02, // Flags (always use same)
(byte)0x20, // ISO15693 command code, in this case it is Read Single Block
(byte)b_val, // Block number
};
byte[] systeminfo;
try {
systeminfo = nfcv_senseTag.transceive(cmd);
}catch (IOException e) {
text_val="Tag transfer failed10";
//Log.i("Tag data", "transfer failed");
return;
}
//Log.i("Tag data", "result= " + bytesToHex(systeminfo));
//write to block 2
cmd = new byte[] {
// (byte)0x18, // Always needed, everything after this 18 is sent over the air, response is given in the text box below
(byte)0x02, // Flags (always use same)
(byte)0x21, // ISO15693 command code, in this case it is Write Single Block
(byte)0x02, //block number
(byte)0x11, //reg1 Reference-ADC1 Configuration Register DECIMATION 12 BIT
(byte)0x11, //reg2 ADC2 Sensor Configuration Register
(byte)0x10, //reg3 ADC0 Sensor Configuration Register
(byte)0x00, //reg4 Internal Sensor Configuration Register
(byte)0x00, //reg5 Initial Delay Period Setup Register
(byte)0x00, //reg6 JTAG Enable Password Register
(byte)0x00, //reg7 Initial Delay Period Register
(byte)0x00, //reg8 Initial Delay Period Register
};
byte[] ack;
try {
ack = nfcv_senseTag.transceive(cmd);
}catch (IOException e) {
text_val="Tag transfer failed11";
//Log.i("Tag data", "transfer failed");
return;
}
//Log.i("Tag data", "ack= " + bytesToHex(ack));
while(b_val>0) {
//write 01 00 04 00 01 01 00 40 to block 0
cmd = new byte[]{
// (byte)0x18, // Always needed, everything after this 18 is sent over the air, response is given in the text box below
(byte) 0x02, // Flags (always use same)
(byte) 0x21, // ISO15693 command code, in this case it is Write Single Block
(byte) 0x00, //block number
(byte) 0x21, //Start bit is set, after this is written this starts the sampling process, interrupt enabled for On/Off
(byte) 0x00, //Status byte
(byte) 0x07, //Reference resistor, thermistor, ADC0 sensor selected
(byte) 0x00, //Frequency register, this is do not care since only one sample or pass is done
(byte) 0x01, //only one pass is needed
(byte) 0x01, //No averaging selected
(byte) 0x0E, //Interrupt enabled, push pull active high options selected
(byte) 0x40, //Selected using thermistor
};
if(dig_op==1)
cmd[3]|=0x40;
ack = new byte[]{0x01};
try {
ack = nfcv_senseTag.transceive(cmd);
} catch (IOException e) {
text_val="Tag transfer failed12";
//Log.i("Tag data", "transfer failed");
return;
}
//Log.i("Tag data", "ack= " + bytesToHex(ack));
//poll status byte 00
cmd = new byte[]{
// (byte)0x18, // Always needed, everything after this 18 is sent over the air, response is given in the text box below
(byte) 0x02, // Flags (always use same)
(byte) 0x20, // ISO15693 command code, in this case it is Read Single Block
(byte) 0x00, // Block number
};
byte[] new_info;
do {
try {
new_info = nfcv_senseTag.transceive(cmd);
} catch (IOException e) {
text_val="Tag transfer failed13"; // new version requires exception handling on each step
//Log.i("Tag data", "transfer failed");
return;
}
} while (new_info[2] != 0x02);
//Read data on 09
cmd = new byte[]{
// (byte)0x18, // Always needed, everything after this 18 is sent over the air, response is given in the text box below
(byte) 0x02, // Flags (always use same)
(byte) 0x20, // ISO15693 command code, in this case it is Read Single Block
(byte) 0x09, // Block number
};
byte[] reading;
try {
reading = nfcv_senseTag.transceive(cmd);
//Value.setText("Value: " + bytesToHex(reading) );
} catch (IOException e) {
text_val="Tag transfer failed14"; // new version requires exception handling on each step
//Log.i("Tag data", "transfer failed");
return;
}
f_val = bytesToHex(reading);
//Log.i("Tag data", "ADC result= " + f_val);
//Read data confirmation on 0A
cmd = new byte[]{
// (byte)0x18, // Always needed, everything after this 18 is sent over the air, response is given in the text box below
(byte) 0x02, // Flags (always use same)
(byte) 0x20, // ISO15693 command code, in this case it is Read Single Block
(byte) 0x0A, // Block number
};
try {
new_info = nfcv_senseTag.transceive(cmd);
} catch (IOException e) {
text_val="Tag transfer failed15"; // new version requires exception handling on each step
//Log.i("Tag data", "transfer failed");
return;
}
//Log.i("Tag data", "ADC confirmation= " + bytesToHex(new_info));
}
try {
nfcv_senseTag.close();
} catch (IOException e) {
//Log.i("Tag data", "transfer failed and stopped");
text_val="Tag disconnection failed";
return;
}
text_val="Tag disconnected";
}
}
}