public void writeNfcV(Tag tag, int oldValue, int newValue) throws Exception { int currentValue = readNfcV(tag); if (((oldValue != -1) && (oldValue != 0) && (currentValue != oldValue)) || (((oldValue == -1) || (oldValue == 0)) && ((currentValue != 0) && currentValue != -1))) { callbackContext.error(FALSE_TAG); return; } byte[] data = ByteBuffer.allocate(4).putInt(newValue).array(); if (tag == null) { callbackContext.error("NULL"); return; } NfcV nfcv = NfcV.get(tag); nfcv.connect(); byte[] request = new byte[7]; request[0] = 0x00; // flag request[1] = 0x21; // command: WRITE ONE BLOCK request[2] = (byte) block; // IMMER im gleichen Block speichern request[3] = (byte) data[0]; request[4] = (byte) data[1]; request[5] = (byte) data[2]; request[6] = (byte) data[3]; try { byte[] response = nfcv.transceive(request); if (response[0] != (byte) 0x00) { callbackContext.error("Error code: " + response[1]); } } catch (IOException e) { if (e.getMessage().equals("Tag was lost.")) { // continue, because of Tag bug } else { callbackContext.error("Couldn't write on Tag"); throw e; } } nfcv.close(); int result = readNfcV(tag); PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result); callbackContext.sendPluginResult(pluginResult); }
public int readNfcV(Tag tag) throws Exception { if (tag == null) { callbackContext.error("NULL"); } byte[] response = new byte[4]; NfcV nfcv = NfcV.get(tag); if (nfcv != null) { try { nfcv.connect(); byte[] request = new byte[] { (byte) 0x00, // flag (byte) 0x20, // command: READ ONE BLOCK (byte) block // IMMER im gleichen Block }; response = nfcv.transceive(request); } catch (IOException e) { callbackContext.error(nfcv.toString()); } finally { try { nfcv.close(); } catch (IOException e) { } } } // 1. Byte: Block locking status byte[] value = new byte[] {response[1], response[2], response[3], response[4]}; return ByteBuffer.wrap(value).order(java.nio.ByteOrder.BIG_ENDIAN).getInt(); }