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();
 }
  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);
  }
Ejemplo n.º 3
0
  public void onNewIntent(Intent intent) {
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    byte[] id = tag.getId();
    NfcV nfcMessage = NfcV.get(tag);

    try {
      nfcMessage.connect();

      int resultIndex = 0;
      int blockIndex = 0;
      int blocks = 4; // 0 means 1 block, so it's +1 than 'blocks'

      byte[] cmd =
          new byte[] {
            (byte) 0x60,
            (byte) 0x23,
            (byte) 0x00,
            (byte) 0x00,
            (byte) 0x00,
            (byte) 0x00,
            (byte) 0x00,
            (byte) 0x00,
            (byte) 0x00,
            (byte) 0x00, // placeholder for tag UID
            (byte) blockIndex,
            (byte) blocks
          };

      System.arraycopy(id, 0, cmd, 2, 8);
      byte[] data = nfcMessage.transceive(cmd);

      int start = 2;
      byte[] results = new byte[data.length];

      for (int i = 0; i <= blocks; i++) {
        for (int b = start; b < start + 4; b++) {
          // First data block may contain zero bytes, which does not denote end of book code.
          // this caused troubles with some books
          if (data[b] == 0 && i > 1) {
            break;
          }
          results[resultIndex] = data[b];
          resultIndex++;
        }
        start += 5;
      }
      nfcMessage.close();

      results = ArrayUtils.subarray(results, 3, resultIndex);
      String res = new String(results);

      HashMap<String, String> temp = new HashMap<String, String>();
      temp.put("sent", "false");
      temp.put("to_send", "false");
      temp.put("code", res);

      dataList.add(0, temp);
      adapter.notifyDataSetChanged();

      this.toast.cancel();

      BookAsyncProcessor p =
          new BookAsyncProcessor(adapter, sharedPref.getString("service_url", ""));
      p.execute(temp);

    } catch (IOException e) {
      this.toast.show();
    }
  }