Esempio n. 1
0
 private boolean formatAndWrite(NdefFormatable format, NdefMessage message)
     throws FormatException {
   try {
     format.connect();
     format.format(message);
     return true;
   } catch (IOException e) {
     return false;
   }
 }
Esempio n. 2
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;
 }
Esempio n. 4
0
  boolean writeTag(NdefMessage message, NdefMessage messageWithoutPhoto, Tag tag) {
    try {
      Ndef ndef = Ndef.get(tag);
      if (ndef != null) {
        return writeToTag(message, messageWithoutPhoto, ndef);
      } else {
        NdefFormatable format = NdefFormatable.get(tag);
        if (format != null) {
          if (formatAndWrite(format, message) || formatAndWrite(format, messageWithoutPhoto)) {
            return true;
          } else {
            toast(getString(R.string.write_tag_failed_format));
            return false;
          }
        } else {
          toast(getString(R.string.write_tag_failed_ndef));
          return false;
        }
      }
    } catch (Exception e) {
      toast(getString(R.string.write_tag_failed));
    }

    return false;
  }
Esempio n. 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;
  }
 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);
   }
 }
  /** 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);
    }
  }
  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;
  }