Code example for NdefMessage

Methods: toByteArray

		// Payload 
		byte[] payload = "Hello there!".getBytes();
		byte[] mimeBytes = "text/plain".getBytes(Charset.forName("US-ASCII"));
		NdefRecord cardRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
		NdefMessage message = new NdefMessage(new NdefRecord[] { cardRecord, appRecord});
		try { 
			// see if tag is already NDEF formatted 
			Ndef ndef = Ndef.get(tag);
			if (ndef != null) {
				if (!ndef.isWritable()) {
					displayMessage("Read-only tag."); 
					return false; 
				int size = message.toByteArray().length;
				if (ndef.getMaxSize() < size) {
					displayMessage("Tag doesn't have enough free space."); 
					return false; 
Experience pair programming with AI  Get Codota for Java