List of usage examples for android.nfc NdefMessage toByteArray
public byte[] toByteArray()
The NDEF Message is formatted as per the NDEF 1.0 specification, and the byte array is suitable for network transmission or storage in an NFC Forum NDEF compatible tag.
This method will not chunk any records, and will always use the short record (SR) format and omit the identifier field when possible.
From source file:Main.java
public static boolean writeNFCTag(NdefMessage message, Tag tag) { int size = message.toByteArray().length; try {/*ww w. j a va 2s . c o m*/ 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:us.rader.tapset.ItemListActivity.java
/** * Handle the response from a request to write to a {@link Tag} * // w ww. j av a 2 s . c o m * @param activity * the {@link Activity} that received the result * * @param resultCode * the result code passed by {@link WriteTagActivity} using * {@link Activity#setResult(int)} or * {@link Activity#setResult(int, Intent)} * * @param resultIntent * the result {@link Intent} passed by the * {@link WriteTagActivity} using * {@link Activity#setResult(int, Intent)} (may be * <code>null</code>) */ public static void handleWriteTagResult(Activity activity, int resultCode, Intent resultIntent) { if (resultCode == RESULT_CANCELED) { Toast.makeText(activity, R.string.cancelled, Toast.LENGTH_SHORT).show(); return; } NdefMessage message = resultIntent.getParcelableExtra(NdefReaderActivity.EXTRA_RESULT); if (message == null) { Toast.makeText(activity, R.string.null_message, Toast.LENGTH_LONG).show(); return; } byte[] bytes = message.toByteArray(); String text = activity.getString(R.string.success_writing_tag, bytes.length); Toast.makeText(activity, text, Toast.LENGTH_LONG).show(); }
From source file:jp.co.brilliantservice.android.writertdtext.HomeActivity.java
/** * NdefMessageNdef??????// w w w . ja va 2 s .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:com.nxp.nfc_demo.fragments.SpeedTestFragment.java
private int eepromCalculateOverhead(int bytes) { int overhead = 0; String messageText = ""; for (int i = 0; i < bytes; i++) { messageText = messageText.concat(" "); }//from w w w . j a va2 s . co m // Calculate the overhead NdefMessage msg; try { msg = createNdefMessage(messageText); int ndef_message_size = (msg.toByteArray().length + 5); ndef_message_size = (int) Math.round(ndef_message_size / 4) * 4; overhead = ndef_message_size - (bytes); } catch (UnsupportedEncodingException e) { e.printStackTrace(); e.printStackTrace(); } return overhead; }
From source file:mai.whack.StickyNotesActivity.java
boolean writeTag(NdefMessage message, Tag tag) { int size = message.toByteArray().length; try {// www . ja va2s. com 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:de.berlin.magun.nfcmime.core.RfidDAO.java
/** * Writes an NDEF message on a NFC-compliant RFID transponder. * @param tag Tag/*from w w w . j a v a2 s . c om*/ * @param message the NdefMessage to write * @throws Exception */ public void writeMessage(Tag tag, NdefMessage message) throws Exception { // use NdefSingleton for I/O operations (which is also accessed by the separate Thread) NdefSingleton.getInstance().setNdef(Ndef.get(tag)); // make sure the tag can be written to if (!NdefSingleton.getInstance().getNdef().isWritable()) { throw new Exception("Tag is read-only!"); // TODO Create custom Exception for that purpose } // make sure the NDEF message is neither null, nor larger than the available tag memory if (message != null) { if (message.toByteArray().length > NdefSingleton.getInstance().getNdef().getMaxSize()) { throw new Exception("NDEF message too long!"); // TODO Create custom Exception for that purpose } } else { throw new NullPointerException(); } // create and run a IOThread that writes the message WriteTask wt = new WriteTask(message); IOThread writeThread = new IOThread(); writeThread.run(wt); }
From source file:ch.bfh.instacircle.NetworkActiveActivity.java
/** * Writes an NFC Tag// www . j a v a2 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 *///from ww w. j ava2 s . co m //@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; }