예제 #1
0
  private boolean writeToTag(NdefMessage message, NdefMessage messageWithoutPhoto, Ndef ndef)
      throws IOException, FormatException {
    int size = message.toByteArray().length;
    int sizeSmall = messageWithoutPhoto.toByteArray().length;

    ndef.connect();

    if (!ndef.isWritable()) {
      toast("Tag is read-only.");
      return false;
    }

    NdefMessage messageToWrite = message;

    if (ndef.getMaxSize() < size) {
      if (ndef.getMaxSize() < sizeSmall) {
        toast(getResources().getString(R.string.write_tag_failed_size, ndef.getMaxSize(), size));
        return false;
      } else {
        messageToWrite = messageWithoutPhoto;
      }
    }

    ndef.writeNdefMessage(messageToWrite);
    return true;
  }
  private void write(String text, Tag tag) throws IOException, FormatException {

    NdefRecord[] records = {createWellKnownRecord(text)};
    NdefMessage message = new NdefMessage(records);
    // Get an instance of Ndef for the tag.
    Ndef ndef = Ndef.get(tag);
    // Enable I/O
    ndef.connect();
    // Write the message
    ndef.writeNdefMessage(message);
    // Close the connection
    ndef.close();
  }
예제 #3
0
    /** Metoda zapisujaca w tle wiadomosc do tagu NFC Przed sprawdzeniem formatuje tag */
    @Override
    protected Void doInBackground(Void... nop) {
      int size = this.msg.toByteArray().length;
      try {
        Ndef ndef = Ndef.get(this.tag);
        if (ndef == null) {
          NdefFormatable formatable = NdefFormatable.get(this.tag);

          if (formatable != null) {
            try {
              formatable.connect();
              try {
                formatable.format(this.msg);
              } catch (Exception e) {
                this.returnText = R.string.nfc_tag_refused_to_format;
              }
            } catch (Exception e) {
              this.returnText = R.string.nfc_tag_refused_to_connect;
            } finally {
              formatable.close();
            }
          } else {
            this.returnText = R.string.nfc_tag_does_not_support_ndef;
          }
        } else {
          ndef.connect();

          try {
            if (!ndef.isWritable()) {
              this.returnText = R.string.nfc_tag_is_read_only;
            } else if (ndef.getMaxSize() < size) {
              this.returnText = R.string.nfc_message_is_too_big;
            } else {
              ndef.writeNdefMessage(this.msg);
              this.returnText = R.string.nfc_tag_saved;
              this.returnStatus = Activity.RESULT_OK;
            }
          } catch (Exception e) {
            this.returnText = R.string.nfc_tag_refused_to_connect;
          } finally {
            ndef.close();
          }
        }
      } catch (Exception e) {
        Log.e(TAG, "Exception when writing tag", e);
        this.returnText = R.string.nfc_general_exception;
      }

      return (null);
    }
 public boolean writeNdefMessageToTag(NdefMessage message, Tag detectedTag) {
   int size = message.toByteArray().length;
   try {
     Ndef ndef = Ndef.get(detectedTag);
     if (ndef != null) {
       ndef.connect();
       if (!ndef.isWritable()) {
         Toast.makeText(this, "Tag is read-only.", Toast.LENGTH_SHORT).show();
         return false;
       }
       if (ndef.getMaxSize() < size) {
         Toast.makeText(
                 this,
                 "The data cannot written to tag,Tag capacity is "
                     + ndef.getMaxSize()
                     + " bytes, message is "
                     + size
                     + " bytes.",
                 Toast.LENGTH_SHORT)
             .show();
         return false;
       }
       ndef.writeNdefMessage(message);
       ndef.close();
       Toast.makeText(this, "Message is written tag.", Toast.LENGTH_SHORT).show();
       return true;
     } else {
       NdefFormatable ndefFormat = NdefFormatable.get(detectedTag);
       if (ndefFormat != null) {
         try {
           ndefFormat.connect();
           ndefFormat.format(message);
           ndefFormat.close();
           Toast.makeText(this, "The data is written to the tag ", Toast.LENGTH_SHORT).show();
           return true;
         } catch (IOException e) {
           Toast.makeText(this, "Failed to format tag", Toast.LENGTH_SHORT).show();
           return false;
         }
       } else {
         Toast.makeText(this, "NDEF is not supported", Toast.LENGTH_SHORT).show();
         return false;
       }
     }
   } catch (Exception e) {
     Toast.makeText(this, "Write opreation is failed", Toast.LENGTH_SHORT).show();
   }
   return false;
 }
예제 #5
0
 public void writeTag(NdefMessage message, Tag tag) {
   int size = message.toByteArray().length;
   String mess;
   try {
     Ndef ndef = Ndef.get(tag);
     if (ndef != null) {
       ndef.connect();
       if (!ndef.isWritable()) {
         this.status = 0;
         this.message = "Tag is read-only";
       }
       if (ndef.getMaxSize() < size) {
         mess = "Tag capacity is " + ndef.getMaxSize() + " bytes, message is " + size + " bytes.";
         this.status = 0;
         this.message = mess;
       }
       ndef.writeNdefMessage(message);
       if (writeProtect) ndef.makeReadOnly();
       mess = "Wrote message to pre-formatted tag.";
       this.status = 1;
       this.message = mess;
     } else {
       NdefFormatable format = NdefFormatable.get(tag);
       if (format != null) {
         try {
           format.connect();
           format.format(message);
           mess = "Formatted tag and wrote message";
           this.status = 1;
           this.message = mess;
         } catch (IOException e) {
           mess = "Failed to format tag.";
           this.status = 0;
           this.message = mess;
         }
       } else {
         mess = "Tag doesn't support NDEF.";
         this.status = 0;
         this.message = mess;
       }
     }
   } catch (Exception e) {
     mess = "Failed to write tag";
     this.status = 0;
     this.message = mess;
   }
 }
  /**
   * タグ書き込みを行う
   *
   * @param message
   * @param tag
   * @return
   */
  boolean writeTag(NdefMessage message, Tag tag) {
    int size = message.toByteArray().length;

    try {
      Ndef ndef = Ndef.get(tag);
      if (ndef != null) {
        ndef.connect();

        if (!ndef.isWritable()) {
          toast("Tag is read-only.");
          return false;
        }
        if (ndef.getMaxSize() < size) {
          toast("Tag capacity is " + ndef.getMaxSize() + " bytes, message is " + size + " bytes.");
          return false;
        }

        ndef.writeNdefMessage(message);
        mVib.vibrate(300);
        toast("Wrote message to pre-formatted tag.");
        return true;
      } else {
        NdefFormatable format = NdefFormatable.get(tag);
        if (format != null) {
          try {
            format.connect();
            format.format(message);
            mVib.vibrate(300);
            toast("Formatted tag and wrote message");
            return true;
          } catch (IOException e) {
            toast("Failed to format tag.");
            return false;
          }
        } else {
          toast("Tag doesn't support NDEF.");
          return false;
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      toast("Failed to write tag");
    }
    return false;
  }
 // El método write es el más importante, será el que se encargue de crear el mensaje
 // y escribirlo en nuestro tag.
 private void write(String text, Tag tag) throws IOException, FormatException {
   try {
     // Creamos un array de elementos NdefRecord. Este Objeto representa un registro del mensaje
     // NDEF
     // Para crear el objeto NdefRecord usamos el método createRecord(String s)
     NdefRecord[] records = {createRecord(text)};
     // NdefMessage encapsula un mensaje Ndef(NFC Data Exchange Format). Estos mensajes están
     // compuestos por varios registros encapsulados por la clase NdefRecord
     NdefMessage message = new NdefMessage(records);
     // Obtenemos una instancia de Ndef del Tag
     Ndef ndef = Ndef.get(tag);
     ndef.connect();
     ndef.writeNdefMessage(message);
     ndef.close();
   } catch (Exception e) {
     Log.d("WRITE", "Excepción: " + e.getMessage());
   }
 } // write
예제 #8
0
 private boolean writeTxtToTag(String textToWrite, Tag tag) throws IOException, FormatException {
   if (tag == null) {
     Log.d(TAG, "writeTxtToTag: tag is null!");
     Toast.makeText(this, "Tag is null", Toast.LENGTH_SHORT).show();
     return false;
   }
   NdefRecord record = createRecord(textToWrite);
   if (record == null) {
     Log.d(TAG, "writeTxtToTag: record is null!");
     return false;
   } else {
     NdefRecord[] records = {record};
     NdefMessage message = new NdefMessage(records);
     Ndef ndef = Ndef.get(tag);
     ndef.connect();
     ndef.writeNdefMessage(message);
     ndef.close();
   }
   return true;
 }
 public void writeTag(NdefMessage ndefMessage, Tag tag) {
   try {
     Ndef ndef = Ndef.get(tag);
     if (ndef != null) {
       ndef.connect();
       ndef.writeNdefMessage(ndefMessage);
     } else {
       NdefFormatable format = NdefFormatable.get(tag);
       if (format != null) {
         try {
           format.connect();
           format.format(ndefMessage);
         } catch (IOException e) {
           Log.e(Constants.LOGTAG, e.getMessage(), e);
         }
       }
     }
   } catch (Exception e) {
     Log.e(Constants.LOGTAG, e.getMessage(), e);
   }
 }
예제 #10
0
  /** Actually writes data into tag (sticker). */
  private void writeTag(NdefMessage message, Tag tag) {
    int size = message.toByteArray().length;

    try {
      Ndef ndef = Ndef.get(tag);
      if (ndef != null) {
        ndef.connect();

        if (!ndef.isWritable()) {
          toast(R.string.tag_read_only);
          return;
        }
        if (ndef.getMaxSize() < size) {
          toast(R.string.exceeded_tag_capacity);
          return;
        }

        ndef.writeNdefMessage(message);
        toast(R.string.sticker_write_success);
      } else {
        NdefFormatable format = NdefFormatable.get(tag);
        if (format != null) {
          try {
            format.connect();
            format.format(message);
            toast(R.string.sticker_write_success);
          } catch (IOException e) {
            toast(R.string.sticker_write_error);
          }
        } else {
          toast(R.string.sticker_write_error);
        }
      }
    } catch (FormatException e) {
      toast(R.string.sticker_write_error);
    } catch (IOException e) {
      toast(R.string.sticker_write_error);
    }
  }
예제 #11
0
  boolean writeTag(NdefMessage message, Tag tag) {
    int size = message.toByteArray().length;

    try {
      Ndef ndef = Ndef.get(tag);
      if (ndef != null) {
        ndef.connect();

        if (!ndef.isWritable()) {
          Toast.makeText(
                  this, "Cannot write to this tag. This tag is read-only.", Toast.LENGTH_LONG)
              .show();
          return false;
        }
        if (ndef.getMaxSize() < size) {
          Toast.makeText(
                  this,
                  "Cannot write to this tag. Message size ("
                      + size
                      + " bytes) exceeds this tag's capacity of "
                      + ndef.getMaxSize()
                      + " bytes.",
                  Toast.LENGTH_LONG)
              .show();
          return false;
        }

        ndef.writeNdefMessage(message);
        Toast.makeText(this, "A pre-formatted tag was successfully updated.", Toast.LENGTH_LONG)
            .show();
        return true;
      } else {
        NdefFormatable format = NdefFormatable.get(tag);
        if (format != null) {
          try {
            format.connect();
            format.format(message);
            Toast.makeText(
                    this, "This tag was successfully formatted and updated.", Toast.LENGTH_LONG)
                .show();
            return true;
          } catch (IOException e) {
            Toast.makeText(
                    this, "Cannot write to this tag due to I/O Exception.", Toast.LENGTH_LONG)
                .show();
            return false;
          }
        } else {
          Toast.makeText(
                  this,
                  "Cannot write to this tag. This tag does not support NDEF.",
                  Toast.LENGTH_LONG)
              .show();
          return false;
        }
      }
    } catch (Exception e) {
      Toast.makeText(this, "Cannot write to this tag due to an Exception.", Toast.LENGTH_LONG)
          .show();
    }

    return false;
  }