List of usage examples for android.nfc.tech Ndef getMaxSize
public int getMaxSize()
From source file:Main.java
public static boolean writeNFCTag(NdefMessage message, Tag tag) { int size = message.toByteArray().length; try {// w w w. j a v a 2s .com Ndef ndef = Ndef.get(tag); if (ndef != null) { ndef.connect(); if (!ndef.isWritable()) { return false; } if (ndef.getMaxSize() < size) { return false; } ndef.writeNdefMessage(message); return true; } else { NdefFormatable format = NdefFormatable.get(tag); if (format != null) { try { format.connect(); format.format(message); return true; } catch (IOException e) { return false; } } else { return false; } } } catch (Exception e) { return false; } }
From source file:Main.java
static JSONObject ndefToJSON(Ndef ndef) { JSONObject json = new JSONObject(); if (ndef != null) { try {/* w w w . j a v a 2 s. c o m*/ Tag tag = ndef.getTag(); // tag is going to be null for NDEF_FORMATABLE until NfcUtil.parseMessage is refactored if (tag != null) { json.put("id", byteArrayToJSON(tag.getId())); json.put("techTypes", new JSONArray(Arrays.asList(tag.getTechList()))); } json.put("type", translateType(ndef.getType())); json.put("maxSize", ndef.getMaxSize()); json.put("isWritable", ndef.isWritable()); json.put("ndefMessage", messageToJSON(ndef.getCachedNdefMessage())); // Workaround for bug in ICS (Android 4.0 and 4.0.1) where // mTag.getTagService(); of the Ndef object sometimes returns null // see http://issues.mroland.at/index.php?do=details&task_id=47 try { json.put("canMakeReadOnly", ndef.canMakeReadOnly()); } catch (NullPointerException e) { json.put("canMakeReadOnly", JSONObject.NULL); } } catch (JSONException e) { Log.e(TAG, "Failed to convert ndef into json: " + ndef.toString(), e); } } return json; }
From source file:com.hybris.mobile.activity.NFCWriteActivity.java
@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); // default failed setResult(RESULT_CANCELED);//w w w . j a va 2 s. c om if (NFCUtil.supportsNdef(tag)) { Ndef ndef = Ndef.get(tag); try { int maxSize = ndef.getMaxSize(); int messageSize = this.msg.toByteArray().length; if (ndef.isWritable() && maxSize > messageSize) { ndef.connect(); ndef.writeNdefMessage(this.msg); if (getResources().getBoolean(R.bool.nfc_read_only)) { if (ndef.canMakeReadOnly()) { boolean success = ndef.makeReadOnly(); if (!success) Toast.makeText(this, "Unable to make tag readonly!", Toast.LENGTH_LONG).show(); else Toast.makeText(this, "Tag is now readonly!", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "This tag cannot be made readonly!", Toast.LENGTH_LONG).show(); } } setResult(RESULT_OK); showDialogFragmentWithMessage(R.string.nfc_tag_written); } else { showDialogFragmentWithMessage(R.string.error_nfc_readonly_size); } } catch (IOException e) { LoggingUtils.e(LOG_CAT, getString(R.string.error_nfc_io), e, Hybris.getAppContext()); } catch (FormatException e) { LoggingUtils.e(LOG_CAT, getString(R.string.error_nfc_format), e, Hybris.getAppContext()); } finally { try { ndef.close(); } catch (IOException e) { } } } else if (NFCUtil.supportsNdefFormatable(tag)) { NdefFormatable ndefFormatable = NdefFormatable.get(tag); try { ndefFormatable.connect(); if (getResources().getBoolean(R.bool.nfc_read_only)) { ndefFormatable.formatReadOnly(this.msg); } else { ndefFormatable.format(this.msg); } setResult(RESULT_OK); showDialogFragmentWithMessage(R.string.nfc_tag_written); } catch (IOException e) { LoggingUtils.e(LOG_CAT, getString(R.string.error_nfc_io), e, Hybris.getAppContext()); } catch (FormatException e) { LoggingUtils.e(LOG_CAT, getString(R.string.error_nfc_format), e, Hybris.getAppContext()); } finally { try { ndefFormatable.close(); } catch (IOException e) { } } } else { showDialogFragmentWithMessage(R.string.error_nfc_no_ndef); } }
From source file:jp.co.brilliantservice.android.writertdtext.HomeActivity.java
/** * NdefMessageNdef??????/*from w w w. jav a 2s . c o m*/ * * @param ndef * @param message */ private void writeNdefToNdefTag(Ndef ndef, NdefMessage message) { int size = message.toByteArray().length; try { ndef.connect(); if (!ndef.isWritable()) { Toast.makeText(getApplicationContext(), "Read Only...", Toast.LENGTH_SHORT).show(); return; } if (ndef.getMaxSize() < size) { Toast.makeText(getApplicationContext(), "Over Size...", Toast.LENGTH_SHORT).show(); return; } ndef.writeNdefMessage(message); } catch (IOException e) { throw new RuntimeException(e); } catch (FormatException e) { throw new RuntimeException(e); } finally { try { ndef.close(); } catch (IOException e) { // ignore } } }
From source file:mai.whack.StickyNotesActivity.java
boolean writeTag(NdefMessage message, Tag tag) { int size = message.toByteArray().length; try {/* w w w. ja v a 2 s. c o m*/ 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); toast("Wrote message to pre-formatted tag."); return true; } else { NdefFormatable format = NdefFormatable.get(tag); if (format != null) { try { format.connect(); format.format(message); 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) { toast("Failed to write tag"); } return false; }
From source file:st.alr.homA.ActivityQuickpublishNfc.java
private boolean write(String text, Tag tag) { boolean success = true; NdefRecord aar = NdefRecord.createApplicationRecord("st.alr.homA"); NdefRecord[] records = { createRecord(text), aar }; NdefMessage ndefMsg = new NdefMessage(records); try {/* ww w.j a va2 s .c o m*/ Ndef ndef = Ndef.get(tag); if (ndef != null) { ndef.connect(); if (!ndef.isWritable()) { publishProgress(getResources().getString(R.string.nfcWriteDialogTagReadOnly), false); success = false; } if (ndef.getMaxSize() < ndefMsg.getByteArrayLength()) { publishProgress( getResources().getString(R.string.nfcWriteDialogTagCapacityIs) + ndef.getMaxSize() + " byte" + getResources().getString(R.string.nfcWriteDialogTagMessageSizeIs) + ndefMsg.getByteArrayLength() + "byte.", false); success = false; } ndef.writeNdefMessage(ndefMsg); success = true; } else { NdefFormatable format = NdefFormatable.get(tag); if (format != null) { try { format.connect(); format.format(ndefMsg); publishProgress(getResources().getString(R.string.nfcWriteDialogTagFormatedAndWrote), true); success = true; } catch (IOException e) { publishProgress(getResources().getString(R.string.nfcWriteDialogTagFailedToFormat), false); success = false; } } else { publishProgress(getResources().getString(R.string.nfcWriteDialogTagNoNDEFSupport), false); success = false; } } } catch (Exception e) { Log.e(this.toString(), getResources().getString(R.string.nfcWriteDialogTagFailedToWrite) + " (" + e.getMessage() + ")", e); } return success; }
From source file:org.protocoderrunner.apprunner.AppRunnerActivity.java
@Override public void onNewIntent(Intent intent) { MLog.d(TAG, "New intent " + intent); if (intent.getAction() != null) { MLog.d(TAG, "Discovered tag with intent: " + intent); // mText.setText("Discovered tag " + ++mCount + " with intent: " + // intent); Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); String nfcID = StrUtils.bytetostring(tag.getId()); // if there is mContext message waiting to be written if (PNFC.nfcMsg != null) { MLog.d(TAG, "->" + PNFC.nfcMsg); PNFC.writeTag(this, tag, PNFC.nfcMsg); onNFCWrittenListener.onNewTag(); onNFCWrittenListener = null; PNFC.nfcMsg = null;//from ww w. j av a 2 s.co m // read the nfc tag info } else { // get NDEF tag details Ndef ndefTag = Ndef.get(tag); if (ndefTag == null) { return; } int size = ndefTag.getMaxSize(); // tag size boolean writable = ndefTag.isWritable(); // is tag writable? String type = ndefTag.getType(); // tag type String nfcMessage = ""; // get NDEF message details NdefMessage ndefMesg = ndefTag.getCachedNdefMessage(); if (ndefMesg != null) { NdefRecord[] ndefRecords = ndefMesg.getRecords(); int len = ndefRecords.length; String[] recTypes = new String[len]; // will contain the // NDEF record types String[] recPayloads = new String[len]; // will contain the // NDEF record types for (int i = 0; i < len; i++) { recTypes[i] = new String(ndefRecords[i].getType()); recPayloads[i] = new String(ndefRecords[i].getPayload()); MLog.d(TAG, "qq " + i + " " + recTypes[i] + " " + recPayloads[i]); } nfcMessage = recPayloads[0]; } onNFCListener.onNewTag(nfcID, nfcMessage); } } }
From source file:ch.bfh.instacircle.NetworkActiveActivity.java
/** * Writes an NFC Tag//from ww w .java2 s .co m * * @param tag * The reference to the tag * @param message * the message which should be writen on the message * @return true if successful, false otherwise */ public boolean writeTag(Tag tag, NdefMessage message) { AlertDialog alertDialog = new AlertDialog.Builder(this).create(); alertDialog.setTitle("InstaCircle - write NFC Tag failed"); alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); try { // see if tag is already NDEF formatted Ndef ndef = Ndef.get(tag); if (ndef != null) { ndef.connect(); if (!ndef.isWritable()) { Log.d(TAG, "This tag is read only."); alertDialog.setMessage("This tag is read only."); alertDialog.show(); return false; } // work out how much space we need for the data int size = message.toByteArray().length; if (ndef.getMaxSize() < size) { Log.d(TAG, "Tag doesn't have enough free space."); alertDialog.setMessage("Tag doesn't have enough free space."); alertDialog.show(); return false; } ndef.writeNdefMessage(message); Log.d(TAG, "Tag written successfully."); } else { // attempt to format tag NdefFormatable format = NdefFormatable.get(tag); if (format != null) { try { format.connect(); format.format(message); Log.d(TAG, "Tag written successfully!"); } catch (IOException e) { alertDialog.setMessage("Unable to format tag to NDEF."); alertDialog.show(); Log.d(TAG, "Unable to format tag to NDEF."); return false; } } else { Log.d(TAG, "Tag doesn't appear to support NDEF format."); alertDialog.setMessage("Tag doesn't appear to support NDEF format."); alertDialog.show(); return false; } } } catch (Exception e) { Log.d(TAG, "Failed to write tag"); return false; } alertDialog.setTitle("InstaCircle"); alertDialog.setMessage("NFC Tag written successfully."); alertDialog.show(); return true; }
From source file:com.example.multi_ndef.Frag_Write.java
/** * Format a tag and write our NDEF message */// w w w .j ava 2s. c om //@SuppressLint("NewApi") private boolean writeTag(Tag tag) { // record to launch Play Store if app is not installed NdefRecord appRecord = NdefRecord.createApplicationRecord("com.example.multi_ndef"); try { ENGLISH = Locale.ENGLISH; encodeInUtf8 = true; } catch (Exception e) { Toast toast = Toast.makeText(getApplicationContext(), "Locale Error " + e.toString(), Toast.LENGTH_SHORT); toast.show(); } try { textRecord = createTextRecord(getText(), ENGLISH, encodeInUtf8); } catch (Exception e) { Toast toast = Toast.makeText(getApplicationContext(), "Text Conversion error " + e.toString(), Toast.LENGTH_SHORT); toast.show(); } try { uriRecord = NdefRecord.createUri(getUri()); } catch (Exception e) { Toast toast = Toast.makeText(getApplicationContext(), "Uri Conversion error " + e.toString(), Toast.LENGTH_SHORT); toast.show(); } byte[] mimeBytes = MimeType.AppName.getBytes(Charset.forName("US-ASCII")); byte[] mimeBytesBT = MimeType.AppNameBT.getBytes(Charset.forName("US-ASCII")); byte[] v_data = VCard(); // Here data is written byte[] payload = data(); // payload in hex //Comments by neha - 3 Jul 2014 byte[] payloadBT = btData(); NdefRecord VcardRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "text/x-vcard".getBytes(), new byte[0], v_data); NdefRecord BTcardRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytesBT, new byte[0], payloadBT); NdefRecord cardRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload); NdefRecord SMSRecord = NdefRecord.createUri(SMSpayload); NdefRecord MailRecord = NdefRecord.createUri(Mailpayload); NdefRecord TeleRecord = NdefRecord.createUri(Telepayload); NdefRecord LocationRecord = NdefRecord.createUri(Locationpayload); NdefMessage message = new NdefMessage(new NdefRecord[] { cardRecord, textRecord, uriRecord, BTcardRecord, VcardRecord, SMSRecord, MailRecord, TeleRecord, LocationRecord, appRecord }); // ringProgressDialog.dismiss(); try { // see if tag is already NDEF formatted Ndef ndef = Ndef.get(tag); if (ndef != null) { ndef.connect(); if (!ndef.isWritable()) { displayMessage("Read-only tag."); return false; } // work out how much space we need for the data int size = message.toByteArray().length; if (ndef.getMaxSize() < size) { displayMessage("Tag doesn't have enough free space."); return false; } ndef.writeNdefMessage(message); displayMessage("Multiple NDEF Records written to tag successfully."); return true; } else { // attempt to format tag NdefFormatable format = NdefFormatable.get(tag); if (format != null) { try { format.connect(); format.format(message); displayMessage("Tag written successfully!\nClose this app and scan tag."); return true; } catch (IOException e) { displayMessage("Unable to format tag to NDEF."); return false; } } else { displayMessage("Tag doesn't appear to support NDEF format."); return false; } } } catch (Exception e) { displayMessage("Failed to write tag"); } return false; }
From source file:net.networksaremadeofstring.rhybudd.WriteNFCActivity.java
public void WriteTag(final Tag receivedTag) { tagHandler.sendEmptyMessage(TAG_IO_IN_PROGRESS); (new Thread() { public void run() { //This could go all kinds of weird Ndef thisNdef = null; try { thisNdef = Ndef.get(receivedTag); } catch (Exception e) { BugSenseHandler.sendExceptionMessage("WriteNFCActivity", "WriteTag", e); e.printStackTrace();//from ww w. j av a 2 s.c o m } if (null == thisNdef) { NdefFormatable formatter = null; try { formatter = NdefFormatable.get(receivedTag); formatter.connect(); formatter.format( new NdefMessage(new NdefRecord[] { NdefRecord.createApplicationRecord("io.d0") })); formatter.close(); thisNdef = Ndef.get(receivedTag); } catch (Exception d) { BugSenseHandler.sendExceptionMessage("WriteNFCActivity", "WriteTag", d); d.printStackTrace(); tagHandler.sendEmptyMessage(FORMATEXCEPTION); } } try { if (null == thisNdef) { throw new FormatException("No NDEF Tag returned from get"); } else { thisNdef.connect(); } if (thisNdef.isWritable()) { //Is this a 203 or larger? if (thisNdef.getMaxSize() < aaRecord.toByteArray().length + idRecord.toByteArray().length) { /*Log.i("WriteTag","This tag was too big. tried to write " + (aaRecord.toByteArray().length + idRecord.toByteArray().length) + " to " + thisNdef.getMaxSize()); idRecord = NdefRecord.createMime("text/plain", Integer.toString(tagMetaData.getInt("i")).getBytes(Charset.forName("US-ASCII"))); Log.i("WriteTag Size Check", "Writing " + (idRecord.toByteArray().length + aaRecord.toByteArray().length) + " to " + thisNdef.getMaxSize());*/ tagHandler.sendEmptyMessage(SIZE_ERROR); } else { //Log.i("WriteTag Size Check", "Writing " + (aaRecord.toByteArray().length + idRecord.toByteArray().length) + " to " + thisNdef.getMaxSize()); NdefMessage tagMsg = new NdefMessage(new NdefRecord[] { idRecord, aaRecord }); //Log.i("WriteTag Size Check", "Wrote " + tagMsg.getByteArrayLength()); thisNdef.writeNdefMessage(tagMsg); thisNdef.makeReadOnly(); thisNdef.close(); tagHandler.sendEmptyMessage(WRITE_SUCCESS); } } else { tagHandler.sendEmptyMessage(READONLY); } } catch (IOException e) { BugSenseHandler.sendExceptionMessage("WriteNFCActivity", "WriteTag", e); tagHandler.sendEmptyMessage(IOEXCEPTION); } catch (FormatException e) { BugSenseHandler.sendExceptionMessage("WriteNFCActivity", "WriteTag", e); e.printStackTrace(); tagHandler.sendEmptyMessage(FORMATEXCEPTION); } } }).start(); }