Code example for NdefMessage

Methods: toByteArray

-1
		if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
			Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
			NdefRecord record1 = createTextRecord(StringToWrite.getText()
					.toString());
 
			NdefMessage msg = new NdefMessage(new NdefRecord[] { record1 });
 
			if (writeTag(msg, detectedTag)) {
				Toast.makeText(this, "Success write operation!",
						Toast.LENGTH_LONG).show();
			} else { 
				Toast.makeText(this, "Failed to write!", Toast.LENGTH_LONG)
						.show();
			} 
		} 
	} 
 
	public NdefRecord createTextRecord(String payload) {
		byte[] textBytes = payload.getBytes();
		byte[] data = new byte[1 + textBytes.length];
		data[0] = (byte) 0;
		System.arraycopy(textBytes, 0, data, 1, textBytes.length);
		NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
				NdefRecord.RTD_TEXT, new byte[0], data);
		return record;
	} 
 
	public static 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()) {