NdefMessage toByteArray - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for NdefMessage toByteArray method.

This code example shows how to use the following methods:toByteArray
            NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
                    "text/x-su-a".getBytes(),
                    new byte[0], 
                    PreferenceManager.getDefaultSharedPreferences(this)
                            .getString("pin", "").getBytes());
            NdefMessage message = new NdefMessage(new NdefRecord[] {record });
 
            Ndef ndef = Ndef.get(tagFromIntent);
            if (ndef != null) {
                if (!ndef.isWritable()) {
                    Toast.makeText(this, "Tag not writeable", Toast.LENGTH_SHORT).show();
                    return; 
                } 
                 
                int maxSize = ndef.getMaxSize();
                 
                if (maxSize < message.toByteArray().length) {
                    Toast.makeText(this, "Tag not big enough", Toast.LENGTH_SHORT).show();
                    return; 
                } 

7
CodeRank
This code example shows how to use the following methods:toByteArray
      this.tag=tag;
    } 
     
    @Override 
    protected Void doInBackground(Void... arg0) {
      int size=msg.toByteArray().length;
 
      try { 
        Ndef ndef=Ndef.get(tag);
         
        if (ndef==null) {
          NdefFormatable formatable=NdefFormatable.get(tag);
           
          if (formatable!=null) {
            try { 
              formatable.connect();
               
              try { 
                formatable.format(msg);
              } 
6
CodeRank
This code example shows how to use the following methods:toByteArray
 
    /* 
     * Writes an NdefMessage to a NFC tag 
     */ 
    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()) {
                    Log.e(TAG, "Tag is not writable!");
                    return false; 
                } 
                if (ndef.getMaxSize() < size) {
                    Log.e(TAG,
                            "Tag exceeds max ndef message size! [" + size + " > "
                                    + ndef.getMaxSize() + "]");
                    return false; 
                } 
6
CodeRank
This code example shows how to use the following methods:toByteArray
        /** 
         * Converts an NdefMessage to ContentValues that can be insrted into this table. 
         */ 
        public static ContentValues toValues(Context context, NdefMessage msg, boolean isStarred,
                boolean isMyTag, long date) {
            ParsedNdefMessage parsedMsg = NdefMessageParser.parse(msg);
            ContentValues values = new ContentValues();
            values.put(BYTES, msg.toByteArray());
            values.put(DATE, date);
            values.put(STARRED, isStarred ? 1 : 0);
            values.put(IS_MY_TAG, isMyTag ? 1 : 0);
            values.put(TITLE, parsedMsg.getSnippet(context, Locale.getDefault()));
            return values;
        } 
    } 
} 
6
CodeRank
This code example shows how to use the following methods:toByteArray
     * write a message to a tag, handling various error conditions by returning 
     * false. If context != null, it makes Toast with the result 
     */ 
    public static boolean writeTag(Context context, NdefMessage message, Tag tag)
    { 
        int size = message.toByteArray().length;
 
        try 
        { 
            Ndef ndef = Ndef.get(tag);
            if (ndef != null)
            { 
                ndef.connect();
 
                if (!ndef.isWritable())
                { 
                    toast(context, "Tag is read-only.");
                    return false; 
                } 
                if (ndef.getMaxSize() < size)
5
CodeRank
This code example shows how to use the following methods:toByteArray
 
  /* 
  * Writes an NdefMessage to a NFC tag 
  */ 
  public static boolean writeNFCTag(NdefMessage message, Tag tag) {
    int size = message.toByteArray().length;
    try { 
        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);
5
CodeRank
This code example shows how to use the following methods:toByteArray
			mAdapter.disableForegroundDispatch(this);
		} 
	} 
 
	private void write(Tag tag) throws IOException, FormatException {
		NdefMessage rawMessage = new NdefMessage(
				NdefRecord.createUri(mTargetUrl));
		NdefFormatable format = NdefFormatable.get(tag);
		if (format != null) {
			Log.d(TAG, "Write unformatted tag");
			try { 
				format.connect();
				format.format(rawMessage);
			} catch (Exception e) {
			} finally { 
				try { 
					format.close();
				} catch (IOException e) {
				} 
			} 

5
CodeRank
This code example shows how to use the following methods:toByteArray
	    NdefRecord relayRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
	                                            new String("application/" + context.getPackageName()).getBytes(Charset.forName("US-ASCII")),
	                                            null, data.getBytes());
	  
	    // Complete NDEF message with both records 
	    NdefMessage message = new NdefMessage(new NdefRecord[] {relayRecord, appRecord});
	  
	    try { 
	        // If the tag is already formatted, just write the message to it 
	        Ndef ndef = Ndef.get(tag);
	        if(ndef != null) {
	            ndef.connect();
	  
	            // Make sure the tag is writable 
	            if(!ndef.isWritable()) {
	                //DialogUtils.displayErrorDialog(context, R.string.nfcReadOnlyErrorTitle, R.string.nfcReadOnlyError); 
	                return false; 
	            } 
	  
	            // Check if there's enough space on the tag for the message 

4
CodeRank
This code example shows how to use the following methods:toByteArray
            Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            // タグにEditTextの内容を書き込み。 
            NdefRecord appRecord = NdefRecord.createApplicationRecord("orz.kassy.nfclauncher.command");
            NdefRecord exRecord = NdefRecord.createExternal("orz.kassy.nfclauncher.command", "type_name",
                    "type_payload".getBytes());
            NdefMessage msg = new NdefMessage(new NdefRecord[] {
                    appRecord, exRecord
            }); 
            writeTag(msg, detectedTag);
        } 
    } 
 
    /** 
     * NFCにNDEFを書き込む処理 
     */ 
    boolean writeTag(NdefMessage message, Tag tag) {
        int size = message.toByteArray().length;
        try { 
            Ndef ndef = Ndef.get(tag);
            if (ndef != null) {

4
CodeRank
This code example shows how to use the following methods:toByteArray
    public boolean writeToTag(NdefMessage aMessage, Tag tag) {
        if (aMessage != null) {
            message = aMessage;
        } 
 
        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; 
                } 
5
CodeRank
toByteArray Method Overview
Return this NDEF Message as raw bytes.
See Code Examples for other Android NdefMessage Methods: