Пример #1
0
  /**
   * Write all the blocks in a Tag to the Mifare tag.
   *
   * @param t
   * @throws IOException
   */
  public void write(Tag t) throws IOException {

    if (t.getSectorCount() > mKeys.getSectorCount()) throw new IOException("Too few keys");

    mTag.connect();

    int sectors = mTag.getSectorCount();

    try {

      for (int s = 0; s < sectors; ++s) {

        // Authenticate for each sector (try B key, then A key)
        if (!mTag.authenticateSectorWithKeyB(s, mKeys.getKeyB(s))
            && !mTag.authenticateSectorWithKeyA(s, mKeys.getKeyA(s)))
          throw new IOException("Auth error");

        // Write to tag. Skip block 0 and the trailer of each sector
        int blockOffset = mTag.sectorToBlock(s);
        int lastBlock = blockOffset + mTag.getBlockCountInSector(s);

        // Skip block 0
        blockOffset = blockOffset == 0 ? 1 : blockOffset;

        for (int b = blockOffset; b < lastBlock; ++b) {
          mTag.writeBlock(b, t.getBlock(b));
          if (mProgressListener != null)
            mProgressListener.publishProgress((100 * b) / t.getBlockCount());
        }
      }
    } finally {
      mTag.close();
    }
  }
Пример #2
0
 /**
  * Write a block of 16 byte data to tag.
  *
  * @param sectorIndex The sector to where the data should be written
  * @param blockIndex The block to where the data should be written
  * @param data 16 byte of data.
  * @param key The Mifare Classic key for the given sector.
  * @param useAsKeyB If true, key will be treated as key B for authentication.
  * @return The return codes are:<br>
  *     <ul>
  *       <li>0 - Everything went fine.
  *       <li>1 - Sector index is out of range.
  *       <li>2 - Block index is out of range.
  *       <li>3 - Data are not 16 byte.
  *       <li>4 - Authentication went wrong.
  *       <li>-1 - Error while writing to tag.
  *     </ul>
  *
  * @see #authenticate(int, byte[], boolean)
  */
 public int writeBlock(
     int sectorIndex, int blockIndex, byte[] data, byte[] key, boolean useAsKeyB) {
   if (mMFC.getSectorCount() - 1 < sectorIndex) {
     return 1;
   }
   if (mMFC.getBlockCountInSector(sectorIndex) - 1 < blockIndex) {
     return 2;
   }
   if (data.length != 16) {
     return 3;
   }
   if (!authenticate(sectorIndex, key, useAsKeyB)) {
     return 4;
   }
   // Write block.
   int block = mMFC.sectorToBlock(sectorIndex) + blockIndex;
   try {
     mMFC.writeBlock(block, data);
   } catch (IOException e) {
     Log.e(LOG_TAG, "Error while writing block to tag.", e);
     return -1;
   }
   return 0;
 }
Пример #3
0
 @Override
 protected void writeChunk(int chunkIndex, byte[] chunk) throws IOException {
   mifareTag.writeBlock(chunkIndex, chunk);
 }